toupperCase() 将字符串的所有字符转换成大写字母;
indexOf() 返回字符串中特定字符串第一次出现的位置
substring() 返回字符串的某个子串
slice() 返回字符串中的某个子串,支持负数参数(字符串中倒数第一个字符定为-1)
concat() 用于将多个字符串拼加成一个字符串
replace() 将字符串中的某个子串以特定的字符串替换
split() 将某个字符串分割成多个字符串,可以指定分隔符
search() 使用正则表达式搜索目标子字符串(返回的是索引)
ps:JavaScript 特殊字符
你可以在 JavaScript 中使用反斜杠来向文本字符串添加特殊字符。
插入特殊字符
反斜杠用来在文本字符串中插入省略号、换行符、引号和其他特殊字符。
请看下面的 JavaScript 代码:
rush:js;">
var txt="We are the so-called "Vikings" from the north."
document.write(txt)
在 JavaScript 中,字符串使用单引号或者双引号来起始或者结束。这意味着上面的字符串将被截为:We are the so-called。
要解决这个问题,就必须把在 "Viking" 中的引号前面加上反斜杠 (\)。这样就可以把每个双引号转换为字面上的字符串。
rush:js;">
var txt="We are the so-called \"Vikings\" from the north."
document.write(txt)
现在 JavaScript 就可以输出正确的文本字符串了:We are the so-called "Vikings" from the north。
这是另一个例子:
rush:js;">
document.write ("You \& me are singing!")
上面的例子会产生以下输出:
You & me are singing!
下面的表格列出了其余的特殊字符,这些特殊字符都可以使用反斜杠来添加到文本字符串中:
ottom: rgb(63,63) 1px solid; color: rgb(255,255,255); padding-bottom: 5px; text-align: left; padding-top: 5px; padding-left: 6px; margin: 0px; border-left: rgb(63,63) 1px solid; padding-right: 15px; background-color: rgb(63,63)">代码 | 输出 | ottom: rgb(170,170) 1px solid; padding-bottom: 6px; padding-top: 6px; padding-left: 6px; margin: 0px; border-left: rgb(170,170) 1px solid; padding-right: 15px">\' | ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(245,245)">ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(255,170) 1px solid; padding-right: 15px">\& | ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(245,170) 1px solid; padding-right: 15px">\\ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(255,170) 1px solid; padding-right: 15px">\nottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(245,170) 1px solid; padding-right: 15px">\rottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(255,170) 1px solid; padding-right: 15px">\tottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(245,170) 1px solid; padding-right: 15px">\bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(255,170) 1px solid; padding-right: 15px">\f
---|
总结
以上所述是小编给大家介绍的JavaScript中字符串的常用操作方法,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!