玖叶教程网

前端编程开发入门

javascript内置对象总结 - String

一、属性:

1.1 常用属性:

1.1.1 String.length属性 - 该属性返回字符串中字符编码单元的数量

var x = "Mozilla";
var empty = "";

console.log("Mozilla is " + x.length + " code units long") // 输出:Mozilla is 7 code units long
console.log("The empty string is has a length of " + empty.length) // 输出:The empty string is has a length of 0

二、方法:

2.1 常用方法:

2.1.1 String.concat() - 方法将一个或多个字符串与原字符串连接合并,形成一个新的字符串并返回

语法:

str.concat(str2, [, ...strN])

案例:

let hello = 'Hello, '
console.log(hello.concat('Kevin', '. Have a nice day.')) // 输出:Hello, Kevin. Have a nice day.

let greetList = ['Hello', ' ', 'Venkat', '!']
"".concat(...greetList)  // "Hello Venkat!"
"".concat({})    // [object Object]
"".concat([])    // ""
"".concat(null)  // "null"
"".concat(true)  // "true"
"".concat(4, 5)  // "45"

2.1.2 String.includes() - 方法用于判断一个字符串是否包含在另一个字符串中,根据情况返回 true 或 false

语法:

str.includes(searchString[, position])

案例:

var str = 'To be, or not to be, that is the question.';

console.log(str.includes('To be'));       // true
console.log(str.includes('question'));    // true
console.log(str.includes('nonexistent')); // false
console.log(str.includes('To be', 1));    // false
console.log(str.includes('TO BE'));       // false

2.1.3 String.indexOf() - 方法返回调用它的 String 对象中第一次出现的指定值的索引,从 fromIndex 处进行搜索。如果未找到该值,则返回 -1

语法:

str.indexOf(searchValue [, fromIndex])

案例:

var anyString = "Brave new world";

console.log(anyString.indexOf("w")) // 第一个w位置为8
console.log(anyString.lastIndexOf("w")) // 最后一个w位置为10
console.log(anyString.indexOf("new")) // 第一个new位置为6
console.log(anyString.lastIndexOf("new")) // 最后一个new位置为6

2.1.4 String.match() - 方法检索返回一个字符串匹配正则表达式的结果

语法:

str.match(regexp)

案例:

const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
const regex = /[A-Z]/g;
const found = paragraph.match(regex);

console.log(found) // 输出:Array ["T", "I"]

2.1.5 String.replace() - 方法返回一个由替换值(replacement)替换部分或所有的模式(pattern)匹配项后的新字符串。模式可以是一个字符串或者一个正则表达式,替换值可以是一个字符串或者一个每次匹配都要调用的回调函数

语法:

str.replace(regexp|substr, newSubStr|function)

案例:

const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
console.log(p.replace('dog', 'monkey')) // 输出:The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?

const regex = /Dog/i;
console.log(p.replace(regex, 'ferret')) // 输出:The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?

2.1.6 String.search() - 方法执行正则表达式和 String 对象之间的一个搜索匹配

语法:

str.search(regexp)

案例:

const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';
const regex = /[^\w\s]/g;

console.log(paragraph.search(regex)) // 输出:43
console.log(paragraph[paragraph.search(regex)]) // 输出:"."

2.1.7 String.slice() - 方法提取某个字符串的一部分,并返回一个新的字符串,且不会改动原字符串

语法:

str.slice(beginIndex[, endIndex])

案例:

const str = 'The quick brown fox jumps over the lazy dog.';

console.log(str.slice(31)) // 输出:the lazy dog.

console.log(str.slice(4, 19)) // 输出:"quick brown fox"

console.log(str.slice(-4)) // 输出:"dog."

console.log(str.slice(-9, -5)) // 输出:"lazy"

2.1.8 String.split() - 方法使用指定的分隔符字符串将一个String对象分割成子字符串数组,以一个指定的分割字串来决定每个拆分的位置

语法:

str.split([separator[, limit]])

案例:

const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split(' ');
console.log(words[3]) // 输出:"fox"

const chars = str.split('');
console.log(chars[8]) // 输出:"k"

const strCopy = str.split();
console.log(strCopy) // 输出:Array ["The quick brown fox jumps over the lazy dog."]

2.1.9 String.substr() - 方法返回一个字符串中从指定位置开始到指定字符数的字符

