Several Methods for operating strings in JavaScript

Source: Internet
Author: User

Copy to ClipboardReference: [www.bkjia.com] <Head>
<Title> connection string </title>
</Head>
<Body>
<Script type = "text/javascript">
Var str1 = "Javascript string connection" + ", method 1 ";
Var str2 = "method 2 ";
Str2 + = "use + = connection ";
Var str3 = "method 3 ";
Str3 + = ", multi-string connection" + ". Use multiple characters to connect" + "at the same time. Correct! ";
Var str4 = "string connection ";
Str4 = str4.concat ("use concat function connec string ");
Window. alert (str1 );
Window. alert (str2); window. alert (str3); window. alert (str4 );
</Script>
</Body>
<Html>

SEARCH strings

Locate the position of a string

Str. indexOf (subStr)

Str indicates the specified string. subStr indicates the substring to be searched.

Search for characters at a specified position

Str. charAt (n) str indicates the string to be searched. n indicates the position where the data type is integer.

Var str1 = "JSstr"; window. alert (str1.charAt (3 ));

Determine the final position of the string

Var n = str. lastindexOf (argStr );

Instance:

Copy to ClipboardReference: [www.bkjia.com] <Head>
<Title> string SEARCH </title>
</Head>
<Body>
<Script type = "text/javascript">
Var str1 = "JSStr ";
Window. alert (str1.lastindexOf ("S "));
Window. alert (str1.lastindexOf ("str "));
Window. alert (str1.indexOf ("S "));
Window. alert (str1.indexOf ("Str "));
</Script>
</Body>
<Html>

Splits a string into arrays.

Var arry = str. split (s );

The parameter s is a delimiter or a separator.

How to Use the function split:

Copy to ClipboardReference: [www.bkjia.com] 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]);
}

Truncates a string.

Var str2 = str1.substring (begin [, end]);

This method is similar to the function of intercepting a string.

Var str2 = str1.slice (begin, [, end]);

Str1 indicates the position from which the bengin string is intercepted by str2. Optional. If the end parameter is not selected, it is truncated to the end 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.