A string inversion function can realize the reverse order of strings

  • 2020-03-30 03:55:04
  • OfStack

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 a description of the JS method used in the example:

1. Join () : this method is used to put all the elements in an array into a string. The element is separated by the specified separator.

Return value: returns a string value that contains all the elements of the concatenated array separated by the specified delimiter.

Format: arrayObj. Join (separator)

ArrayObj Array object;

The separator is optional. Specifies the separator to use. If the parameter is omitted, use a comma 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 an array of substrings and return the result as an array of strings.

Format: stringobj. split(separator, hovmany)

StringObj must option, String object or text to be decomposed.

Separator is optional. A string or regular expression object that identifies whether one or more characters are used to separate strings. If you ignore this option, return an array of single elements containing the entire string.

Hovmany 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 the & # 63;

, H, o, w, a, r, e, a, y, o, u, d, o, I, n, g, t, o, d, a, y, & # 63;

How, are, and you

3. Reverse () : returns an Array object whose order of elements has been reversed.

Format: arrayObj. Reverse ()

ArrayObj mandatory, Array object.

This method changes the original array instead of creating a new one.


<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 a character at the specified location.

grammar

StringObject. CharAt (index)

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

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

The 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


Related articles: