Home  >  Article  >  Web Front-end  >  Several common methods of JavaScript string manipulation_javascript skills

Several common methods of JavaScript string manipulation_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:41:011018browse

Connection string


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

Search string
Find the position of the string in the string
Str.indexOf(subStr)
Str represents the specified string subStr represents the substring to be searched
Find the character at the specified position
Use str.charAt(n ) str represents the string to be searched n represents the data type is an integer and represents the position
var str1="JSstr"; window.alert(str1.charAt(3));
Make sure the string is in The last position in the string
var n=str.lastindexOf(argStr);
Instance

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]

Split a string into Array
var arry=str.split(s);
The parameter s is the separator or split string
How to use the function split()
var str1="Tom";
str1 =",Jerry,Jhon,Sqm,qqqq";
var strArry=new Arry();
strArry=str1.split(",");
for(var i=0;i< strAry.length;i )
{
Window.alert(strAry[i]);
}
Intercept a string of strings
var str2=str1.substring(begin[, end]);
This method of intercepting a part of a string has the same function as intercepting a string of a string var str2=str1.slice(begin,[,end]); str1 represents the intercepted The string parameter bengin after the string str2 is intercepted is from which bit the end parameter is optional. If the end parameter is not selected, it means intercepting to the end of the string
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn