String problem

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?

str = “>Hello World\n”
str[1…-2] #=> “Hello World”

Hans M. 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!

On Thu, Apr 21, 2011 at 01:48:55AM +0900, Cyril J. wrote:

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?

There are probably a dozen ways to do that, but how you do it probably
depends on what, overall, you really want to accomplish. Examples, with
slightly different results:

str.slice!(0) && str.slice!(-1)

new_var = str.gsub(/^./, ' ').chomp

The former gives you what you described, but not what you showed in your
example. The latter gives you exactly what you what your example
suggests. Do you actually want that space at the beginning of the new
string?

The short version of pointing you in the right direction is “I would
suggest reading about the methods in the String class.”

http://ruby-doc.org/core/classes/String.html

On Thu, Apr 21, 2011 at 01:54:27AM +0900, Cyril J. wrote:

Hans M. 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!

That shouldn’t be affected by the length of the string.

Thanks guys. Found what I was looking for.

On Thu, Apr 21, 2011 at 01:52:00AM +0900, Hans M. wrote:

str = “>Hello World\n”
str[1…-2] #=> “Hello World”

Okay, yeah . . . that’s probably better than either of the two options
that immediately occurred to me.

Cyril J. 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”

On 20.04.2011 18:54, Cyril J. wrote:

Hans M. 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs