Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3253 > unrolled thread

String problem

Started byCyril Jose <cyril_jose@ymail.com>
First post2011-04-20 11:48 -0500
Last post2011-04-22 19:38 +0000
Articles 6 — 4 participants

Back to article view | Back to comp.lang.ruby


Contents

  String problem Cyril Jose <cyril_jose@ymail.com> - 2011-04-20 11:48 -0500
    Re: String problem Hans Mackowiak <hanmac@gmx.de> - 2011-04-20 11:52 -0500
      Re: String problem Cyril Jose <cyril_jose@ymail.com> - 2011-04-20 11:54 -0500
        Re: String problem Robert Klemme <shortcutter@googlemail.com> - 2011-04-23 00:04 +0200
    Re: String problem Cyril Jose <cyril_jose@ymail.com> - 2011-04-21 20:36 -0500
    Re: String problem "WJ" <w_a_x_man@yahoo.com> - 2011-04-22 19:38 +0000

#3253 — String problem

FromCyril Jose <cyril_jose@ymail.com>
Date2011-04-20 11:48 -0500
SubjectString problem
Message-ID<b6b1c8892065126a3980ffcb280fcd37@ruby-forum.com>
Hello all,

I am trying to read characters in a string but I want to ignore the
first character and the newline at the end of the string, then store the
characters in between into a new variable:

str = ">Hello World\n"
new_var = " Hello World"

Any ideas to point me in the right direction?

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [next] | [standalone]


#3254

FromHans Mackowiak <hanmac@gmx.de>
Date2011-04-20 11:52 -0500
Message-ID<47e7c58ad0690ad17efbca10bcbafbf6@ruby-forum.com>
In reply to#3253
str = ">Hello World\n"
str[1..-2] #=> "Hello World"

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#3255

FromCyril Jose <cyril_jose@ymail.com>
Date2011-04-20 11:54 -0500
Message-ID<21ab86409f391aab83c7c7b490d2c713@ruby-forum.com>
In reply to#3254
Hans Mackowiak wrote in post #994076:
> str = ">Hello World\n"
> str[1..-2] #=> "Hello World"

Forgot to mention that I have a bunch of these types of strings of all 
different lengths...thanks for the quick reply however!

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#3392

FromRobert Klemme <shortcutter@googlemail.com>
Date2011-04-23 00:04 +0200
Message-ID<91ecb8F9dsU1@mid.individual.net>
In reply to#3255
On 20.04.2011 18:54, Cyril Jose wrote:
> Hans Mackowiak wrote in post #994076:
>> str = ">Hello World\n"
>> str[1..-2] #=>  "Hello World"
>
> Forgot to mention that I have a bunch of these types of strings of all
> different lengths...thanks for the quick reply however!

Hans's suggestion does work with different length strings

irb(main):001:0> 5.times {|i| s="a"+("b"*i)+"x";puts s, s[1..-2]}
ax

abx
b
abbx
bb
abbbx
bbb
abbbbx
bbbb
=> 5

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [next] | [standalone]


#3349

FromCyril Jose <cyril_jose@ymail.com>
Date2011-04-21 20:36 -0500
Message-ID<3296db8c50a22995561b25cfe5e82e48@ruby-forum.com>
In reply to#3253
Thanks guys. Found what I was looking for.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#3389

From"WJ" <w_a_x_man@yahoo.com>
Date2011-04-22 19:38 +0000
Message-ID<ioslfn01to3@enews5.newsguy.com>
In reply to#3253
Cyril Jose wrote:

> Hello all,
> 
> I am trying to read characters in a string but I want to ignore the
> first character and the newline at the end of the string, then store the
> characters in between into a new variable:
> 
> str = ">Hello World\n"
> new_var = " Hello World"
> 
> Any ideas to point me in the right direction?

">Hello World\n"[ /.(.*)/, 1]
    ==>"Hello World"
">Hello World\n".rstrip[ 1 .. -1]
    ==>"Hello World"

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web