Several common methods of JavaScript string manipulation _javascript Tips

Source: Internet
Author: User
Tags truncated

Connection string

<ptml> <pead> <title> Connection string </title> </pead> <body> <script type= "Text/javascri PT "> var str1=" javascript string Connection "+", method one; var str2= "Method Two"; str2+= "Use + + connection"; var str3= "method three"; str3+= ", multiple string concatenation" + ". Use multiple characters simultaneously to connect" + ", correct! "; var str4= "string concatenation"; Str4=str4.concat ("Use concat function Connec string"); Window.alert (STR1); Window.alert (STR2); Window.alert (STR3); Window.alert (STR4); </script> </body> <ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Search string
Find the position of a string
Str.indexof (SUBSTR)
STR represents the specified string substr the substring to search for
Find the specified position character
Using Str.charat (n) str to represent the string n to be searched represents the position of the data type as the integral type represents the ordinal
var str1= "Jsstr"; Window.alert (Str1.charat (3));
Determines the last position in a string that appears
var n=str.lastindexof (ARGSTR);
Instance
<ptml> <pead> <title> string search </title> </pead> <body> <script type= "Text/javascri PT "> var str1=" Jsstr "; Alert (Str1.lastindexof ("S")); Alert (Str1.lastindexof ("str")); Alert (Str1.indexof ("S")); Alert (Str1.indexof ("Str")); </script> </body> <ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Splits a string into groups
var arry=str.split (s);
Where the parameter s is a separator or a split string
function split () use method
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]);
}
Intercepts a string of strings
var str2=str1.substring (Begin[,end]);
Intercepting a part of a string this method is similar to a string function that intercepts the strings.
var str2=str1.slice (Begin,[,end]);
STR1 represents the truncated string str2 the truncated string argument bengin from which one to start intercepting the end parameter optionally, if you do not select the ending argument, it is truncated to the ends of the string

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.