fized size record from a string

Discussion in 'Ruby' started by lucac81, Mar 10, 2009.

  1. lucac81

    lucac81 Guest

    Hello, I'm new to Ruby, and I'm learning it with a project that also
    involves rails...
    I'm stuck with an apparently simple problem, I receive from a form a
    String and I need to extend it to a fixed char number (it must be 16
    chars with eventual trailing spaces)
    I've tought to convert it to an array, it's easy but I don't know how
    to extend it to the needed size.
    Also I could add spaces to the string, but the again how can i control
    how many I need to add?
    Any hint on how I could do that?
    Thank you very much
     
    lucac81, Mar 10, 2009
    #1
    1. Advertising

  2. lucac81

    Tim Hunter Guest

    lucac81 wrote:
    > Hello, I'm new to Ruby, and I'm learning it with a project that also
    > involves rails...
    > I'm stuck with an apparently simple problem, I receive from a form a
    > String and I need to extend it to a fixed char number (it must be 16
    > chars with eventual trailing spaces)


    Look at the String#% method:

    irb(main):003:0> "%-16s" % "123"
    => "123 "

    This won't work if the original string is longer than 16, though. In
    that case you just a copy of the original string.
    --
    Posted via http://www.ruby-forum.com/.
     
    Tim Hunter, Mar 10, 2009
    #2
    1. Advertising

  3. lucac81

    dkmd_nielsen Guest

    On Mar 10, 8:51 am, lucac81 <> wrote:
    > Hello, I'm new to Ruby, and I'm learning it with a project that also
    > involves rails...
    > I'm stuck with an apparently simple problem, I receive from a form a
    > String and I need to extend it to a fixed char number (it must be 16
    > chars with eventual trailing spaces)
    > I've tought to convert it to an array, it's easy but I don't know how
    > to extend it to the needed size.
    > Also I could add spaces to the string, but the again how can i control
    > how many I need to add?
    > Any hint on how I could do that?
    > Thank you very much


    Simple, not very intelligent way could be

    ('string' + '-'*30)[0..16]
     
    dkmd_nielsen, Mar 10, 2009
    #3
  4. lucac81

    lucac81 Guest

    On Mar 10, 2:51 pm, lucac81 <> wrote:
    > Hello, I'm new to Ruby, and I'm learning it with a project that also
    > involves rails...
    > I'm stuck with an apparently simple problem, I receive from a form a
    > String and I need to extend it to a fixed char number (it must be 16
    > chars with eventual trailing spaces)
    > I've tought to convert it to an array, it's easy but I don't know how
    > to extend it to the needed size.
    > Also I could add spaces to the string, but the again how can i control
    > how many I need to add?
    > Any hint on how I could do that?
    > Thank you very much


    Uhmm I just found how to achieve this... using the ljust method of the
    String class...

    str.ljust(integer, padstr=' ') => new_str

    If integer is greater than the length of str, returns a new String of
    length integer with str left justified and padded with padstr;
    otherwise, returns str.

    "hello".ljust(4) #=> "hello"
    "hello".ljust(20) #=> "hello "
    "hello".ljust(20, '1234') #=> "hello123412341234123"

    this solves my problem, and there is the rjust method that works fine
    on the other side (I need that too)
     
    lucac81, Mar 10, 2009
    #4
  5. On Mar 10, 2009, at 9:53 AM, lucac81 wrote:

    > Hello, I'm new to Ruby, and I'm learning it with a project that also
    > involves rails...
    > I'm stuck with an apparently simple problem, I receive from a form a
    > String and I need to extend it to a fixed char number (it must be 16
    > chars with eventual trailing spaces)
    > I've tought to convert it to an array, it's easy but I don't know how
    > to extend it to the needed size.
    > Also I could add spaces to the string, but the again how can i control
    > how many I need to add?
    > Any hint on how I could do that?
    > Thank you very much
    >


    irb> str = "too short"
    => "too short"
    irb> lng = "this one is big enough"
    => "this one is big enough"

    With Kernel#sprintf (which String#% calls)
    irb> "%-16.16s"%str
    => "too short "
    irb> "%-16.16s"%lng
    => "this one is big "

    With String#ljust
    irb> str.ljust(16)
    => "too short "
    irb> lng.ljust(16)
    => "this one is big enough"

    With String#[] (not what you want for short ones)
    irb> str[0,16]
    => "too short"
    irb> lng[0,16]
    => "this one is big "

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
     
    Rob Biedenharn, Mar 10, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SnVzdGlu?=

    Retrieving Record Key while creating the record.

    =?Utf-8?B?SnVzdGlu?=, Oct 4, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    666
    =?Utf-8?B?SnVzdGlu?=
    Oct 5, 2004
  2. André
    Replies:
    0
    Views:
    3,784
    André
    Jun 25, 2006
  3. MRW
    Replies:
    0
    Views:
    400
  4. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,742
    Michael Jung
    May 25, 2008
  5. THurkmans
    Replies:
    2
    Views:
    624
Loading...

Share This Page