javaScript 字符串用于存储和处理文本,几乎被全部的编程语言所实现(然而c、c++没有提供)。多数开发者几乎天天都在和字符串打交道,语言内置的String模块,极大地提高了开发者的效率。本文对js字符串经常使用的方法进行了整理java
属性 | 描述 |
---|---|
constructor | 返回建立字符串属性函数 |
length | 返回字符串的长度 |
prototype | 容许您向对象添加属性和方法 |
返回在指定位置的字符c++
例子1:正则表达式
返回字符串中的第三个字符编程
var str = "HELLO WORLD";
var n = str.charAt(2)
console.log(n);
复制代码
运行结果 数组
例子2:bash
返回字符串中的最后一个字符markdown
var str = "HELLO WORLD";
var n = str.charAt(str.length-1);
console.log(n);
复制代码
运行结果 编程语言
返回在指定的位置的字符的Unicode编码函数
例子1:ui
返回字符串第一个字符的 Unicode 编码
var str = "HELLO WORLD";
var n = str.charCodeAt(0);
console.log(n);
复制代码
运行结果
例子2:
返回字符串中最后一个字符的 Unicode 编码
var str = "HELLO WORLD";
var n = str.charCodeAt(str.length-1);
console.log(n);
复制代码
运行结果
链接两个或更多字符串,并返回新的字符串
例子1:
链接两个字符串
var str1 = "Hello ";
var str2 = "world!";
var n = str1.concat(str2);
console.log(n);
复制代码
运行结果
例子2:
链接3个字符串
var str1="Hello ";
var str2="world!";
var str3=" Have a nice day!";
var n = str1.concat(str2,str3);
console.log(n);
复制代码
运行结果
将Unicode编码转为字符
例子1:
将 Unicode 编码转为一个字符
var n = String.fromCharCode(65);
console.log(n);
复制代码
运行结果
例子2:
将 Unicode 编码转换为一个字符串
var n = String.fromCharCode(72,69,76,76,79);
console.log(n);
复制代码
运行结果
返回某个指定的字符串值在字符串中首次出现的位置
例子1:
查找字符串 "welcome"
var str="Hello world, welcome to the universe.";
var n=str.indexOf("welcome");
console.log(n);
复制代码
运行结果
例子2:
在字符串查找字符 "e" 第一次出现的位置
var str="Hello world, welcome to the universe.";
var n=str.indexOf("e");
console.log(n);
复制代码
运行结果
例子3:
在字符串第五个位置开始查找字符 "e" 第一次出现的位置
var str="Hello world, welcome to the universe.";
var n=str.indexOf("e",5);
console.log(n);
复制代码
运行结果
查找字符串中是否包含指定的子字符串
例子1:
查找字符串是否包含 "world"
var str = "Hello world, welcome to the Runoob。";
var n = str.includes("world");
console.log(n);
复制代码
运行结果
例子2:
从第 12 个索引位置开始查找字符串
var str = "Hello world, welcome to the Runoob.";
var n = str.includes("world", 12);
console.log(n);
复制代码
运行结果
从后向前搜索字符串,并从起始位置(0)开始计算返回字符串最后出现的位置
例子1:
查找字符串 "runoob" 最后出现的位置
var str="I am from runoob,welcome to runoob site.";
var n=str.lastIndexOf("runoob");
console.log(n);
复制代码
运行结果
例子2:
从第 20 个字符开始查找字符串 "runoob" 最后出现的位置
var str="I am from runoob,welcome to runoob site.";
var n=str.lastIndexOf("runoob", 20);
console.log(n);
复制代码
运行结果
查找找到一个或多个正则表达式的匹配
例子1:
在字符串中查找 "ain"
var str="The rain in SPAIN stays mainly in the plain";
var n=str.match(/ain/g);
console.log(n);
复制代码
运行结果
例子2:
全局查找字符串 "ain",且不区分大小写
var str="The rain in SPAIN stays mainly in the plain";
var n=str.match(/ain/gi);
console.log(n);
复制代码
运行结果
复制字符串指定次数,并将它们链接在一块儿返回
例子1:
复制字符串 "Runoob" 两次
var str = "Runoob";
console.log(str.repeat(2));
复制代码
运行结果
在字符串中查找匹配的子串, 并替换与正则表达式匹配的子串
例子1:
在本例中,咱们将执行一次替换,当第一个 "Microsoft" 被找到,它就被替换为 "Runoob"
var str="Visit Microsoft! Visit Microsoft!";
var n=str.replace("Microsoft","Runoob");
console.log(n);
复制代码
运行结果
例子2:
执行一个全局替换
var str="Mr Blue has a blue house and a blue car";
var n=str.replace(/blue/g,"red");
console.log(n);
复制代码
运行结果
例子3:
执行一个全局替换, 忽略大小写
var str="Mr Blue has a blue house and a blue car";
var n=str.replace(/blue/gi, "red");
console.log(n);
复制代码
运行结果
查找与正则表达式相匹配的值
例子1:
查找 "Runoob"
var str="Visit Runoob!";
var n=str.search("Runoob");
console.log(n);
复制代码
运行结果
例子2:
执行一次对大小写敏感的查找
var str="Mr. Blue has a blue house";
console.log(str.search("blue"));
复制代码
运行结果
例子3:
执行一次忽略大小写的检索
var str="Mr. Blue has a blue house";
console.log(str.search(/blue/i));
复制代码
运行结果
提取字符串片断,并在新的字符串中返回被提取的部分
例子1:
提取字符串的片段
var str="Hello world!";
var n=str.slice(1,5);
console.log(n);
复制代码
运行结果
例子2:
提取最后一个字符
var str="Hello world!";
var n=str.slice(-1);
console.log(n);
复制代码
运行结果
把字符串分割成字符串数组
例子1:
把一个字符串分割成字符串数组
var str="How are you doing today?";
var n=str.split(" ");
console.log(n);
复制代码
运行结果
例子2:
n 将输出3个数组的值
var str="How are you doing today?";
var n=str.split(" ",3);
console.log(n);
复制代码
运行结果
例子3:
使用一个字符做为分隔符
var str="How are you doing today?";
var n=str.split("o");
console.log(n);
复制代码
运行结果
查看字符串是否以指定的子字符串开头
例子1:
查看字符串是否为 "Hello" 开头
var str = "Hello world, welcome to the Runoob.";
var n = str.startsWith("Hello");
console.log(n);
复制代码
运行结果
例子2:
查看从第 6 个索引位置是否以 "world" 开头
var str = "Hello world, welcome to the Runoob.";
var n = str.startsWith("world", 6);
console.log(n);
复制代码
运行结果
从起始索引号提取字符串中指定数目的字符
例子1:
抽取指定数目的字符
var str="Hello world!";
var n=str.substr(2,3)
console.log(n);
复制代码
运行结果
例子2:
在本例中,咱们将使用 substr() 从字符串第二个位置中提取一些字符
var str="Hello world!";
var n=str.substr(2)
console.log(n);
复制代码
运行结果
提取字符串中两个指定的索引号之间的字符
例子1:
在本例中,咱们将使用 substring() 从字符串中提取一些字符
var str="Hello world!";
console.log(str.substring(3));
console.log(str.substring(3,7));
复制代码
运行结果
把字符串转换为小写
例子1:
把字符串转换为小写
var str="Runoob";
console.log(str.toLowerCase());
复制代码
运行结果
把字符串转换为大写
例子1:
把字符串转换为大写
var str="Runoob";
console.log(str.toUpperCase());
复制代码
运行结果
去除字符串两边的空白
例子1:
去除字符串的头尾空格
var str = " Runoob ";
console.log(str.trim());
复制代码
运行结果
根据本地主机的语言环境把字符串转换为小写
例子1:
将字符串转换为小写
var str = "Runoob";
var res = str.toLocaleLowerCase();
console.log(res);
复制代码
运行结果
根据本地主机的语言环境把字符串转换为大写
例子1:
将字符串转换为大写
var str = "Runoob";
var res = str.toLocaleUpperCase();
console.log(res);
复制代码
运行结果
返回某个字符串对象的原始值
例子1:
返回 String 对象的原始值
var str="Hello world!";
console.log(str.valueOf());
复制代码
运行结果
返回一个字符串
例子1:
返回一个 String 对象的值
var str = "Runoob";
var res = str.toString();
console.log(res);
复制代码
运行结果
返回布尔值,表示是否找到参数字符串
let s='Hello world!';
console.log(s.includes('o'));
复制代码
运行结果
支持第二参数,表示开始搜索的位置
let s='Hello world!';
console.log(s.startsWith('world',6));
复制代码
运行结果
返回布尔值,表示参数字符串是否在源字符串的头部
let s='Hello world!';
console.log(s.startsWith('Hello'));
复制代码
运行结果
支持第二参数,表示开始搜索的位置
let s='Hello world!';
console.log(s.endsWith('Hello',5));
复制代码
运行结果
返回布尔值,表示参数字符串是否在源字符串的尾部
let s='Hello world!';
console.log(s.endsWith('!'));
复制代码
运行结果
支持第二参数,表示开始搜索的位置
let s='Hello world!';
console.log(s.includes('Hello',6));
复制代码
运行结果
返回一个新字符串,表示将原字符串重复n次
console.log('a'.repeat(3));
console.log('a'.repeat(0));
console.log('a'.repeat(2.6));
复制代码
运行结果
注意:
字符串补全长度的功能,若是某个字符串不够指定长度,会在头部或尾部补全
padStart()用于头部补全
padEnd()用于尾部补全
注意: padStart和padEnd一共接受两个参数,第一个参数是字符串补全生效的最大长度,第二个参数是用来补全的字符串
console.log('a'.padStart(5, 'cd'));
console.log('a'.padStart(4, 'cd'));
console.log('a'.padEnd(5, 'cd'));
console.log('a'.padEnd(4, 'cd'));
复制代码
运行结果
若是原字符串的长度,等于或大于最大长度,则字符串补全不生效,返回原字符串
console.log('aaa'.padStart(2, 'cd'));
console.log('aaa'.padEnd(2, 'cd'));
复制代码
运行结果
用来补全的字符串与原字符串,二者的长度之和超过了最大长度,则会截去超出位数的补全字符串
console.log('abc'.padStart(10, '0123456789'));
复制代码
运行结果
若是省略第二个参数,默认使用空格补全长度
console.log('a'.padStart(4));
console.log('a'.padEnd(4));
复制代码
运行结果
模板字符串是加强版的字符串,用反引号(`)标识
let temps="abc"
console.log(`ABC is ${temps}`)
复制代码
运行结果