Ruby: Insert Spaces Every X Number of Characters

Ruby: Insert spaces every X number of characters

>> s = "1234567812345678123456781234567812345678"
=> "1234567812345678123456781234567812345678"
>> s.gsub(/(.{8})/, '\1 ')
=> "12345678 12345678 12345678 12345678 12345678 "

Edit: You could use positive lookahead to avoid adding an extra space at the end:

>> s.gsub(/(.{8})(?=.)/, '\1 \2')
=> "12345678 12345678 12345678 12345678 12345678"

Insert Something Every X Number of Characters Without Regex

class String
def in_groups_of(n)
chars.each_slice(n).map(&:join).join(' ')
end
end

'12345678123456781234567812345678'.in_groups_of(8)
# => '12345678 12345678 12345678 12345678'

Insert Something Every X Number of Characters Without Regex

class String
def in_groups_of(n)
chars.each_slice(n).map(&:join).join(' ')
end
end

'12345678123456781234567812345678'.in_groups_of(8)
# => '12345678 12345678 12345678 12345678'

How to insert tag every 5 characters in a Ruby String?

s = 'HelloWorld-Hello guys'
s.scan(/.{5}|.+/).join("<wbr>")

Explanation:

Scan groups all matches of the regexp into an array. The .{5} matches any 5 characters. If there are characters left at the end of the string, they will be matched by the .+. Join the array with your string

How do I add a space after x amount of digits?

Use '\& ' as replacement string. (\& represents the matched string)

'02920555555'.gsub(/\d{4}/, '\& ')
# => "0292 0555 555"

UPDATE

to add a space after the 5th number only, use sub instead of gsub:

'02920555555'.sub(/\d{5}/, '\& ')
# => "02920 555555"

or using gsub with the pattern ^... (matches only at the beginning of the input string):

'02920555555'.gsub(/^\d{5}/, '\& ')
# => "02920 555555"

How do I add spaces to the end of my string?

The integer to ljust() must be larger than the length of the string, or nothing will be appended. Since line is six chars, I believe you want:

line = "abcdef"
line = line.ljust(10, " ")

That'll add four spaces after the six characters already present in the string.

You could likely also do something along the lines of:

line = line.ljust(line.length + 4, " ")

How do I add characters between each character in a string in ruby?

There are many solutions to this, I would suggest the following :

1/ split the string into an array of individual characters with chars

"hello".chars
=> ["h", "e", "l", "l", "o"]

2/ join them with the two characters you want to add in-between each character

["h", "e", "l", "l", "o"].join('--')
=> "h--e--l--l--o"

You can execute this in one line as such :

"hello".chars.join('--')

Insert space before some character if space does not exist

>> "_abc_de _e _f_tes_fefe".gsub(/(\S)_/,'\1 _')
=> "_abc _de _e _f _tes _fefe"

Inserting a space in between characters using gsub - Ruby

In order to reference a match you should use \n where n is the match, not $1.

s = "I have 36 dogs in 54 of my houses in 24 countries"
s.gsub(/(\d)(\d)/, '\1 \2')
# => "I have 3 6 dogs in 5 4 of my houses in 2 4 countries"

How to force Ruby string to n characters

Ruby supports using format strings, like many other languages:


[11] (pry) main: 0> '%3.3s' % 'f'
=> " f"
[12] (pry) main: 0> '%3.3s' % 'foo'
=> "foo"
[13] (pry) main: 0> '%3.3s' % 'foobar'
=> "foo"

If you want to pad on the right, use a - in the format string:


[14] (pry) main: 0> '%-3.3s' % 'f'
=> "f "
[15] (pry) main: 0> '%-3.3s' % 'foo'
=> "foo"
[16] (pry) main: 0> '%-3.3s' % 'foobar'
=> "foo"

You could also use printf or sprintf, but I prefer the more generic %, which is like sprintf.

From the sprintf docs:


s | Argument is a string to be substituted. If the format
| sequence contains a precision, at most that many characters
| will be copied.

and:


- | all | Left-justify the result of this conversion.

What if I want to pad not with spaces but with some other character?

The underlying Ruby code is written in C, and it's hard-coded to use ' ' as the pad character, so that's not possible directly, unless you want to modify the source, recompile your Ruby, and live with a one-off version of the language. Instead, you can do something like:


[17] (pry) main: 0> ('%-3.3s' % 'f').gsub(' ', '.')
=> "f.."

This gets a lot more complicated than a simple gsub though. Often it's better to adjust the string without using format strings if you need to supply special characters. Format strings are very powerful but they're not totally flexible.

Otherwise, I'd go with something like @steenslag's solution, which is the basis for rolling your own. You lose the ability to use format strings and have to rely on concatenating your strings or something similar to get columnar output, but it will also work.



Related Topics



Leave a reply



Submit