Suppose I have an array of string like following:
str_arr=["10$", "10$ I am a student","10$Good","10$ Nice weekend!"]
I would like to re-organize the array element value in the way that, in each element of the array , if there is(are) white space(s) after 10$ sign, then, combine the 10$ with the following word.
That’s generating a new array like following:
str_arr=["10$", "10$I am a student","10$Good","10$Nice weekend!"]
What I tried to do is like following:
str_arra.map{|elem|
# not sure how to do here,
#split and check then combine again?
if elem.size>1
words=elem.split()
if words[0]=='10$'
#not sure how to do here
end
elsif elem.size==1
elem
end
}
but not sure how to generate the new array … and the code above seems verbose…
P.S. it is possible that there are multiple white-spaces after 10$, then comes a word
If you only have those cases, the following should do the trick:
Here’s an example: http://codepad.org/XHeo7E8B