Native method of JavaScript manipulation Strings _ Basics

Source: Internet
Author: User

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

ToString () represents an array as a string

Reverse () array inversion--change the original array itself

ValueOf () returns an array value

Copy Code code as follows:

<script type= "Text/javascript" >
var fruits= ["Apple", "pear", "orange"];//recommend this to define, initialize an array
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>

toUpperCase ()--capitalization

CharAt (Index)--refers to a character

SUBSTRING (begin,len)--Truncate string

Copy Code code as follows:

<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>

Run Result:

Copy Code code as follows:

Javascript
3
Java

indexof--determine if a character exists in the string

Copy Code code as follows:

<script type= "Text/javascript" >
function Isemail () {
var emailvalue=document.getelementsbyname ("email") [0].value;
if (Emailvalue.indexof ("@") ==-1) {
Alert ("Please enter the correct email");
}else{
Alert ("OK");
}
}
</script>
<body>
<input type= "text" name= "email" >
<input type= "button" value= "Check" onclick= "Isemail ();" >
</body>

The small partners are aware of the native methods of manipulating strings in JavaScript, and many times these native methods can simply implement the functionality 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.