Write a string inversion function to implement reverse string order.

Source: Internet
Author: User

Method 1:

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

Method 2:

<Input type = "textfield" id = "input"/>
<Div id = "result"> </div>
<Input type = "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 describes the JS methods used in the example:

1. Join (): This method is used to put all elements in the array into a string. Elements are separated by the specified delimiter.

Return Value: returns the string value, which contains all elements of the connected array. The elements are separated by the specified separator.

Format: arrayobj. Join (separator)

Arrayobj is an array object;

Optional. Specifies the delimiter to use. If this parameter is omitted, a comma is used as the separator.

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 a sub-String Array and returns the result as a string array.

Format: stringobj. Split (separator, hovator)

Stringobj is a required String object or text to be decomposed.

Separator is optional. A string or regular expression object that identifies whether a string is separated by one or more characters. If this option is ignored, a single array of elements containing the entire string is returned.

Hovquota is optional. This value is used to limit the maximum length of the returned array. If this parameter is set, no more substrings are returned than the array specified by this parameter. If this parameter is not set, the entire string is split, regardless of its length.

<SCRIPT type = "text/JavaScript">

VaR STR = "How are you 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,, Y ,?

How, are, you

3. Reverse (): returns an array object whose element order is reversed.

Format: arrayobj. Reverse ()

Arrayobj is an array object.

This method will change the original array instead of creating a new array.

<SCRIPT type = "text/JavaScript">

VaR arr = new array (3)

Arr [0] = "George"

Arr [1] = "John" arr [2] = "Thomas"

Document. Write (ARR + "<br/> ")

Document. Write (ARR. Reverse ())

</SCRIPT>

Output:

George, John, Thomas

Thomas, John, George

4. The charat () method returns characters at the specified position.

Syntax

Stringobject. charat (INDEX)

Index is required. Represents the number at a certain position in the string, that is, the subscript of the character in the string

Tips and comments

Note: The subscript of the first character in the string is 0. If the index parameter is not between 0 and string. length, this method returns an empty string.

Instance

In the string "Hello world! ", We will return the character at location 1:

<SCRIPT type = "text/JavaScript">

VaR STR = "Hello world! "

Document. Write (Str. charat (1 ))

</SCRIPT>

The output of the above Code is:

E

Write a string inversion function to implement reverse string order.

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.