From: steve Date: 2009-09-10T14:50:06+09:00 Subject: Re: string.each Bigmac Turdsplash wrote: > 7stud -- wrote: >> Bigmac Turdsplash 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) 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