locked
double quote inside a double quote RRS feed

  • 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,
    Liang
    Wednesday, 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
    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.


    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
    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.


    Wednesday, January 21, 2009 4:18 AM