js string processing - contains a string and the string interception

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/cxh6863/article/details/99711281

1, within the string contains a string --indexOf ()

the indexOf () method returns the location of a specified string value in the first occurrence of the string, if a string specified does not exist in the original string, it returns -1 exists return the specified string position of the first character first appeared in the original first string.
Usage examples Example:

var oldString="tongyuwan";
var newString1=oldString.indexOf('tong');
var newString2=oldString.indexOf('yu');
var newString3=oldString.indexOf('Wan');

结果如下:
newString1=0
newString2=4
newString3=-1  //因为indexOf的用法是对大小写敏感的

Analyzing contains a string of characters, including a small example
var = oldString "tongyuwan";
var = oldString.indexOf newString ( 'Tong');
if newString equals -1, which does not include the representative "tong" string
if (of newString = -!. 1) {
// oldString comprising representatives of "Tong"
}

2, string interception --substring ()

the substring (start, stop)
which is used to intercept between the specified character index, and returns a character string comprising at the start, but not at a stop character include
examples of usage examples:

var oldString="tongyuwan";
var newString1=oldString.substring(4,6);
var newString2=oldString.substring(4);
var newString3=oldString.substring(4,4);
var newString4=oldString.substring(6,4);

结果如下:
newString1=“yu”
newString2="yuwan"
newString3=""
newString4="yu"//因为start比stop大,它会先交换4和6的位置,然后再截取

3, string interception --substr ()

substr (start, length)
which is used by the interception from the start, how many bits of string interception
usage illustrative examples:

var oldString="tongyuwan";
var newString=oldString.substr(4,5);

结果如下:
newString=“yuwan”

Substr substring and the difference is, substring is taken by someone to the string, the string is taken substr fixed length.

Guess you like

Origin blog.csdn.net/cxh6863/article/details/99711281