string.each[1,2,3,4,5]

B

Bigmac Turdsplash

this is making me mad, i cant figure out how to brake a string down into
individual strings

string = "hello my name is bigmac"

string.each[1,2,3,4,5,6]

puts string[1] #'hello'
puts string[2] #'my'
puts string[3] #'name'
puts string[4] #'is'
puts string[5] #'bigmac'
puts string[6] #nil
 
M

Mario Camou

[Note: parts of this message were removed to make it a legal post.]

irb(main):001:0> "hello my name is bigmac".split
=> ["hello", "my", "name", "is", "bigmac"]

HTH,
-Mario.
 
B

Bigmac Turdsplash

Mario said:
irb(main):001:0> "hello my name is bigmac".split
=> ["hello", "my", "name", "is", "bigmac"]

HTH,
-Mario.

o man, i tried that like 50 times, the problem was my sockets
 
B

Bigmac Turdsplash

after i split the string, how can i put the string back into its
original form?
 
7

7stud --

Bigmac said:
after i split the string, how can i put the string back into its
original form?

str = ["hello", "my", "name", "is", "bigmac"].join(" ")
puts str

--output:--
hello my name is bigmac
 
B

Bigmac Turdsplash

7stud said:
Bigmac said:
after i split the string, how can i put the string back into its
original form?

str = ["hello", "my", "name", "is", "bigmac"].join(" ")
puts str

--output:--
hello my name is bigmac

lets say im reading a string from a socket and its super long, i want to
trim the first word from the string then do something with the rest of
the string...

str = "disaster a bunch of junk. 98rh98rh98ujvi".split

puts str[1] # idetify the first word,

puts str[2 threw X].join(" ") # takes the rest of the string and prints
to screen in its original form...


but how do i know what X is? (until end of string)
 
7

7stud --

Bigmac said:
7stud said:
Bigmac said:
after i split the string, how can i put the string back into its
original form?

str = ["hello", "my", "name", "is", "bigmac"].join(" ")
puts str

--output:--
hello my name is bigmac

lets say im reading a string from a socket and its super long, i want to
trim the first word from the string then do something with the rest of
the string...

str = "disaster a bunch of junk. 98rh98rh98ujvi".split

puts str[1] # idetify the first word,

puts str[2 threw X].join(" ") # takes the rest of the string and prints
to screen in its original form...


but how do i know what X is? (until end of string)

X = -1

arr = [10, 20, 30, 40]
p arr[1..-1]

--output:--
[20, 30, 40]

But instead of splitting a super long string and then re-joining it, you
can just split off the first word like this:

str = "a really long string"

first_word, the_rest = str.split(" ", 2)

p first_word
p the_rest

--output:--
"a"
"really long string"
 
S

steve

Bigmac said:
7stud said:
Bigmac said:
after i split the string, how can i put the string back into its
original form?
str = ["hello", "my", "name", "is", "bigmac"].join(" ")
puts str

--output:--
hello my name is bigmac

lets say im reading a string from a socket and its super long, i want to
trim the first word from the string then do something with the rest of
the string...

str = "disaster a bunch of junk. 98rh98rh98ujvi".split

puts str[1] # idetify the first word,

puts str[2 threw X].join(" ") # takes the rest of the string and prints
to screen in its original form...


but how do i know what X is? (until end of string)

Look at the RDoc for the Array class
use str[0] to get the first element
use str[1,-1] to get the second thru last elements, or more explicitly
str.slice(1,-1)


You might also like to use the optional second operand to the
String#split method

irb(main):003:0> f,r="disaster a bunch of junk.
98rh98rh98ujvi".split(/\s+/,2)
=> ["disaster", "a bunch of junk. 98rh98rh98ujvi"]
irb(main):004:0> puts f
disaster
=> nil
irb(main):005:0> puts r
a bunch of junk. 98rh98rh98ujvi
=> nil
irb(main):006:0>

Regards

Steve
 
A

Adam Salter

[Note: parts of this message were removed to make it a legal post.]

Did not know that.
... and I already thought ruby was awesome...

-Adam
 

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

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top