晴明的博客园 GitHub      CodePen      CodeWars     

[js] string

String.prototype.substr()
 str.substr(start[, length])

substr() 方法返回字符串中从指定位置开始到指定长度的子字符串。

substr()不改变原字符串。
start 只识别起始位置,不传length,默认从start位置到结束,负数则倒序位置开始到结束。
length>0,意为取多少长度,不够还剩多少取多少。

#

                str = 'Hello World';//11
                console.log(str.substr(0));//Hello World
                console.log(str);//Hello World
                console.log(str.substr(1));//ello World
                console.log(str);//Hello World
                console.log(str.substr(-1));//d
                console.log(str);//Hello World
                console.log(str.substr(-2));//ld
                console.log(str.substr(0,7));//Hello W
                console.log(str.substr(1,7));//ello Wo
                console.log(str.substr(-1,5));//d
                console.log(str.substr(-7,5));//o Wor
                console.log(str.substr(1,-2));//empty
                console.log(str.substr(0,0));//empty
                console.log(str.substr(11));//empty
                console.log(str.substr(-14));//error            

 

String.prototype.substring()

str.substring(indexStart[, indexEnd])

substring() 返回字符串两个索引之间(或到字符串末尾)的子串。

substring()不改变原字符串。
start>=0 只识别起始位置,不传end,默认从start位置到结束,负数则倒序开始。
end>=0  只识别结束位置,截取的字符串不包括结束位置。

#

                str = 'Hello World';//11
                console.log(str.substring(0));//Hello World
                console.log(str);//Hello World
                console.log(str.substring(1));//ello World
                console.log(str);//Hello World
                console.log(str.substring(-1));//Hello World  任一参数小于 0 或为 NaN,则被当作 0
                console.log(str);//Hello World
                console.log(str.substring(-2));//Hello World  任一参数小于 0 或为 NaN,则被当作 0
                console.log(str.substring(0,7));//Hello W
                console.log(str.substring(1,7));//ello Wo
                console.log(str.substring(-1,5));//Hello 任一参数小于 0 或为 NaN,则被当作 0
                console.log(str.substring(-7,5));//Hello 任一参数小于 0 或为 NaN,则被当作 0
                console.log(str.substring(1,-2));//H       任一参数小于 0 或为 NaN,则被当作 0
                console.log(str.substring(1,1));//empty
                console.log(str.substring(1,100));//ello World 任一参数大于 stringName.length,则被当作 stringName.length。
                console.log(str.substring(5,2));//llo  start>stop 两者位置对调
                console.log(str.substring(2,5));//llo

 

String.prototype.split()

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

split() 方法通过把字符串分割成子字符串来把一个 String 对象分割成一个字符串数组。

#

            function replaceString(oldS, newS, fullS) {
                //console.log(fullS.split(oldS));
                //console.log(fullS.split(oldS).join(newS));
                return fullS.split(oldS).join(newS);
            }
            console.log(replaceString("World", "Web", "Brave New World"));
            //Brave New Web
            
            var names = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand ";
            console.log(names);
            var re = /\s*;\s*/;
            var nameList = names.split(re);
            console.log(nameList);
            //["Harry Trump", "Fred Barney", "Helen Rigby", "Bill Abel", "Chris Hand "]
            
            var myString = "Hello World. How are you doing?";
            var splits = myString.split(" ", 3); //只3个
            console.log(splits);
            //["Hello", "World.", "How"]
            
            var myString = "Hello 1 word. Sentence number 2.";
            var splits = myString.split(/(\d)/);//数字将会保留
            console.log(splits);
            //["Hello ", "1", " word. Sentence number ", "2", "."]

 

String.prototype.indexOf()
str.indexOf(searchValue[, fromIndex])
indexOf() 方法返回指定值在字符串对象中首次出现的位置。从 fromIndex 位置开始查找,如果不存在,则返回 -1。

#

"Blue Whale".indexOf("Blue");     // returns  0
"Blue Whale".indexOf("Blute");    // returns -1
"Blue Whale".indexOf("Whale", 0); // returns  5
"Blue Whale".indexOf("Whale", 5); // returns  5
"Blue Whale".indexOf("", 9);      // returns  9
"Blue Whale".indexOf("", 10);     // returns 10
"Blue Whale".indexOf("", 11);     // returns 10

"Blue Whale".indexOf("Blue") !== -1; // true
"Blue Whale".indexOf("Bloe") !== -1; // false

 

#统计一个字符串中某个字母出现的次数

var str = 'To be, or not to be, that is the question.';
var count = 0;
var pos = str.indexOf('e');
while (pos !== -1) {
  count++;
  pos = str.indexOf('e', pos + 1);
}
console.log(count); // displays 4

 

#

posted @ 2016-04-05 18:54  晴明桑  阅读(144)  评论(0编辑  收藏  举报