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

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

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

HTH,
-Mario.


I want to change the world but they won’t give me the source code.

Mario C. wrote:

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

HTH,
-Mario.


I want to change the world but they won’t give me the source code.

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

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

On Sep 9, 10:34 pm, Bigmac T. [email protected] wrote:

string = “hello my name is bigmac”

I’ve seen a bunch of weird names on this list, but you’re the weirdest
so far.

Yossef M. wrote:

On Sep 9, 10:34�pm, Bigmac T. [email protected] wrote:

string = “hello my name is bigmac”

I’ve seen a bunch of weird names on this list, but you’re the weirdest
so far.

i didnt know both names would be displayed…

Bigmac T. wrote:

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

7stud – wrote:

Bigmac T. wrote:

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)

Bigmac T. wrote:

lets say im reading a string from a socket and its super long, i want to

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

Bigmac T. wrote:

7stud – wrote:

Bigmac T. wrote:

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”

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

-Adam

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