String problem

C

Cyril Jose

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?
 
C

Cyril Jose

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!
 
W

WJ

Cyril said:
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"
 
R

Robert Klemme

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,123
Messages
2,570,718
Members
47,284
Latest member
LeroyOlver

Latest Threads

Top