简体   繁体   中英

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM