字符串的一些用法总结

1. 两个用于访问字符串中特定字符 charAt() 和 charCodeAt() 

两者都只有一个参数,不同的是前者返回字符,后者返回字符的字符编码。 

var stringValue = "hello world";
console.log(stringValue.charAt(1)); //"e"

var stringValue = "hello world"; 
console.log(stringValue.charCodeAt(1)); //输出"101" 

2. 字符串拼接方法 concat() 

用于将一或多个字符串拼接起来,返回拼接得到的新字符串,实战中还是更多的使用 ‘+’ 操作符来拼接。

var stringValue = "hello "; 
var result = stringValue.concat("world"); 
console.log(result); //"hello world" 
console.log(stringValue); //"hello" 

3. 字符串创建方法 slice()、substr()、substring()

该三个方法都可以传递一个或两个参数,第一个参数都是字符串开始的位置,slice() 与 substring() 方法的第二个参数表示结束位置,返回第一个参数到第二个参数之间的字符串片段,substr() 的第二个参数表示返回字符串的个数。三个方法第二个如果不给,则默认返回当前位置到最后的字符串。举例:

var stringValue = "hello world"; 
console.log(stringValue.slice(3)); //"lo world" 
console.log(stringValue.substring(3)); //"lo world" 
console.log(stringValue.substr(3)); //"lo world" 
console.log(stringValue.slice(3, 7)); //"lo w" 
console.log(stringValue.substring(3,7)); //"lo w" 
console.log(stringValue.substr(3, 7)); //"lo worl" 

如过传入的值为负值,slice()方法会将传入的负值与字符串的长度相加,substr() 方法将负的第一个参数加上字符串的长度,而将负的第二个参数转换为 0。最后,substring() 方法会把所有负值参数都转换为 0。下面来看例子。

var stringValue = " hello world "; 
var trimmedStringValue = stringValue.trim(); 
console.log(stringValue); //" hello world alert(trimmedStringValue); //"hello world" 

4. 从字符串中查找子字符串的方法 indexOf() 和 lastIndexOf()

indexOf()方法从字符串的开头向后搜索子字符串,而 lastIndexOf()方法是从字符串的末尾向前搜索子字符串。举例:

var stringValue = "hello world"; 
console.log(stringValue.indexOf("o")); //4 
console.log(stringValue.lastIndexOf("o")); //7 

这两个方法都可以接收可选的第二个参数,表示从字符串中的哪个位置开始搜索。

var stringValue = "hello world"; 
console.log(stringValue.indexOf("o", 6)); //7 
console.log(stringValue.lastIndexOf("o", 6)); //4 

5. trim()方法

删除字符串前后的空格。举例:

var stringValue = " hello world ";
var trimmedStringValue = stringValue.trim();
console.log(trimmedStringValue); //"hello world"

6. 字符串大小写转换方法 toLowerCase()、toLocaleLowerCase()、toUpperCase()和 toLocaleUpperCase()

 直接举例:

var stringValue = "hello world"; 
console.log(stringValue.toLocaleUpperCase()); //"HELLO WORLD" 
console.log(stringValue.toUpperCase()); //"HELLO WORLD" 
console.log(stringValue.toLocaleLowerCase()); //"hello world" 
console.log(stringValue.toLowerCase()); //"hello world" 

7. 字符串的模式匹配方法  match()、search()、replace()

match()方法接受一个参数要么是一个正则表达式,要么是一个 RegExp 对象,与RegExp的exec() 方法相同。

var text = "cat, bat, sat, fat"; 
var pattern = /.at/; 
//与 pattern.exec(text)相同
var matches = text.match(pattern); 
console.log(matches.index); //0 
console.log(matches[0]); //"cat" 
console.log(pattern.lastIndex); //0 

search()方法接受一个参数要么是一个正则表达式,要么是一个 RegExp 对象,如果匹配则返回第一次出现的位置,否则返回-1。

var text = "cat, bat, sat, fat"; 
var pos = text.search(/at/); 
console.log(pos); //1

replace()方法第接受两个参数可以是一个 RegExp 对象或者一个字符串(这个字符串不会被转换成正则表达式),第二个参数可以是一个字符串或者一个函数。如果第一个参数是字符串,那么只会替换第一个子字符串。要想替换所有子字符串,唯一的办法就是提供一个正则表达式,而且要指定全局(g)标志。举例:

var text = "cat, bat, sat, fat"; 
var result = text.replace("at", "ond"); 
console.log(result); //"cond, bat, sat, fat" 
result = text.replace(/at/g, "ond"); 
console.log(result); //"cond, bond, sond, fond" 

8. localeCompare()

与操作字符串有关的最后一个方法是 localeCompare(),这个方法比较两个字符串,并返回下列
值中的一个:

  • 如果字符串在字母表中应该排在字符串参数之前,则返回一个负数(大多数情况下是-1,具体的值要视实现而定);
  • 如果字符串等于字符串参数,则返回 0;
  • 如果字符串在字母表中应该排在字符串参数之后,则返回一个正数(大多数情况下是 1,具体的值同样要视实现而定)。

下面是几个例子:

var stringValue = "yellow"; 
console.log(stringValue.localeCompare("brick")); //1 
console.log(stringValue.localeCompare("yellow")); //0 
console.log(stringValue.localeCompare("zoo")); //-1 

本文结。

发布了29 篇原创文章 · 获赞 35 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/linxner/article/details/87364570
今日推荐