How do you reverse a string in place in JavaScript?

前端 未结 30 2336
猫巷女王i
猫巷女王i 2020-11-22 00:29

How do you reverse a string in place (or in-place) in JavaScript when it is passed to a function with a return statement, without using built-in functions (.reverse()<

相关标签:
30条回答
  • 2020-11-22 00:31
    var str = 'sample string';
    [].map.call(str, function(x) {
      return x;
    }).reverse().join('');
    

    OR

    var str = 'sample string';
    console.log(str.split('').reverse().join(''));
    

    // Output: 'gnirts elpmas'

    0 讨论(0)
  • 2020-11-22 00:33

    In ECMAScript 6, you can reverse a string even faster without using .split('') split method, with the spread operator like so:

    var str = [...'racecar'].reverse().join('');
    
    0 讨论(0)
  • 2020-11-22 00:33
    //es6
    //array.from
    const reverseString = (string) => Array.from(string).reduce((a, e) => e + a);
    //split
    const reverseString = (string) => string.split('').reduce((a, e) => e + a); 
    
    //split problem
    "                                                                    
    0 讨论(0)
  • 2020-11-22 00:35

    Detailed analysis and ten different ways to reverse a string and their performance details.

    http://eddmann.com/posts/ten-ways-to-reverse-a-string-in-javascript/

    Perfomance of these implementations:

    Best performing implementation(s) per browser

    • Chrome 15 - Implemations 1 and 6
    • Firefox 7 - Implementation 6
    • IE 9 - Implementation 4
    • Opera 12 - Implementation 9

    Here are those implementations:

    Implementation 1:

    function reverse(s) {
      var o = '';
      for (var i = s.length - 1; i >= 0; i--)
        o += s[i];
      return o;
    }
    

    Implementation 2:

    function reverse(s) {
      var o = [];
      for (var i = s.length - 1, j = 0; i >= 0; i--, j++)
        o[j] = s[i];
      return o.join('');
    }
    

    Implementation 3:

    function reverse(s) {
      var o = [];
      for (var i = 0, len = s.length; i <= len; i++)
        o.push(s.charAt(len - i));
      return o.join('');
    }
    

    Implementation 4:

    function reverse(s) {
      return s.split('').reverse().join('');
    }
    

    Implementation 5:

    function reverse(s) {
      var i = s.length,
          o = '';
      while (i > 0) {
        o += s.substring(i - 1, i);
        i--;
      }
      return o;
    }
    

    Implementation 6:

    function reverse(s) {
      for (var i = s.length - 1, o = ''; i >= 0; o += s[i--]) { }
      return o;
    }
    

    Implementation 7:

    function reverse(s) {
      return (s === '') ? '' : reverse(s.substr(1)) + s.charAt(0);
    }
    

    Implementation 8:

    function reverse(s) {
      function rev(s, len, o) {
        return (len === 0) ? o : rev(s, --len, (o += s[len]));
      };
      return rev(s, s.length, '');
    }
    

    Implementation 9:

    function reverse(s) {
      s = s.split('');
      var len = s.length,
          halfIndex = Math.floor(len / 2) - 1,
          tmp;
    
    
         for (var i = 0; i <= halfIndex; i++) {
            tmp = s[len - i - 1];
            s[len - i - 1] = s[i];
            s[i] = tmp;
          }
          return s.join('');
        }
    

    Implementation 10

    function reverse(s) {
      if (s.length < 2)
        return s;
      var halfIndex = Math.ceil(s.length / 2);
      return reverse(s.substr(halfIndex)) +
             reverse(s.substr(0, halfIndex));
    }
    
    0 讨论(0)
  • 2020-11-22 00:37
    var str = "my name is saurabh ";
    var empStr='',finalString='';
    var chunk=[];
    function reverse(str){
    var i,j=0,n=str.length;
        for(i=0;i<n;++i){
            if(str[i]===' '){
                chunk[j]=empStr;
                empStr = '';
                j++;
            }else{
                empStr=empStr+str[i];
            }
        }
        for(var z=chunk.length-1;z>=0;z--){
            finalString = finalString +' '+ chunk[z];
            console.log(finalString);
        }
        return true;
    }
    reverse(str);
    
    0 讨论(0)
  • 2020-11-22 00:39
    function reverseString(string) {
        var reversedString = "";
        var stringLength = string.length - 1;
        for (var i = stringLength; i >= 0; i--) {
            reversedString += string[i];
        }
        return reversedString;
    }
    
    0 讨论(0)
提交回复
热议问题