A string inversion function can implement string reverse _javascript technique

Source: Internet
Author: User

The first method:

<script type= "Text/javascript" >
var str= "abcdeg";
function Demo (str) {
var str2= "";
for (Var i=0;i<str.length;i++) {
str2+=str.charat (str.length-i-1);
}

document.write (str+ "<br/>" +str2)
}
demo (str);
</script>

The second method:

<input type= "TextField" id= "input"/> <div "result
" ></div>
id= <input "button" value= "Reverse" onclick= "reverse ()"/> <script language= 
"javascript" >
function reverse ()
{
var Str=document.getelementbyid ("Input"). Value;
var a=str.split (');
var result=new Array ();
while (A.length)
{
Result.push (A.pop ());
}
document.getElementById ("Result"). Innerhtml=result.join (');
}

</script>

The following is a description of the JS method used in the example:

1, join (): This method is used to put all the elements in the array into a string. The elements are delimited by the specified delimiter.

Return value: Returns a String value that contains all the elements of an array that is connected together, separated by the specified delimiter.

Format: Arrayobj.join (separator)

Arrayobj must option, Array object;

Separator optional. Specifies the separator character to use. If this argument is omitted, commas are used as delimiters.

var arr = new Array (3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas" 

document.write (Arr.join ("."))

Output:
George.John.Thomas

Note: Array.join () is equivalent to array.tostring ()

2. Split (): Splits a string into an array of substrings and returns the result as an array of strings.

Format: Stringobj.split (separator, Hovmany)

Stringobj required, String object or text to be exploded.

Separator can be selected. A string or regular expression object that identifies whether one or more characters are used to delimit a string. If this option is omitted, an array of single elements containing the entire string is returned.

Hovmany can be selected. This value is used to limit the maximum length of the returned array. If this argument is set, the returned substring will not be more than the array specified by this parameter. If this argument is not set, the entire string is split, regardless of its length.

<script type= "Text/javascript" >

var str= "How are your doing today?"

document.write (Str.split ("") + "<br/>")

document.write (Str.split ("") + "<br/>")

document.write (Str.split ("", 3))

</script>

Output:

How,are,you,doing,today?

H,o,w, A,r,e, Y,o,u, D,o,i,n,g, T,o,d,a,y,?

How,are,you

3, reverse (): Returns an Array object in which the order of elements is reversed.

Format: Arrayobj.reverse ()

Arrayobj required, Array object.

This method changes the original array without creating a new array.

<script type= "Text/javascript" >

var arr = new Array (3)

arr[0] = "George"

arr[1] = "John" arr[2] = "Thoma S "

document.write (arr +" <br/> ")

document.write (Arr.reverse ()) 

</script>

Output:

George,john,thomas

Thomas,john,george

The 4.charAt () method returns the character at the specified position.

Grammar

Stringobject.charat (Index)

Index required. A number that represents a position in a string, that is, the subscript of a character in a string

Tips and comments

Note: The subscript for the first character in the string is 0. If the argument index is not between 0 and String.Length, the method returns an empty string.

Instance

In the string "Hello world!", we will return the character of position 1:

<script type= "Text/javascript" >
var str= "Hello world!"
document.write (Str.charat (1)) 
</script>

The output of the above code is:

E

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.