Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I add padding spaces to a fix length

Tags:

ruby

I need all strings' length with 5

Original [477, 4770,]

Expected ["477 ", "4770 "]

How could I do it with Ruby ?

like image 687
user3675188 Avatar asked Jun 23 '15 07:06

user3675188


People also ask

How do you pad a string to a fixed length with spaces in Python?

In Python, strings have a built-in method named ljust . The method lets you pad a string with characters up to a certain length. The ljust method means "left-justify"; this makes sense because your string will be to the left after adjustment up to the specified length.

How do you add padding to a string in Java?

Use the String. format() method to pad the string with spaces on left and right, and then replace these spaces with the given character using String. replace() method. For left padding, the syntax to use the String.

How do you get a fixed length input in python?

You just need to use input to get the user input. You can put this inside a while-loop, so it keeps asking until a valid input is given. For the string you use input as well, there you could keep asking if the string given is not the right length, or just truncate the input.

What does Stringutils rightPad do?

The rightPad(final String str, final int size, final char padChar) method. This method is used to pad the given character to the right side of the string. The number of characters padded is equal to the difference of the specified size and the length of the given string.

How to PAD cells to a fixed length in Excel?

To pad cells to a fixed length, you just need a simple formula. Select the cells you want to use, type this formula =LEFT (A1&"*****",5), press Enter key, and drag fill handle over the cells as you need.

How do you add padding to the bottom of a table?

To add padding on table cells, use the CSS padding property: To add padding only above the content, use the padding-top property. And the others sides with the padding-bottom, padding-left , and padding-right properties: Cell spacing is the space between each cell. By default the space is set to 2 pixels.

How do I change the space between cells in a table?

And the others sides with the padding-bottom, padding-left , and padding-right properties: Cell spacing is the space between each cell. By default the space is set to 2 pixels. To change the space between table cells, use the CSS border-spacing property on the table element:

What is the default padding for a table cell?

Cell padding is the space between the cell edges and the cell content. By default the padding is set to 0. To add padding on table cells, use the CSS padding property:


2 Answers

You should use ljust:

arr = [477, 4770]
strings = arr.map { |number| number.to_s.ljust(5) }
# => ["477  ", "4770 "]

Good luck!

like image 78
Paweł Dawczak Avatar answered Sep 22 '22 06:09

Paweł Dawczak


You need the method String#ljust. Here is an example:

t = 123
s = t.to_s.ljust(5, ' ')

Please note that ' ' is the default padding symbol. I only added it for clarity.

like image 36
Ivaylo Strandjev Avatar answered Sep 21 '22 06:09

Ivaylo Strandjev