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 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 is an explanation of the JS method used in the example:
1. Join(): This method is used to put all elements in the array into a string. Elements are separated by specified delimiters.
Return value: Returns the string value, which contains all elements of the array connected together, separated by the specified delimiter.
Format: arrayObj.join(separator)
arrayObj Required option, Array object;
separator optional. Specifies the separator 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(): Split a string into a substring array, and then return the result as a string array.
Format: stringObj.split(separator, hovmany)
stringObj Required option, String object or text to be decomposed.
separator optional. A string or regular expression object that identifies whether one or more characters are used when separating a string. If this option is ignored, a single element array containing the entire string is returned.
hovmany optional. This value is used to limit the maximum length of the returned array. If this parameter is set, the returned substrings will not be more than the array specified by this parameter. If this parameter is not set, the entire string will be 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,a,y,?
How are, you
3. reverse(): Returns an Array object whose element order is reversed.
Format: arrayObj.reverse()
arrayObj Required option, 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] = "Thomas"document.write(arr + "<br />")document.write(arr.reverse()) </script>
Output:
George, John, Thomas
Thomas, John, George
4. The charAt() method can return characters at the specified position.
grammar
stringObject.charAt(index)
Index Required. A number representing a position in a string, that is, the subscript of a character in a string
Tips and comments
Comment: The subscript of the first character in the string is 0. If the parameter index is not between 0 and string.length, the method returns an empty string.
Example
In the string "Hello world!" we will return the character in position 1:
<script type="text/javascript">var str="Hello world!"document.write(str.charAt(1)) </script>
The output of the above code is:
e