Jordan Trainor
Jordan Trainor

Reputation: 2438

replacing the beginning and end but not the same chars within a string

lines = "some stuff\"some other \"stuff\"\"";
lines = lines.Replace("\"", "\"");
lines = lines.Replace("\"", "\"");

in its current context and in its simplest form these two actions seem absolutely pointless but when I put this into code it will be not be pointless and will have a purpose other than replacing itself with itself.

OK so I have the String lines that has 4 escaped quotation marks and I wish to replace the first quote with a quote and the end quote with a quote how would I accomplish this without replacing any of the inner quotes?

Upvotes: 0

Views: 89

Answers (1)

Jonathan Graef
Jonathan Graef

Reputation: 391

Use IndexOf and LastIndexOf to find the first and last quotes. Then use Substring to replace the quotes:

lines = "some stuff\"some other \"stuff\"\"";
firstQuote = lines.IndexOf("\"");
lastQuote = lines.LastIndexOf("\"");
lines = lines.Substring(0, firstQuote) + "\"" + lines.Substring(firstQuote + 1, lastQuote) + "\"" + lines.Substring(lastQuote + 1, lines.Length);

Upvotes: 2

Related Questions