js intercepts a character after a string

js string how to implement the interception function slice (), substring (), substr ()

In js character interception function there are commonly used three slice (), substring (), substr (), here I come to give you an introduction to slice (), substring (), substr () function in the character interception of some of the uses and differences. The three functions of the string: slice(), substring() and substr()

Related Properties:

slice()

The first parameter represents the start position, the second parameter represents the end position, the next position of the interceptor. The length of the intercepted string is the difference between the second parameter and the first parameter; if the parameter value is negative, the value will be added to the length of the string and turned into a positive value; if the first parameter is equal to or greater than the second parameter, then the empty string is returned…

substring()

The first parameter represents the start position, the second parameter represents the end position of the next position; if the value of the parameter is negative, the value will be converted to 0; of the two parameters, take the smaller value of the start position, the length of the intercepted string is the difference between the larger value and the smaller value.

substr()

The first parameter represents the start position, the second parameter represents the length of the intercept

PS: strings are counted from 0

Example:

<scripttype=”text/javascript”>

varstmp=”rcinn.cn”;

Use one parameter

alert(stmp.slice(3));//starting from the 4th character, intercept to the last character; return “nn.cn”

alert(stmp.string(3));//starting from the 4th character, intercept to the last character; return “nn.cn”

alert(stmp.string(3));//starting from the 4th character, intercept to the last character. to the last character; return “nn.cn”

//Use two parameters

alert(stmp.slice(1,5))//start from the 2nd character, to the 5th character; return “cinn”

alert(stmp.substring(1 ,5));//start at 2nd character, go to 5th character; return “cinn”

///If only one parameter is used and it’s 0 then return the whole parameter

alert(stmp.slice(0));//return the whole string

alert(stmp.substring(0));//return the whole string

alert(stmp.substring(0));//return the whole string

alert(stmp.substring(0))

alert(stmp.substring(0))

alert(stmp.substring(0))

alert(stmp. substring(0));//returns the whole string

//returns the first character

alert(stmp.slice(0,1));//returns “r”

alert(stmp. >

// In the above example we can see that the usage of slice() and substring() is the same

// The return value is also the same, but when the parameter is a negative number, their return value is different, look at the following example

alert(stmp.slice(2,-5));//return ” i”

alert(stmp.substring(2,-5));//returns “rc”

// From the above two examples, you can see that slice(2,-5) is actually slice(2,3)

/// Negative 5 plus a string length of 8 converts it to a positive 3 (if the first digit is equal to or greater than the second digit, then the empty character is returned). number, then return the empty string);

// And substring(2,-5) is actually substring(2,0), negative numbers are converted to 0, substring always takes the smaller number as the starting position.

alert(stmp.substring(1,5))//from the 2nd character, to the 5th character; return “cinn”

alert(stmp.substr(1,5));//from the 2nd character, to the 5th character; return “cinn.”

</ script>difference between substr and substring methods

<scripttype=”text/javascript”>

varstr=”0123456789″;//

alert(str.substring( 0));//————“0123456789”

alert(str.substring(5));//————“56789”

alert(str.substring(10));//———–“”< /p>

alert(str.substring(12));//———–“”

alert(str.substring(-5));//———–“0123456789”

alert(str.substring( -10));//———-“0123456789”

alert(str.substring(-12));//———-“0123456789”

alert(str.substring(0,5));//——– — “01234”

alert(str.substring(0,10));//———“0123456789”

alert(str.substring(0,12));//———“0123456789”

alert(str.substring(2,0));//———-“01”

alert(str.substring(2,2));//———-“”

alert(str.substring(2,5));//—— —- “234”

alert(str.substring(2,12));//———“23456789”

alert(str.substring(2,-2));//———“01”

alert(str. substring(-1,5));//———“01234”

alert(str.substring(-1,-5));//——–“”

alert(str.substr(0));//—————” 0123456789″

alert(str.substr(5));//—————“56789”

alert(str.substr(10));//————–“”

alert(str. substr(12));//————–“”

alert(str.substr(-5));//————–“0123456789”

alert(str.substr(-10));//———– — “0123456789”

alert(str.substr(-12));//————-“0123456789”

alert(str.substr(0,5));//————-“01234”

alert(str.substr(0,10));//————“0123456789”

alert(str.substr(0,12));//————“0123456789”

alert(str.substr (2,0));//————-“”

alert(str.substr(2,2));//————-“23”

alert(str.substr(2,5));//————-“23456”

< p>alert(str.substr(2,12));//————“23456789”

alert(str.substr(2,-2));//————“”

alert(str.substr(-1,5));// ———— “01234”

alert(str.substr(-1,-5));//———–“”

</script> Function: split()

Function: Uses a specified delimiter to split and store a string into an array

Example:

str=”jpg|bmp|gif|ico|png”;

arr=theString.split(“|”);//arr is a string containing the characters “jpg”, “bmp”, “gif”, “ico” and “png”

Function: John()

Function: Combines an array into a string using a delimiter of your choice

Example:

vardelimitedString=myArray.join( delimiter);

varmyList=newArray(“jpg”, “bmp”, “gif”, “ico”, “png”);

varportableList=myList.join(“|”);

< p>// The result is jpg|bmp|gif|ico|png function: indexOf()

Function: returns the subscript of the first character in the string that matches the substring

varmyString=”JavaScript”;

varw=myString.indexOf(“v”);wwillbe2

varx=myString.indexOf(“S”);xwillbe4

< p>vary=myString.indexOf(“Script”);ywillalsobe4

varz=myString.indexOf(“key”); zwillbe-1 saw another very simple method online with the following code:

functionfunc(s,n){

returns.replace(/([^x00-xff])/g,”$1a”).slice (0,n).replace(/([^x00-xff])a/g,”$1″);

}This is very clever, and largely correct. I say “basically” because when it takes the substring of length 6 to the left of the “123 Hanzi test”, it returns “123 Hanzi” instead of 123Kanji” instead of “123Kanji”. Of course, this is not necessarily a problem, and in some cases the requirements may be such. This method could be improved as follows:

functionfunc(s,n){

returns.slice(0,n).replace(/([^x00-xff])/g,”$1a”). n).replace(/([^x00-xff])a/g,”$1″);

}

js intercepts all strings after / preceded by the specified character

letstr=’Zhang San>Li Si>Wang Wu’

Intercepts the last > preceded by the string

letindex=str. lastIndexOf(“>”)

str=str.substring(0,index);

console.log(str)//Zhang San>Li Si

Intercepts the string that comes after the last >

letindex=str.lastIndexOf(“>”)

str=str.substring(index+1,str.length);

console.log(str)//Wang Wu

js intercepts the character following a given character

Get the string before/after the specified character:

<scripttype=”text/javascript”>

/*

str specified character;

split(str)[0], reads the value with index 0 (the first value) in the array, all array indexes start at 0 by default;

*/

functiongetStr(string,str){

varstr_before=string.split(str)[0];

varstr_after=string.split(str)[1];

alert(‘before:’+str_before+’-after:’+str_after);

}

</script>

<inputtype=”button” onClick=”getStr(‘string’,’r’); “value=”Get Value”/>

Get the characters at the specified position in the string:

&lt. html>

<head>

<title>Get the character at the specified position in the string-Liehuo. ><scriptlanguage=”javascript”>

<! —

str=”JavaScript”;

str1=str.charAt(0);

str2=str.charAt(4);

document.write(“str=”+str+”<br>”);

< p>document.write(“The 1st character is: “+str1+”<br>”);

document.write(“The 5th character is: “+str2+”<br>”);

//–>

</script >

</body>

</html>