String 全局对象是一个用于字符串或一个字符序列的构造函数。。
你也能使用 String 函数将其他值生成或转换成字符串:
String(thing)
new String(thing)
模板字面量
从 ECMAScript 2015 开始,字符串字面量也可以称为模板字面量:
hello world
hello! world!
hello ${who}
escape <a>${who}</a>
和其他语言不同,javascript 的字符串不区分单引号和双引号,所以不论是单引号还是双引号的字符串,上面的转义字符都能运行 。
长字符串
有时,你的代码可能含有很长的字符串。你可能想将这样的字符串写成多行,而不是让这一行无限延长或着被编辑器折叠。有两种方法可以做到这一点。
其一,可以使用 + 运算符将多个字符串连接起来,如下所示:
let longString = "This is a very long string which needs " +
"to wrap across multiple lines because " +
"otherwise my code is unreadable.";
其二,可以在每行末尾使用反斜杠字符(“\”),以指示字符串将在下一行继续。确保反斜杠后面没有空格或任何除换行符之外的字符或缩进; 否则反斜杠将不会工作。 如下所示:
let longString = "This is a very long string which needs \
to wrap across multiple lines because \
otherwise my code is unreadable.";
使用这两种方式会创建相同的字符串。
描述
字符串对于保存可以以文本形式表示的数据非常有用。 一些常用的字符串操作有:查询字符串长度,使用 + 和 += 运算符来构建和连接字符串,使用 indexOf 方法检查某一子字符串在父字符串中的位置,又或是使用 substring 方法提取从父字符串中提取子字符串。
从字符串中获取单个字符
获取字符串的某个字符有两种方法。 第一种是使用 charAt
方法:
return 'cat'.charAt(1); // returns "a"
另一种 (在ECMAScript 5中有所介绍) 是把字符串当作一个类似数组的对象,其中的每个字符对应一个数值索引:
return 'cat'[1]; // returns "a"
使用括号访问字符串不可以对其进行删除或添加,因为字符串对应未知的属性并不是可读或配置的。
字符串比较
,但在
JavaScript 中,你只需要使用比较操作符(>/</>=/<=):
var a = "a";var b = "b";
if (a < b) // true
print(a + " is less than " + b);
else if (a > b)
print(a + " is greater than " + b);
else
print(a + " and " + b + " are equal.");
使用从字符串实例继承而来的 localeCompare
方法也能达到同样的效果。
String几种方法的学习
- charAt (index)
var str = "HELLO WORLD";
var res = str.charAt(str.length-1); //D
- charCodeAt(index)
var str = "HELLO WORLD";
var n = str.charCodeAt(0); //72
-
concat(str)
var str1 = "Hello "; var str2 = "world!"; var res = str1.concat(str2); //Hello world!
endWith(searchvalue,length) length没有多大用,不填,前面字符串的长度就是length
var str = "Hello world, welcome to the universe.";
var n = str.endsWith("universe.");//true
- fromCharCode()
var res = String.fromCharCode(65); //A
- includes()
var str = "Hello world, welcome to the universe.";
var n = str.includes("world");
- indexOf(SearchValue , start) //0开始
var str = "Hello world, welcome to the universe.";
var n = str.indexOf("welcome");
- lastIndexOf(searchValue,start)
var str = "Hello planet earth, you are a great planet.";
var n = str.lastIndexOf("planet"); //36
- localCompare()
var str1 = "ab";
var str2 = "cd";
var n = str1.localeCompare(str2);
-1 // str1 is sorted before str2
var str1 = "ab";
var str2 = "ab";
var n = str1.localeCompare(str2);
0 // the two strings are equal
- match()
var str = "The rain in SPAIN stays mainly in the plain";
var res = str.match(/ain/g);
//ain,ain,ain
var str = "The rain in SPAIN stays mainly in the plain";
var res = str.match(/ain/gi);
// ain,AIN,ain,ain
- repeat(count)
var str = "Hello world!";
str.repeat(2);
//Hello world!Hello world!
- search()
var str = "Visit W3Schools!";
var n = str.search("W3Schools");
//6
- slice(start ,end) //0 开始 截取
Extracts a part of a string and returns a new string
var str = "Hello world!";
var res = str.slice(1,5);
//ello
var str = "Hello world!";
var res = str.slice(0); //没有指定结束位置,就是默认到结束
// Hello world!
- split() 按什么分隔
var str = "How are you doing today?";
var res = str.split(" ");
//How,are,you,doing,today?
var str = "How are you doing today?";
var res = str.split(""); //还有这种操作
// H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?
var str = "How are you doing today?";
var res = str.split();
//How are you doing today?
- startWith()
var str = "Hello world, welcome to the universe.";
var n = str.startsWith("Hello");
//true
var str = "Hello world, welcome to the universe.";
var n = str.startsWith("world", 6);
// true
- subStr()
The substr() method does not change the original string.
var str = "Hello world!";
var res = str.substr(1, 4);
//ello
var str = "Hello world!";
var res = str.substr(2);
//llo world!
var str = "Hello world!";
var res = str.substr(0, 1);
// H
- subString(start ,end)
The substring() method extracts the characters from a string, between two specified indices, and
returns the new sub string.
This method extracts the characters in a string between "start" and "end", not including "end" itself.
If "start" is greater than "end", this method will swap the two arguments, meaning str.substring(1,4) == str.substring(4,1).
If either "start" or "stop" is less than 0, it is treated as if it were 0.
var str = "Hello world!";
var res = str.substring(-3);
- trim()
var str = " Hello World! ";
alert(str.trim());
//Hello World!
- toLocaleLowerCase()
- toLocaleupperCase()
- toLowerCase()
- toUpperCase()
- valueOf()
- toString()
valueOf 和toString()的区别
总结:valueOf偏向于运算,toString偏向于显示。
1、 在进行强转字符串类型时将优先调用toString方法,强转为数字时优先调用valueOf。
2、 在有运算操作符的情况下,valueOf的优先级高于toString。