每篇文章仅做为自己的备忘笔记,若有描述不清或不对的地方还请指明,感谢^_^

js 中 对 String 的操作

// charAt():返回指定位置的字符。
const str = "hello";
const char = str.charAt(1); // "e"

// charCodeAt():返回指定位置字符的Unicode编码。
const str = "hello";
const unicode = str.charCodeAt(1); // 101

// concat():连接两个或多个字符串,并返回新的字符串。
const str1 = "hello";
const str2 = "world";
const newStr = str1.concat(" ", str2); // "hello world"

// includes():判断一个字符串是否包含另一个字符串,返回布尔值。
const str = "hello world";
const included = str.includes("world"); // true

// endsWith():判断一个字符串是否以指定的字符结尾,返回布尔值。
const str = "hello world";
const ended = str.endsWith("world"); // true

// indexOf():返回字符串中指定字符的位置,如果未找到则返回-1。
const str = "hello world";
const index = str.indexOf("world"); // 6

// lastIndexOf():返回字符串中指定字符最后出现的位置,如果未找到则返回-1。
const str = "hello world";
const lastIndex = str.lastIndexOf("l"); // 9

// match():在字符串中查找一个正则表达式匹配的内容,返回数组。
const str = "hello world";
const matched = str.match(/o/g); // ["o", "o"]

// repeat():将字符串重复指定的次数,并返回新的字符串。
const str = "hello";
const repeated = str.repeat(3); // "hellohellohello"

// replace():在字符串中用一个新的子串替换某个子串。
const str = "hello world";
const replaced = str.replace("world", "javascript"); // "hello javascript"

// search():搜索字符串中指定的子串,返回匹配到的位置。
const str = "hello world";
const position = str.search(/wo/); // 6

// slice():提取字符串中的一部分,返回新的字符串。
const str = "hello world";
const sliced = str.slice(6, 11); // "world"

// split():将字符串按指定的分隔符拆分成数组。
const str = "hello world";
const splitted = str.split(" "); // ["hello", "world"]

// startsWith():判断一个字符串是否以指定的字符开头,返回布尔值。
const str = "hello world";
const started = str.startsWith("hello"); // true

// substr():从指定位置开始,截取指定长度的字符串。
const str = "hello world";
const substred = str.substr(6, 5); // "world"

// substring():提取字符串中两个指定的索引号之间的字符。
const str = "hello world";
const substr = str.substring(6, 11); // "world"

// toLowerCase():将字符串中的所有字符转换成小写字母。
const str = "HELLO WORLD";
const lowercased = str.toLowerCase(); // "hello world"

// toUpperCase():将字符串中的所有字符转换成大写字母。
const str = "hello world";
const uppercased = str.toUpperCase(); // "HELLO WORLD"

// trim():去除字符串两端的空格,并返回新的字符串。
const str = "   hello world   ";
const trimmed = str.trim(); // "hello world"


// valueOf():返回指定字符串的原始值。
const str = "hello world";
const value = str.valueOf(); // "hello world"

// toString():将任何类型的值转换成字符串。
const num = 123;
const str = num.toString(); // "123"

// localeCompare():比较两个字符串,并返回一个数字表示它们的顺序关系。
const str1 = "apple";
const str2 = "banana";
const comparison = str1.localeCompare(str2); // -1

// padStart():在字符串的开头插入指定数量的字符。
const str = "world";
const padded = str.padStart(10, "hello "); // "hello world"

// padEnd():在字符串的结尾插入指定数量的字符。
const str = "hello";
const padded = str.padEnd(10, " world"); // "hello world"

// toLocaleLowerCase():将字符串中的所有字符转换成小写字母,根据本地化规则。
const str = "HELLO WORLD";
const lowercased = str.toLocaleLowerCase(); // "hello world"

// toLocaleUpperCase():将字符串中的所有字符转换成大写字母,根据本地化规则。
const str = "hello world";
const uppercased = str.toLocaleUpperCase(); // "HELLO WORLD"

//trimStart():去除字符串开头的空格,并返回新的字符串。
const str = "   hello world";
const trimmed = str.trimStart(); // "hello world"

//trimEnd():去除字符串结尾的空格,并返回新的字符串。
const str = "hello world   ";
const trimmed = str.trimEnd(); // "hello world"

//codePointAt():返回在给定位置的字符的Unicode代码点。
const str = "hello world";
const codePoint = str.codePointAt(1); // 101

//fromCharCode():将Unicode编码转换为字符。
const char = String.fromCharCode(97); // "a"

//fromCodePoint():将Unicode代码点转换为字符。
const char = String.fromCodePoint(9731); // "☃"

//normalize():按指定的 Unicode 正规化形式对当前字符串进行编码。
const str = "héllö wõrld";
const normalized = str.normalize(); // "héllö wõrld"

//substrinig():提取字符串中两个指定的索引号之间的字符。
const str = "hello world";
const substring = str.substring(6, 11); // "world"

// match():在字符串中搜索一个指定的值或者一个正则表达式,并返回匹配的结果。
const str = "hello world";
const matched = str.match("world"); // ["world"]

// replace():使用指定的值或者正则表达式替换字符串中的一个子串,并返回新的字符串。
const str = "hello world";
const replaced = str.replace("world", "universe"); // "hello universe"

// search():在字符串中搜索指定的值或者正则表达式,并返回第一个匹配的位置。
const str = "hello world";
const position = str.search("world"); // 6

// slice():提取字符串中的一部分,并返回新的字符串。
const str = "hello world";
const sliced = str.slice(6, 11); // "world"

// split():将字符串拆分成一个数组,通过指定的分隔符来确定每个数组元素。
const str = "hello,world";
const splitted = str.split(","); // ["hello", "world"]

// startsWith():检查字符串是否以指定的值开始。
const str = "hello world";
const startsWithHello = str.startsWith("hello"); // true

// endsWith():检查字符串是否以指定的值结束。
const str = "hello world";
const endsWithWorld = str.endsWith("world"); // true

// toLowerCase():将字符串转换为小写,并返回新的字符串。
const str = "Hello World";
const lowerCase = str.toLowerCase(); // "hello world"

// toUpperCase():将字符串转换为大写,并返回新的字符串。
const str = "Hello World";
const upperCase = str.toUpperCase(); // "HELLO WORLD"

// toString():将一个数值转换为字符串。
const num = 42;
const str = num.toString(); // "42"

// valueOf():返回一个字符串对象的原始值。
const strObj = new String("Hello World");
const value = strObj.valueOf(); // "Hello World"

 

posted @ 2023-04-23 13:05  菜汤不甜  阅读(49)  评论(0编辑  收藏  举报