say i have the string “qxq” and i want to repeat the middle char 4 times
i could do this
str = "qxq
newstring = str.gsub(/(x)/, ‘\1’*4)
but if i have a string full of x’s i.e. “xxx”
then it will repeat all of them 4 times and thats not what i want to
do…i just want the middle one to be repeated.
say i have the string “qxq” and i want to repeat the middle char 4 times
i could do this
str = "qxq
newstring = str.gsub(/(x)/, ‘\1’*4)
but if i have a string full of x’s i.e. “xxx”
then it will repeat all of them 4 times and thats not what i want to
do…i just want the middle one to be repeated.
Don’t use a regexp. It isn’t clear what you want to happen if there
isn’t a middle character if the string has a odd integer for its length.
Try using some type of substring approach such as this:
Don’t use a regexp. It isn’t clear what you want to happen if there isn’t a middle character if the string has a odd integer for its length. Try using some type of substring approach such as this: