1

I can only get the position of the first space " " but not the other three using index.

text = "Lorem ipsum dolor sit amet"
puts text.index(" ")

Is there any way to get the position of the other three spaces? Should I use any other string method?

Edit: I ended up solving it myself.

i = 0
while i < text.length do
    if text[i] == " "
        puts i
    end
    i += 1
end

I looped through the string until it found a space, and then it displayed the variable i, which in this case is equal to the positioning of the spaces in the string.

sawa
  • 165,429
  • 45
  • 277
  • 381
Oscar Q.
  • 65
  • 8

0 Answers0