JavaScript String的基本方法
length 是string的 property
slice(), indexOf() , toLowerCase(), toUpperCase() , replace()是方法methods
slice(), indexOf() , toLowerCase(), toUpperCase() , replace()是方法methods
- There are three instances of strings that need to be replaced. You may repeat the replace() method multiple times, or you can use regular expressions. For instance,
let text = 'I am the biggest lover, I love my love'; text.replace(/love/g,'like');
will replace all instances of 'love' to 'like'. Remember, Strings are immutable! im 不 + mut 改变,交换 + able 可…的,能…的 → 不变的
let browserType = "Mozilla" browserType.length
so for example to retrieve the first letter you'd do this:
Remember: computers count from 0, not 1!
browserType[0];
browserType[browserType.length-1];
browserType.indexOf('zilla');
browserType.indexOf('vanilla');
if(browserType.indexOf('mozilla') === -1) { // do stuff with the string if the 'mozilla' // substring is NOT contained within it } if(browserType.indexOf('mozilla') !== -1) { // do stuff with the string if the 'mozilla' // substring IS contained within it }
browserType.slice(0,3);
This returns "moz" — the first parameter is the character position to start extracting at, and the second parameter is the character position after the last one to be extracted.
Instead, you only need to include the character position from where you want to extract the remaining characters in a string. Try the following:
Instead, you only need to include the character position from where you want to extract the remaining characters in a string. Try the following:
browserType.slice(2);
大小写
let radData = "My name Is MuD" radData.toLowerCase(); radData.toUpperCase();
browserType.replace('moz','van'); browserType = browserType.replace('moz','van');
the parameters of the string methods don't have to be string literals; they can also be variables, or even variables with a method being invoked on them.
字符串方法的参数不必是字符串文字;它们也可以是变量,甚至可以是对其调用方法的变量。
阅读量: 489
发布于:
修改于:
发布于:
修改于: