美文网首页
字符串方法

字符串方法

作者: 麓语 | 来源:发表于2018-08-08 14:42 被阅读0次

字符方法

charAt()

接受一个参数:基于0的字符位置
返回值:指定位置的字符

var str = 'hello world';
console.log(str.charAt(1));                     // => 'e'
charCodeAt()

接受一个参数:基于0的字符位置
返回值:指定位置字符的字符编码

var str = 'hello world';
console.log(str.charCodeAt(1));                // => 101
ECMAScript 5 访问个别字符方法

使用方括号加数字索引来访问字符串中特定字符

var str = 'hello world';
console.log(str[1])                           // => 'e'

字符串操作方法

concat()

可以接受任意多个参数,用于一个或多个字符串拼接起来

var str = 'hello ';
var resultStr = str.concat('world', '!');
console.log(resultStr);                      // => 'hello world!'
console.log(str);                            // => 'hello '
slice() 、substr() 、substring()

参数1:指定子字符串的开始位置
参数2:(可选)在指定的情况下,表示字符串到哪里结束
slice()substring()的第二个参数指定的是子字符串最后一个字符后面的位置,substr()的第二个参数是指定返回的字符个数
这三个方法都不会修改原字符串的值,只是返回一个基本类型的字符串值

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

在传递参数存在负值的情况下:
slice()会将传入的负值与字符串的长度相加;
substr()会将负的第一个参数与字符串的长度相加,将负的第二个参数转换为0

substring()会将所有负值参数都转化为0

var str = 'hello world';
console.log(str.slice(-3));                    // => 'rld'
console.log(str.substring(-3));                // => 'hello world'
console.log(str.substr(-3));                   // => 'rld'
console.log(str.slice(3 ,-4));                 // => 'lo w'
console.log(str.substring(3, -4));             // => 'hel'
console.log(str.substr(3, -4));                // => ''

字符串位置方法

indexOf()、lastIndexOf()

从字符串中检索给定的子字符串,然后返回子字符串的位置(如果没有找到该子字符串,则返回-1
indexOf()从字符串的开头向后搜索子字符串;
lastIndexOf()从字符串末尾向前搜索子字符串

var str = 'hello world';
console.log(str.indexOf('o'));                  // => 4
console.log(str.lastIndexOf('o'));              // => 7

这两个方法都可以接受第二个参数:表示从字符串中的哪个位置开始搜索
indexOf()从指定的位置向后搜索子字符串;
lastIndexOf()从指定的位置向前搜索子字符串

var str = 'hello world';
console.log(str.indexOf('o', 6));                  // => 7
console.log(str.lastIndexOf('o', 6));              // => 4

trim()方法

ECMAScript 5 为字符串定义了trim()方法:创建一个字符串的副本,删除前置和后缀的所有空格

var str = '     hello world     ';
var resultStr = str.trim();
console.log(str);                    // => '     hello world     '
console.log(resultStr);              // => 'hello world'

Chrome 8+ 支持trimLeft()trimRight(),分别用于删除开头和结尾的空格

字符串大小写转化方法

toLowerCase()、toLocaleLowerCase()、toUpperCase()、toLocaleUpperCase()
var str = 'hello world';
console.log(str.toLocaleUpperCase());          // => 'HELLO WORLD'
console.log(str.toUpperCase());                // => 'HELLO WORLD'
var str2 = 'HELLO WORLD';
console.log(str2.toLocaleLowerCase());         // => 'hello world'
console.log(str2.toLowerCase());               // => 'hello world'

字符串的模式匹配方法

match()

只接受一个参数:要么是一个正则表达式,要么是一个RegExp对象

var str = 'cat, bat, sat, fat';
var reg = /.at/;
var matches = str.match(reg);
console.log(matches.index);                  // => 0
console.log(matches[0]);                     // => 'cat'
console.log(reg.lastIndex);                  // => 0

这里当正则匹配全局模式时matches结果就是一个普通数组,包含所有匹配项的数组,不带有index等属性

search()

参数: 和match()方法参数相同:由字符串或RegExp对象指定的一个正则表达式
返回值: 字符串中第一个匹配项的索引,如果没有匹配项就返回-1

var str = 'cat, bat, sat, fat';
var reg = /at/;
var pos = str.search(reg);
console.log(pos);                          // => 1
replace()

参数1:

相关文章

  • JAVA之UUID去掉横线

    方法一(字符串截取) 方法一 方法二(字符串分割) 方法二

  • 字符串格式化,字符串方法

    字符串格式化 方法一 方法二 字符串方法

  • spring工具类最佳实践

    字符串处理函数 StringUtils方法分为三大类: 普通方法 格式化方法 字符串数组方法 常规方法 判定字符串...

  • 8章 对象方法

    本章大纲 字符串的方法 列表的方法 查看python手册 对象方法概念 字符串的方法 count 计算字符串中包含...

  • String 常用方法汇总

    截取字符串 字符串替换 字符串拼接 Stringbuilder 方法 StringJoiner 方法 setEmp...

  • String 字符串常用操作

    indexof()方法 substring() 方法substring() 方法返回字符串的子字符串 replac...

  • 针对ES6的新知识学习

    字符串startsWith()方法 判断字符串string是否是以str开头 字符串endsWith()方法 判断...

  • python字符串

    字符串常用方法 1、首字母大写 capitalize方法 2、字符串替换 replace方法 参数1:老字符串 参...

  • 字符串`trim()`方法的使用

    字符串trim()方法 trim()方法并不影响字符串本身,它返回的是一个新的字符串 trim()方法去除字符串两...

  • iOS - 字符串方法

    删除字符串中的空格 用系统替换字符串方法 字符串分割 字符串分割方法 一字符串是否包含另一字符串 判断方法 大写转...

网友评论

      本文标题:字符串方法

      本文链接:https://www.haomeiwen.com/subject/mhntbftx.html