I have a string:
str="D\\projects\\myown\\java"
I assigned this string to two variables like:
str1=str
str2=str
After I did the below operation:
idgb1=str1.gsub!("\\","_")
I get str1 as D_projects_myown_java and str2 is the same. why does this happen? I don’t want str2 to change its value.
str1andstr2are just references tostr, the string is not copied to those variables. AndString#gsub!updates the string in place.If you want to keep
str2intact, you need to do it like this:or
Besides, if you use
String#gsubinstead ofString#gsub!,strandstr2will not be changed.