Home > Web Front-end > JS Tutorial > body text

What is a string in javascript

青灯夜游
Release: 2023-01-06 11:16:56
Original
2896 people have browsed it

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.

What is a string in javascript

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(&#39;<meta charset="UTF-8">&#39;);
Copy after login

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("字符串
直接量"); //抛出异常
Copy after login

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直接量");  //在字符串中添加换行符
Copy after login

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("字符串\
直接量");  //显示“字符串直接量”
Copy after login

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(&#39;I can\&#39;t read.&#39;);  //显示"I can&#39; t read."
Copy after login

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]);
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template