Loop through a string with while : char in string « String « Ruby
- Ruby
- String
- char in string
Loop through a string with while
s = "hello"
while(s["l"]) # While the string contains the substring "l"
s["l"] = "L"; # Replace first occurrence of "l" with "L"
end # Now we have "heLLo"
Related examples in the same category