In JavaScript, a string (String) is a fixed sequence of characters consisting of zero or more Unicode characters; zero characters represent an empty string. Strings must be enclosed in single or double quotes, and each character in the string has a fixed position.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
JavaScript string (String) is a character sequence composed of zero or more Unicode characters. Zero characters represent an empty string.
String literal
Strings must be enclosed in single or double quotes. String literals have the following characteristics:
1) If the string is contained in double quotes, the string can contain single quotes; conversely, the string can also contain double quotes within single quotes. For example, when defining an HTML string, it is customary to use single quotes to represent the string, and the attribute values contained in HTML to use double quotes, so that errors are less likely to occur.
console.log('<meta charset="UTF-8">');
2) In ECMAScript 3, strings must be represented within one line, and newline representation is not allowed. For example, the following string literal is written incorrectly.
console.log("字符串 直接量"); //抛出异常
If you want to display the string in a new line, you can add the newline character \n in the string. For example:
console.log("字符串\n直接量"); //在字符串中添加换行符
3) In ECMAScript 5, strings allow multi-line representation. How to implement: Add a backslash\ at the end of the newline. Backslashes and newlines are not included as part of a string literal. For example:
console.log("字符串\ 直接量"); //显示“字符串直接量”
4) To insert special characters into a string, you need to use escape characters, such as single quotes, double quotes, etc. For example, single quotes are commonly used to represent apostrophes in English. If you use single quotes to define a string, you should add a backslash escape character. The single quotes will no longer be parsed as string identifiers, but used as apostrophes. .
console.log('I can\'t read.'); //显示"I can' t read."
5) Each character in the string has a fixed position. The subscript position of the first character is 0, the subscript position of the second character is 1... and so on, the subscript position of the last character is the length of the string minus 1.
Character sequence
JavaScript string is a fixed sequence of characters. Although various methods can be used to perform operations on the string, all returned are new String, the original string remains fixed. Additionally, you cannot use the delete operator to delete a character at a specified position in a string.
In ECMAScript 5, strings can be used as read-only arrays. In addition to using charAt() to access the characters, you can also use the bracket operator to access. Position subscripts start from 0 and the maximum position subscript is length-1.
Example
The following code uses the for statement to read each character in the string one by one and display it.
var str = "学而不思则罔,思而不学则殆"; for(var i=0; i<str.length; i++){ console.log(str[i]); }
Note: The characters in the string cannot be enumerated by the for/in statement loop.
Related recommendations: javascript learning tutorial
The above is the detailed content of What is a string in javascript. For more information, please follow other related articles on the PHP Chinese website!