Home Web Front-end JS Tutorial How to implement inverted string in javascript

How to implement inverted string in javascript

Mar 15, 2018 pm 04:40 PM
javascript js string

This article mainly shares with you the method of inverting strings in javascript. I hope it can help you.

Javascript method of inverting a string

Method 1:

1

2

3

4

5

6

7

var str = "abcdefg";                //常规方法使用for循环加charAt

                function reverse(str){

                    var result ="";                    for(var i = str.length; i > 0; i-- ){

                        console.log(str.charAt(i-1));

                        result +=  str.charAt(i-1);

                    }                    return result;

                }

Copy after login

Output result:

How to implement inverted string in javascript

Method 2: Cleverly use the reverse and join methods of array

1

2

3

4

5

//巧妙使用array的reverse和join方法

                function reverse(str){

                    var result, arr = str.split("");    //使用空字符则会直接分割字符返回数组[a,b,c,d,e,f,g]

                    var result = arr.reverse().join("");                    return result; 

                }

Copy after login

Output results:

How to implement inverted string in javascript

Sometimes during the project When you encounter some better methods, it is good to record them. For example, if the js native domList wants to use the array method, you can also do this, so that you can avoid excessive use of for loops

1

2

3

4

5

6

7

8

var op = document.querySelectorAll("select option");

                var arr = Array.prototype.slice.call(op,1);

                arr.forEach(function(item, index, arr){

                    item.style.backgroundColor = "red";

                    console.log(index);

                    console.log(item);

                    console.log(arr);

                });

Copy after login

​​​

Related recommendations:

Summary of JavaScript string methods

The above is the detailed content of How to implement inverted string in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Recommended: Excellent JS open source face detection and recognition project

Detailed explanation of the method of converting int type to string in PHP Detailed explanation of the method of converting int type to string in PHP Mar 26, 2024 am 11:45 AM

Detailed explanation of the method of converting int type to string in PHP

How to determine whether a Golang string ends with a specified character How to determine whether a Golang string ends with a specified character Mar 12, 2024 pm 04:48 PM

How to determine whether a Golang string ends with a specified character

How to check if a string starts with a specific character in Golang? How to check if a string starts with a specific character in Golang? Mar 12, 2024 pm 09:42 PM

How to check if a string starts with a specific character in Golang?

How to repeat a string in python_python repeating string tutorial How to repeat a string in python_python repeating string tutorial Apr 02, 2024 pm 03:58 PM

How to repeat a string in python_python repeating string tutorial

How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP Mar 04, 2024 am 09:36 AM

How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP

PHP string manipulation: a practical way to effectively remove spaces PHP string manipulation: a practical way to effectively remove spaces Mar 24, 2024 am 11:45 AM

PHP string manipulation: a practical way to effectively remove spaces

PHP String Matching Tips: Avoid Ambiguous Included Expressions PHP String Matching Tips: Avoid Ambiguous Included Expressions Feb 29, 2024 am 08:06 AM

PHP String Matching Tips: Avoid Ambiguous Included Expressions

See all articles