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.