Hi again
Is there any way to format a string. I.E. I've got a variable which may
be 1, 2, 3 and the variable is added to string. I want to show whole
string formatted as follows:
100
1
10
1100
not like that:
100
1
10
1100
Any help would be helpful (as always )
thanks in advance
···
--
Posted via http://www.ruby-forum.com/.
Please look for printf, sprintf and % in the docs. The last one might
be hard to find. You use it as: "%3d" % 1
Kind regards
robert
···
2007/7/25, Marcin Tyman <m.tyman@interia.pl>:
Hi again
Is there any way to format a string. I.E. I've got a variable which may
be 1, 2, 3 and the variable is added to string. I want to show whole
string formatted as follows:
100
1
10
1100
not like that:
100
1
10
1100
Any help would be helpful (as always )
# Is there any way to format a string. I.E. I've got a variable
# which may be 1, 2, 3 and the variable is added to string. I want
# to show whole string formatted as follows:
# 100
# 1
# 10
# 1100
it would be nice to show original string and it's transformation,
but anyway, is it like this?
irb(main):092:0> "1 20 333 1000 55".split.each{|x| puts x.rjust(4)}
1
20
333
1000
55
this is just using #rjust.
irb(main):093:0> system "qri String#rjust"
----------------------------------------------------------- String#rjust
str.rjust(integer, padstr=' ') => new_str
···
From: Marcin Tyman [mailto:m.tyman@interia.pl]
------------------------------------------------------------------------
If integer is greater than the length of str, returns a new String
of length integer with str right justified and padded with padstr;
otherwise, returns str.
"hello".rjust(4) #=> "hello"
"hello".rjust(20) #=> " hello"
"hello".rjust(20, '1234') #=> "123412341234123hello"
=> true
irb(main):094:0>
robert already gave a handful of other hints.
kind regards -botp
Marcin Tyman wrote:
Robert Klemme wrote:
> Please look for printf, sprintf and % in the docs. The last one might
> be hard to find. You use it as: "%3d" % 1
OK, Fine. But... I want to format the string not its output to redirect
the string to a file on remote machine (the file should be formatted).
sprintf and % return a string, they don't print anything to the screen. You
can write the result of "%3d" % 1 to a file without a problem.
···
--
NP: Katatonia - Fractured
Ist so, weil ist so
Bleibt so, weil war so
Now what? Do you want to output or not? I do not understand your
remark because with the methods I listed you can do output as well as
change strings.
robert
···
2007/7/25, Marcin Tyman <m.tyman@interia.pl>:
Robert Klemme wrote:
> 2007/7/25, Marcin Tyman <m.tyman@interia.pl>:
>> 100
>> 1
>> 10
>> 1100
>>
>> Any help would be helpful (as always )
>
> Please look for printf, sprintf and % in the docs. The last one might
> be hard to find. You use it as: "%3d" % 1
>
> Kind regards
>
> robert
OK, Fine. But... I want to format the string not its output to redirect
the string to a file on remote machine (the file should be formatted).
# OK, Fine. But... I want to format the string not its output
# to redirect the string to a file on remote machine (the file
# should be formatted).
the simple way is just do the output on screen (in a format you want). then if you're happy with the output, just redirect it to a file.
like eg,
ruby mytestprogram.rb > myfile
just try it.
kind regards -botp
···
From: Marcin Tyman [mailto:m.tyman@interia.pl]
Same is true for rjust() : http://www.whytheluckystiff.net/ruby/pickaxe/html/ref_c_string.html#String.rjust
···
On Jul 25, 5:54 am, Sebastian Hungerecker <sep...@googlemail.com> wrote:
sprintf and % return a string, they don't print anything to the screen. You
can write the result of "%3d" % 1 to a file without a problem.