定义方式
var str = 'hello world!';----字面量定义方式var str = new String('hello world!');----构造方法定义方式
获取
使用[]针对字符串操作,可以获取字符串对应位置字符,但不能修改和删除该字符
var str = 'hello world';
console.log(str[4]); //o
str[4] = 'x';
console.log(str); //hello world
length
获取长度
var str = 'hello world';
console.log(str.length); //11
常用ASCII码对应值
字符与ASCII码无条件相等
- 13-------回车
- 32-------space
- 48-------“0”
- 65-------A
- 90-------Z
- 97-------a
常用方法
charAt
- 功能:返回当前下标所对应的字符
- 用法:
str.charAt(index);下标默认为0
var str = 'hello world';
console.log(str.charAt(6)); //w
charCodeAt
- 功能:返回当前下标所对应的字符ASCII值
- 用法:
str.charCodeAt(index);下标默认为0
var str = 'hello world';
console.log(str.charCodeAt()); //104
codePointAt
- 功能:返回字符串的unicode编码
- 用法:
str.codePointAt(str);
var str = '叶';
console.log(str.codePointAt()); //21494
javascript内部,字符以utf-16的格式存储,每个中文字符固定为2个字节,对于那些需要4个字节存储的字符(unicode码大于0xFFFF的字符),javascript会认为他们是两个字符。如:"𡱖”汉字编码为21c56(需要4个字节),超过了FFFF。即超过javascript本身能表示的字符的最大范围,javascript就会把它当作两个字符处理,charAt()函数一次只能读取该汉字的其中一半。
例:
var str = '𡱖';
console.log(str.length); //2
console.log(str.codePointAt().toString(16)); //21c56 打印16进制的unicode编码值
console.log(str.charAt(0)); //? 访问不到
console.log("\u{21c56}"); //𡱖 通过编码来访问字体本身
String.fromCharCode
- 功能:将ASCII码值转换为字符
- 用法:
String.charCodeAt(ASCII码,...,[ASCII码]);静态方法,通过String调用
console.log(String.fromCharCode(104)); //h
console.log(String.fromCharCode(104, 101, 108, 108, 111)); //hello
indexOf
- 功能:查找字符串第一次出现的位置,返回下标,不存在返回-1
- 用法:
str.indexOf(string,[start]);指定开始位置后从前向后查找
var str = 'hello world';
console.log(str.indexOf('o')); //4
console.log(str.indexOf('o', 6)); //7
console.log(str.indexOf('aye')); //-1
//判断某个字符串是否存在于另一个字符串中:
if (str.indexOf('0') !== -1) { } //存在
if (~str.indexOf('0')) { } //存在
if (!~str.indexOf('0')) { } //不存在
lastIndexOf
- 功能:查找字符串最后一次出现的位置并返回下标,不存在返回-1
- 用法:
str.lastIndexOf(string,[start]);指定开始位置后从后向前查找
var str = 'hello world';
console.log(str.lastIndexOf('o')); //7
console.log(str.lastIndexOf('o',6)); //4
search
- 功能:检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串,找到返回下标,没找到返回-1
- 用法:
str.lastIndexOf(string/regex);
var str = 'hello world';
console.log(str.search('world')); //6
console.log(str.search('aye')); //-1
console.log(str.search(/l+/)); //2
replace
- 功能:替换字符串
- 用法:
str.replace(src/regex,dest);默认只能替换第一个出现的源字符,可通过正则替换所有位置出现的字符串
var str = 'hello world';
console.log(str.replace('world', 'aye')); //hello aye
console.log(str.replace('l', 'y')); //heylo world
console.log(str.replace(/l/g, 'y')); //heyyo woryd
concat
- 功能:连接一个或多个字符串
- 用法:
str.concat(str,...,[strn]);
var str = 'hello';
console.log(str.concat(' world')); //hello world
console.log(str.concat(' world hh')); //hello world hh
slice
- 功能:截取从开始位置到结束位置处的字符,包含开始位置,不包含结束位置
- 用法:
str.slice(start,[end]);参数可为负数,不可逆向;如果不指定结束位置,将截取从开始位置到后面所有的字符串
var str = 'hello world';
console.log(str.slice(6)); //world
console.log(str.slice(6, 10)); //worl
console.log(str.slice(6, -1)); //worl
console.log(str.slice(-4, -1)); //orl
substring
- 功能:截取从开始位置到结束位置处的字符,包含开始位置,不包含结束位置
- 用法:
str.substring(start,[end]);参数不可为负数,可逆向;如果不指定结束位置,将截取从开始位置到后面所有的字符串
var str = 'hello world';
console.log(str.substring(6)); //world
console.log(str.substring(6, 11)); //world
//逆向:
console.log(str.substring(11, 6)); //world
substr
- 功能:截取从开始位置指定长度的字符,包含开始位置,不包含结束位置
- 用法:
str.substr(start,[length]);如果长度不指定,默认截取到最后
var str = 'hello world';
console.log(str.substr(6)); //world
console.log(str.substr(6, 2)); //wo
split
- 功能:字符串转数组,该数组是通过在分隔符指定的边界处将字符串string分割成子串的。返回的数组中的字串不包括分隔符自身。
- 用法:
str.split([分隔符/regex]); - 参数:
- 如果把空字符串"”用作分隔符,那么string中的每个字符之间都会被分割 //单词分为字母
- 如果以空格用作” ”分隔符,那么string中的每个词之间会被分割 //句子分为单词
- 不指定参数,将整体当作一个数组
var str = 'hello world';
console.log(str.split()); //['hello world'];
console.log(str.split('')); //['h','e','l','l','o','','w','o','r','l','d'];
console.log(str.split(' ')); //['hello','world'];
console.log(str.split('wo')); //['hello','rld'];
toUpperCase
- 功能:转大写
- 用法:
str.toUpperCase();
var str = 'hello world';
console.log(str.toUpperCase()); //HELLO WORLD
toLowerCase
- 功能:转小写
- 用法:
str.toLowerCase();
var str = 'HELLO WORLD';
console.log(str.toLowerCase()); //hello world
includes
- 功能:判断字符串是否包含在另一个字符串中,返回布尔值,表示是否找到了参数字符串
- 用法:
str.includes(str);
var str = 'hello world';
console.log(str.includes('aye')); //false
console.log(str.includes('wor')); //true
startsWith
- 功能:判断字符串参数是否在源字符串的头部,返回布尔值
- 用法:
str.startsWith(str);
var str = 'http://www.xxx.com';
console.log(str.startsWith('https')); //false
console.log(str.startsWith('http')); //true
endsWith
- 功能:判断字符串参数是否在源字符串的尾部,返回布尔值
- 用法:
str.endsWith(str);
var str = 'xxx.jpg';
console.log(str.endsWith('.jpeg')); //false
console.log(str.endsWith('.jpg')); //true
padStart
- 功能:从字符串的开头用另一个字符串填充字符串到指定长度
- 用法:
str.padStart(length,str);
var str = '1';
console.log(str.padStart(3, 0)); //001
padEnd
- 功能:从字符串的尾部用另一个字符串填充字符串到指定长度
- 用法:
str.padEnd(length,str);
var str = '1';
console.log(str.padEnd(3, 0)); //100
JSON对象和字符串的相互转换
JSON.parse
- 功能:字符串转json对象,返回Json对象
- 用法:
JSON.parse(strJson);
var obj = "{ \"name\": \"aye\" }";
console.log(JSON.parse(obj)); //{name:'aye'}
console.log(typeof JSON.parse(obj)); //object
var arr = "[1,2,3]";
console.log(JSON.parse(arr)); //[1,2,3]
console.log(Array.isArray(JSON.parse(arr))); //true
JSON.stringify
- 功能:json对象转字符串,返回json字符串
- 用法:
JSON.stringify(json);
var obj = { name: 'aye' };
console.log(JSON.stringify(obj)); //'{"name":"aye"}'
console.log(typeof JSON.stringify(obj)); //string
toJSON
如果要序列化的对象具有 toJSON() 方法,那么该方法将被调用,以便返回可序列化的值。toJSON() 方法可以在对象中定义,用于自定义对象在序列化过程中的行为。
const obj = {
name: 'aye',
age: 18,
toJSON: function () {
return {
fullName: this.name,
yearsOld: this.age + 1
};
}
};
const jsonString = JSON.stringify(obj);
console.log(jsonString); // 输出: {"fullName":"aye","yearsOld":19}