I have a string as below.
string data = "A := B;\nC:=D";
The string has to be replaced with SET statement as below.
data = "SET A=B;\nSET C=D"
It should replace := with =and insert aSET` statement.
I came out with an algorithm as below but it’s not working when I have multiple :=.
Is there any other easiest and efficient way to handle this? Perhaps using RegEx?
private string AddSETStatement(string script)
{
script = script.Replace(": =", ":=");
script = script.Replace(":=", ":= ");
script = script.Replace(" :=", ":= ");
int found = script.IndexOf(":=");
while (found > -1)
{
int foundspace = -1;
for (int index = found; index >= 0; index--)
{
string temp = script.Substring(index, 1);
if (temp == " ")
{
foundspace = index;
break;
}
}
script = script.Remove(found, 1);
if (foundspace == -1)
{
script = "SET " + script;
}
else
{
script = script.Insert(foundspace, " SET ");
}
found = script.IndexOf(":=");
}
return script.Trim();
}
Any help would be appreciated.
I’ve tested this and I think this is the command that matches your requirement as you’ve coded your algorithm (which can be replaced with one line of code):
Just in case you actually use more than one space between and around := you can use this instead:
This turns this:
Into this:
And one more which handles variable names that are bother upper and lower case and contain numbers:
Turns this:
Into:
At least one those or a combination of those should be able to do the work for you.