警告: 尽管 String.prototype.substr(…) 没有严格被废弃, 但它被认作是遗留的函数并且可以的话应该避免使用。它并非JavaScript核心语言的一部分,未来将可能会被移除掉。如果可以的话,使用 substring() 替代它

语法:

str.substr(start[, length])

案例:

var str = "abcdefghij";

console.log("(1,2): "    + str.substr(1,2));   // (1,2): bc
console.log("(-3,2): "   + str.substr(-3,2));  // (-3,2): hi
console.log("(-3): "     + str.substr(-3));    // (-3): hij
console.log("(1): "      + str.substr(1));     // (1): bcdefghij
console.log("(-20, 2): " + str.substr(-20,2)); // (-20, 2): ab
console.log("(20, 2): "  + str.substr(20,2));  // (20, 2):

2.1.10 String.substring() - 方法返回一个字符串在开始索引到结束索引之间的一个子集, 或从开始索引直到字符串的末尾的一个子集

语法:

str.substring(indexStart[, indexEnd])

案例:

var anyString = "Mozilla";

// 输出 "Moz"
console.log(anyString.substring(0,3));
console.log(anyString.substring(3,0));
console.log(anyString.substring(3,-3));
console.log(anyString.substring(3,NaN));
console.log(anyString.substring(-2,3));
console.log(anyString.substring(NaN,3));

// 输出 "lla"
console.log(anyString.substring(4,7));
console.log(anyString.substring(7,4));

// 输出 ""
console.log(anyString.substring(4,4));

// 输出 "Mozill"
console.log(anyString.substring(0,6));

// 输出 "Mozilla"
console.log(anyString.substring(0,7));
console.log(anyString.substring(0,10));

2.1.11 String.toLocaleLowerCase() - 方法根据任何指定区域语言环境设置的大小写映射,返回调用字符串被转换为小写的格式

语法:

str.toLocaleLowerCase()
str.toLocaleLowerCase(locale)
str.toLocaleLowerCase([locale, locale, ...])

案例:

'ALPHABET'.toLocaleLowerCase(); // 'alphabet'

'\u0130'.toLocaleLowerCase('tr') === 'i';    // true
'\u0130'.toLocaleLowerCase('en-US') === 'i'; // false

let locales = ['tr', 'TR', 'tr-TR', 'tr-u-co-search', 'tr-x-turkish'];
'\u0130'.toLocaleLowerCase(locales) === 'i'; // true

2.1.12 String.toLocaleUpperCase() - 方法根据本地主机语言环境把字符串转换为大写格式,并返回转换后的字符串

语法:

str.toLocaleUpperCase()
str.toLocaleUpperCase(locale)
str.toLocaleUpperCase([locale, locale, ...])

案例:

'alphabet'.toLocaleUpperCase(); // 'ALPHABET'

'Ges??'.toLocaleUpperCase(); // 'GES?SS'

'i\u0307'.toLocaleUpperCase('lt-LT'); // 'I'

let locales = ['lt', 'LT', 'lt-LT', 'lt-u-co-phonebk', 'lt-x-lietuva'];
'i\u0307'.toLocaleUpperCase(locales); // 'I'

2.1.13 String.toLowerCase() - 会将调用该方法的字符串值转为小写形式,并返回

语法:

str.toLowerCase()

案例:

console.log('中文简体 zh-CN || zh-Hans'.toLowerCase()) // 输出:中文简体 zh-cn || zh-hans
console.log( "ALPHABET".toLowerCase() ) // 输出:alphabet

2.1.14 String.toUpperCase() - 方法将调用该方法的字符串转为大写形式并返回(如果调用该方法的值不是字符串类型会被强制转换)

语法:

str.toUpperCase()

案例:

const sentence = 'The quick brown fox jumps over the lazy dog.';
console.log(sentence.toUpperCase()) // 输出:THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.

2.1.15 String.toString() - 方法返回指定对象的字符串形式

语法:

str.toString()

案例:

var x = new String("Hello world");
console.log(x.toString()) // 输出:"Hello world"

2.1.16 String.trim() - 方法会从一个字符串的两端删除空白字符。在这个上下文中的空白字符是所有的空白字符

语法:

str.trim()

案例:

const greeting = '   Hello world!   ';

console.log(greeting) // 输出: "   Hello world!   "
console.log(greeting.trim()) // 输出:"Hello world!"

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言