字符串:用数字替换空格

时间:2022-02-02 21:44:44

I would like to replace every blank spaces in a string by a fixnum (which is the number of blank spaces).

我想用一个fixnum(即空格的个数)替换字符串中的每个空格。

Let me give an example:

让我举个例子:

s = "hello,   how          are  you ?"
omg(s) # => "hello,3how10are2you1?"

Do you see a way (sexy if possible) to update a string like this?

你有办法(如果可能的话)更新这样的字符串吗?

Thank you Rubists :)

谢谢Rubists:)

2 个解决方案

#1


10  

gsub can be fed a block for the "replace with" param, the result of the block is inserted into place where the match was found. The argument to the block is the matched string. So to implement this we capture as much whitespace as we can ( /\s+/ ) and feed that into the block each time a section is found, returning that string's length, which gets put back where the whitespace was originally found.

gsub可以为“替换为”提供一个块,将块的结果插入到找到匹配的位置。块的参数是匹配的字符串。因此,为了实现这一点,我们捕获尽可能多的空格(/\s+/),并在每次找到一个区段时将其输入到块中,返回该字符串的长度,该字符串被返回到最初发现空格的位置。

Code:

代码:

s = "hello,   how          are  you ?"
res = s.gsub(/\s+/) { |m| m.length }
puts res
# => hello,3how10are2you1?

#2


-1  

it is possible to do this via an array split : Javascript example

可以通过数组拆分:Javascript示例来实现这一点

var s = "hello,   how          are  you ?";

function omg( str ) {
    var strArr = str.split('');
    var count = 0;
    var finalStr = '';
    for( var i = 0; i < strArr.length; i++ ) {
        if( strArr[i] == ' ' ) {
            count++;
        }
        else 
        {
            if( count > 0 ) {
                finalStr += '' + count;
                count = 0;
            }  

            finalStr += strArr[i];
        }
    }
    return finalStr
}

alert( omg( s ) ); //"hello,3how10are2you1?"

Lol, this seems the best it can be for javascript

Lol,这对javascript来说似乎是最好的了

#1


10  

gsub can be fed a block for the "replace with" param, the result of the block is inserted into place where the match was found. The argument to the block is the matched string. So to implement this we capture as much whitespace as we can ( /\s+/ ) and feed that into the block each time a section is found, returning that string's length, which gets put back where the whitespace was originally found.

gsub可以为“替换为”提供一个块,将块的结果插入到找到匹配的位置。块的参数是匹配的字符串。因此,为了实现这一点,我们捕获尽可能多的空格(/\s+/),并在每次找到一个区段时将其输入到块中,返回该字符串的长度,该字符串被返回到最初发现空格的位置。

Code:

代码:

s = "hello,   how          are  you ?"
res = s.gsub(/\s+/) { |m| m.length }
puts res
# => hello,3how10are2you1?

#2


-1  

it is possible to do this via an array split : Javascript example

可以通过数组拆分:Javascript示例来实现这一点

var s = "hello,   how          are  you ?";

function omg( str ) {
    var strArr = str.split('');
    var count = 0;
    var finalStr = '';
    for( var i = 0; i < strArr.length; i++ ) {
        if( strArr[i] == ' ' ) {
            count++;
        }
        else 
        {
            if( count > 0 ) {
                finalStr += '' + count;
                count = 0;
            }  

            finalStr += strArr[i];
        }
    }
    return finalStr
}

alert( omg( s ) ); //"hello,3how10are2you1?"

Lol, this seems the best it can be for javascript

Lol,这对javascript来说似乎是最好的了