- Home /
The question is answered, right answer was accepted
Replace() with string and Regex not replacing numbers/integers
Hey, there! I have a bunch of variables that I need to change to different names.
Currently they're ordered and stored in a scriptable object 'data' and held in my character objects as vars[].
I'm replacing all of the .cs file instances of vars[i] with the name held in data[i].
My code abstract is...
foreach(string assetPath in AssetDatabase.GetAllAssetPaths())
for(int v = 0; v < data.Count; v++)
string findText = "vars[" + i.tostring + "]";
string replaceText = data[i].name;
string finalFile = File.ReadAllText(assetPath);
finalFile = finalFile.Replace(findText, replaceText);
File.WriteAllText(finalFile)
It finds all the instances of vars[i] in each file perfectly fine (the actual code finds all instances of vars[i] in the file first), but when I try to replace them with string.Replace or Regex.Replace it doesn't work. When I change replace text to something without numbers it seems to work. What am I doing wrong?
Answer by CAS_Kicks · Aug 26, 2017 at 03:57 PM
Turns out it was my fault. I went over the code tons of times before posting this, but the way my loops were nested things weren't being saved and re-read properly. Anyways... Nothing to see here.
Answer by tanoshimi · Aug 24, 2017 at 06:37 AM
Do you really need to do this in Unity? I'd always do any Find and Replace operations through an IDE - it's trivial to do a Regex operation on all the files in a directory using Visual Studio, for example.
I'm open to anything. What would that look like? I wasn't sure how I could do it while iterating through an array of variables (400+ in this case)