Home  >  Article  >  Web Front-end  >  A string reversal function can realize string reverse order_javascript skills

A string reversal function can realize string reverse order_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:36:222042browse

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>

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 JS methods used in the examples are explained below:

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 a string value that contains all the elements of the array concatenated together, with the elements separated by the specified delimiter.

Format: arrayObj.join(separator)

arrayObj required, Array object;

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

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 an array of substrings, and then return the result as a string array.

Format: stringObj.split(separator, hovmany)

stringObj Required, the String object or literal to be decomposed.

separator Optional. A string or regular expression object that identifies whether one or more characters are used to separate the string. If this option is omitted, 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, no more substrings will be returned 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&#63;"

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 with the order of elements reversed.

Format: arrayObj.reverse()

arrayObj Required, Array object.

This method will change 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.charAt() method can return the character at the specified position.

Grammar

stringObject.charAt(index)

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

Tips and Notes

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

Example

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

<script type="text/javascript">
var str="Hello world!"
document.write(str.charAt(1)) 
</script>

The output of the above code is:

e

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn