I have a string that is in an array. I am trying to find all the ~
tilde chars in a string and replace them with "
double quote. This is a conversion, and I had to replace all the "
with ~
to make the parser work.
Here is my code:
for (int i = 0; i < columnCount; i++)
{
row[i] = Regex.Replace(row[i], "~", """);
}
The string is coming across as:
~~Generator~~
The result I am looking for would be:
""Generator""
This line of code:
row[i] = Regex.Replace(row[i], "~", """);
is returning the following result:
"""GENERATOR"""
I have also tried this line of code as well:
row[i] = row[i].Replace("~", """);
This gives me the result of:
"""GENERATOR"""
I have tried to look up other answers, and none of them really fit what I was trying to do.
5
This is how it is shown in debugger (with escape character ):
but if you would print the character, it would show as you would expect:
By the way, you can simplify your code to:
columnCount = columnCount.Select(x => x.Replace("~", """)).ToArray();
0
The issue you’re encountering arises because in C# the backslash is an escape character. This means that when you include
"
in a string, it represents a literal double quote "
character.
string[] row = { "~~Generator~~" };
for(int i=0;i<row.Length; i++)
{
row[i] = row[i].Replace("~", """);
}
Console.WriteLine(row[0]);
Sumanta Swain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.