The native method of the javascript operation string, the javascript string

Source: Internet
Author: User

The native method of the javascript operation string, the javascript string

Join ([separator]) array elements are combined into strings

ToString () represents an array as a string

Reverse () array -- change the original array itself

ValueOf () returns the array value

Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = "text/javascript">
Var fruits = ["apple", "", "orange"]; // we recommend that you define and initialize arrays in this way.
With (document ){
Writeln ("<ul> ");
Writeln ("<li>" + fruits. join () + "</li> ");
Writeln ("<li>" + fruits. join ("--") + "</li> ");
Writeln ("<li>" + fruits. reverse (). join () + "</li> ");
Writeln ("<li>" + fruits. valueOf () + "</li> ");
Writeln ("</ul> ");
}
</Script>
</Head>
</Html>

ToUpperCase () -- change to uppercase

CharAt (index) -- indicates a character.

Substring (begin, len) -- truncates a string

Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = "text/javascript">
Var str = "javascript ";
Var num = 1234;
With (document ){
Writeln (str. toUpperCase () + "<br> ");
Writeln (num. toString (). charAt (3) + "<br> ");
Writeln (str. substring (0, 4) + "<br> ");
}
</Script>
</Head>
</Html>

Running result:

Copy codeThe Code is as follows:
JAVASCRIPT
3
Java

IndexOf -- determines whether a character exists in a string

Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = "text/javascript">
Function isEmail (){
Var emailValue = document. getElementsByName ("email") [0]. value;
If (emailValue. indexOf ("@") =-1 ){
Alert ("enter the correct email ");
} Else {
Alert ("OK ");
}
}
</Script>
</Head>
<Body>
<Input type = "text" name = "email">
<Input type = "button" value = "check" onclick = "isEmail ();">
</Body>
</Html>

Are you familiar with the native methods used to operate strings in javascript? Many times, these native methods can easily implement the functions we need.

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.