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)
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>
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: