Defining a String Object
A JavaScript string object is used to work with text strings. The syntax for creating a String object is as follows:
Copy Code code as follows:
<script language= "JavaScript" >
var str_object = new String (str);
var str1 = String (str);
var str2 = str;
</script>
of the above three methods, only the first one is to use the string constructor to strictly define a string object and return an object. The second is to call the string function, and the conversion parameter str to the original string string and return. The third is to define a string variable, but the JavaScript is still processed by the string object.
You can tell the difference by running the following statement:
Copy Code code as follows:
Alert (typeof Str_object); Output Object
Alert (typeof str1); Output string
Alert (typeof str2); Output string
String Object Properties
Property |
Description |
Constructor |
A reference to the function that created the object |
Length |
Length of string |
Prototype |
Adding properties and methods to an object |
String Object method
A list of common methods for String objects is as follows:
String output
Alert (): Message warning Box output text
document.write (): Output text to a Web page
String manipulation
Concat (): Connecting two or more strings
Replace (): string substitution or regular matching substitution
Slice (): to intercept a string by specifying a start and end position
Split (): Dividing a string into an array of strings
SUBSTR (): Intercepting strings based on start position and length
SUBSTRING (): To intercept a string by specifying a start and end position
String conversions
toLowerCase (): Converts a string to lowercase
toUpperCase (): Converts a string to uppercase
fromCharCode (): Converts one or more Unicode values to strings
String Lookup
CharAt (): Gets the character at the specified position
charCodeAt (): Gets the Unicode encoding of the specified location character
IndexOf (): Calculates the first occurrence of a specified string in a string
LastIndexOf (): Calculates the first occurrence of a specified string in a string
HTML Tag Class
Bold (): Gets the character at the specified position
FontColor (): Displays the string according to the specified color
FontSize (): Displays the string according to the specified size
Italics (): Display string as italic
Link (): Adds a hyperlink to a string
Strike (): Add Strikethrough to String
Sub (): Display string as subscript
SUP (): Display string as superscript