Answered by:
double quote inside a double quote

Question
-
I would like to know how to detect a double quote inside of a double quoted string when I am reading a text file, for example if I have a line likes this
text_line ="Project "Comment" Goes Here"
How can I detect the double quote around the word Comment and how can I delete those two double quotes around the word Comment.
Thank you,
LiangWednesday, January 21, 2009 3:34 AM
Answers
-
Hi, if you are reading from a text file, then the double quotes would have been escaped.
To check if the string has double quotes you can use:
text_line.Contains("\"");
Here \" will escape the double-quote.
To Remove them you could use:
text_line = text_line.Replace("\"", "'"); // Replace double with single quotes.
text_line = text_line.Replace("\"", " "); // Replace double quotes with a space or u could use an empty string as well
- Proposed as answer by David M Morton Wednesday, January 21, 2009 4:46 PM
- Marked as answer by Michael Sun [MSFT]Microsoft employee Wednesday, January 28, 2009 1:27 PM
Wednesday, January 21, 2009 4:12 AM -
Hi,
Following code might help you. Code written on VS2008.string s = File.ReadAllText("data.txt"); string[] arr = s.Split('"'); StringBuilder sb = new StringBuilder(); foreach (string str in arr) { sb.Append(str); } string newStr = sb.ToString();
Asanga.
- Edited by asanga vipulasiri Wednesday, January 21, 2009 4:21 AM Forgot to mentioned the VS version
- Proposed as answer by Harry Zhu Thursday, January 22, 2009 1:40 AM
- Marked as answer by Michael Sun [MSFT]Microsoft employee Wednesday, January 28, 2009 1:27 PM
Wednesday, January 21, 2009 4:18 AM
All replies
-
Hi, if you are reading from a text file, then the double quotes would have been escaped.
To check if the string has double quotes you can use:
text_line.Contains("\"");
Here \" will escape the double-quote.
To Remove them you could use:
text_line = text_line.Replace("\"", "'"); // Replace double with single quotes.
text_line = text_line.Replace("\"", " "); // Replace double quotes with a space or u could use an empty string as well
- Proposed as answer by David M Morton Wednesday, January 21, 2009 4:46 PM
- Marked as answer by Michael Sun [MSFT]Microsoft employee Wednesday, January 28, 2009 1:27 PM
Wednesday, January 21, 2009 4:12 AM -
Hi,
Following code might help you. Code written on VS2008.string s = File.ReadAllText("data.txt"); string[] arr = s.Split('"'); StringBuilder sb = new StringBuilder(); foreach (string str in arr) { sb.Append(str); } string newStr = sb.ToString();
Asanga.
- Edited by asanga vipulasiri Wednesday, January 21, 2009 4:21 AM Forgot to mentioned the VS version
- Proposed as answer by Harry Zhu Thursday, January 22, 2009 1:40 AM
- Marked as answer by Michael Sun [MSFT]Microsoft employee Wednesday, January 28, 2009 1:27 PM
Wednesday, January 21, 2009 4:18 AM