Define a String object
JavaScript String object is used to process text strings. The syntax for creating a String object is as follows:
The code copy is as follows:
<script language="JavaScript">
var str_object = new String( str );
var str1 = String( str );
var str2 = str;
</script>
Among the above three methods, only the first is to use the String constructor to strictly define a string object, and the returned object is also an object. The second is to call the String function, convert the parameter str to the original string and return it. The third type is to define a string variable, but in JavaScript it is still processed as a string object.
Run the following statement to know their differences:
The code copy is as follows:
alert( typeof str_object );// Output object
alert( typeof str1 );// Output string
alert( typeof str2 );// Output string
String object properties
| property | describe |
|---|---|
| constructor | Reference to the function that created the object |
| length | The length of the string |
| prototype | Add properties and methods to objects |
String object method
A list of common methods for String objects is as follows:
String output
alert(): The message warning box output text
document.write(): Output text to the web page
String operation
concat(): concatenate two or more strings
replace(): String replacement or regular match replacement
slice(): Intercepts string by specifying the start and end positions
split(): Split string into string array
substr(): Intercept strings based on start position and length
substring(): Intercept string by specifying the start and end positions
String conversion
toLowerCase(): Convert string to lowercase
toUpperCase(): Convert string to uppercase
fromCharCode(): Convert one or more Unicode values to strings
String search
charAt(): Get the character at the specified position
charCodeAt(): Get Unicode encoding of the specified position character
indexOf(): calculates the first occurrence of a specified string in the string
lastIndexOf(): Calculate the first occurrence of a specified string in the string.
HTML tag class
bold(): Get the character at the specified position
fontcolor(): Display strings according to specified color
fontsize(): Display strings according to specified size
italics(): Display strings as italics
link(): Add a hyperlink to the string
Strike(): Add a delete line to the string
sub(): Display the string as a subscript
sup(): Display the string as a superscript