JavaScript string

JavaScript string

Strings can store a sequence of characters, such as "John Doe".

A string can be any character inserted into quotation marks. You can use single or double quotes:


You can use the index position to access each character in the string:

Strings are indexed from 0, which means that the first character has an index value of [0], the second is [1], and so on.

You can use quotes inside strings, but don't use the same quotes as strings:


string length

 The length of a string can be calculated using the built-in property  length :


 The backslash is an escape character . Escape characters convert special characters to string characters:

The escape character (\) can be used to escape apostrophes, newlines, quotes, and other special characters.

The following table lists the special characters that can be escaped in strings using escape characters:

code output
\' apostrophe
\" Double quotes
\\ backslash
\n newline
\r Enter
\t tab (tab)
\b backspace character
\f form feed


typeof determines the character type

Do not create String objects. It slows down execution and may have other side effects:


Difference between == and === in JavaScript

1. For basic types such as string and number, == and === are different

  • a) Comparison between different types, the comparison of == "transforms the value of the same type" to see if the "value" is equal, === If the types are different, the result is unequal.
  •  b) Same type comparison, direct "value" comparison, both results are the same.

2. For advanced types such as Array and Object, there is no difference between == and ===

Do a "pointer address" comparison

3. There is a difference between basic types and advanced types, == and ===

  • a) For ==, convert the high-level to the underlying type and perform a "value" comparison
  •  b) === result is false because the types are different

4. != is the negation of ==, and !== is the negation of ===

var num=1

var str="1"

var test=1

test == num   //true 相同类型 相同值 

test === num  //true 相同类型 相同值 

test !== num  //false test与num类型相同,其值也相同, 非运算肯定是false 

num == str   //true  把str转换为数字,检查其是否相等。 

num != str   //false  == 的 非运算 

num === str  //false  类型不同,直接返回false 

num !== str  //true   num 与 str类型不同 意味着其两者不等 非运算自然是true啦


字符串属性和方法

原始值字符串,如 "John", 没有属性和方法(因为他们不是对象)。

原始值可以使用 JavaScript 的属性和方法,因为 JavaScript 在执行方法和属性时可以把原始值当作对象。

字符串方法我们将在下一章节中介绍。


字符串属性

属性 描述
constructor 返回创建字符串属性的函数
length 返回字符串的长度
prototype 允许您向对象添加属性和方法

字符串方法

更多方法实例可以参见:JavaScript String 对象

方法 描述
charAt() 返回指定索引位置的字符
charCodeAt() 返回指定索引位置字符的 Unicode 值
concat() 连接两个或多个字符串,返回连接后的字符串
fromCharCode() 将 Unicode 转换为字符串
indexOf() 返回字符串中检索指定字符第一次出现的位置
lastIndexOf() 返回字符串中检索指定字符最后一次出现的位置
localeCompare() 用本地特定的顺序来比较两个字符串
match() 找到一个或多个正则表达式的匹配
replace() 替换与正则表达式匹配的子串
search() 检索与正则表达式相匹配的值
slice() 提取字符串的片断,并在新的字符串中返回被提取的部分
split() 把字符串分割为子字符串数组
substr() 从起始索引号提取字符串中指定数目的字符
substring() 提取字符串中两个指定的索引号之间的字符
toLocaleLowerCase() 根据主机的语言环境把字符串转换为小写,只有几种语言(如土耳其语)具有地方特有的大小写映射
toLocaleUpperCase() 根据主机的语言环境把字符串转换为大写,只有几种语言(如土耳其语)具有地方特有的大小写映射
toLowerCase() 把字符串转换为小写
toString() 返回字符串对象值
toUpperCase() 把字符串转换为大写
trim() 移除字符串首尾空白
valueOf() 返回某个字符串对象的原始值



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325989934&siteId=291194637