JavaScript strings are used to store and manipulate text, and strings can store a range of characters, such as "John Doe"; A string can be any character inserted into quotation marks, and you can use either single or double quotation marks.
To Create a string:
1 //direct creation placed in single/double quotes2 varstr = "This is a string";//Double quotes3 varSTR1 = ' This is a string ';//single quotation marks4 5 /*6 * If you want to output quotation marks, there are two methods:7 * 1. Escape with escape character (\)8 * 2, using single double quotes nested (double quotation marks can only be nested single quotation marks, single quotation marks can only be nested inside the double quotation marks, otherwise it will be an error)9 */Ten varstr2 = "My name is ' Lily '"; OneConsole.log (STR2);//back to my name is ' Lily ' A varSTR3 = ' Lily say: ' Hello '; -Console.log (STR3);//back to Lily say: "Hello" - the varSTR4 = "Hello \" world\ "!"; -Console.log (STR4);//back to Hello "world" - - //created with new (not recommended, slow execution speed, and side effects) + varSTR1 =NewString ("This is a string");
String Object Properties:
Length-Calculates the lengths of the strings
constructor-constructor for creating strings
Prototype-adding properties and methods to an object
1 <script>2 var str = "Hello world"; 3 Console.log (str.length); // back 4 Console.log (str.constructor); // returns function String () {} 5 </script>
Common methods for string objects:
CharAt ()-Returns the character at the specified position (index)
charCodeAt ()-Returns the Unicode encoding of the character at the specified position
Concat ()-Connection string
IndexOf ()-Returns the position of the specified character (index)
Match ()-finds the match of one or more regular expressions
Replace ()-replaces a substring that matches a regular expression
Search ()-retrieves a value that matches a regular expression
Slice ()-Extracts a fragment of a string, returning the extracted part
Split ()-Convert string to string array
SUBSTRING ()-extracts the characters between two specified index numbers in a string
toLowerCase (), toLocaleLowerCase ()-Converts a string to lowercase
toUpperCase (), toLocaleUpperCase ()-Converts a string to uppercase
ToString ()-Returns a string
1<script>2 varstr = "Hello", str1 = "World";3Console.log (Str.charat (2));//return L4Console.log (Str.charcodeat (2));//returns the Unicode encoding of L 1085Console.log (Str.concat (str1));//back to HelloWorld6Console.log (Str.indexof ("O"));//returns 47Console.log (Str.slice (1,3));//returns El (ends at index 1 to index 3, including index 1 without index 3, i.e. [1,3))8Console.log (Str.split (""));//return ["H", "E", "L", "L", "O"]9Console.log (str.substring (1,3));//return to ElTenConsole.log (Str.tolowercase ());//return Hello OneConsole.log (Str.touppercase ());//return HELLO A</script>
Special characters:
code |
|
\ ' |
single quotes |
\ " |
double quotes |
\\ |
|
\n |
newline |
\r |
return |
\t |
tab (tab) |
\b |
backspace |
\f |
page break |
This article is only for oneself in peacetime work study to make notes to use! If there are errors please point out!!!
string of JavaScript