0

I try to detect quotes in a loaded text file but it is not working. I have tried with '"' and '\"' without success. Any suggestion? thanks

 void read()
 {
    txt = File.ReadAllText("txt/txttst");

    for(int i=0;i<txt.Length;i++)
    {
        if(txt[i]=='"')
        {
            Debug.Log("Quotes at "+i);
        }
    }
 }
Paul Suart
  • 6,505
  • 7
  • 44
  • 65
cubecube
  • 1
  • 1
  • What is exception or ? – Erdem Köşk May 25 '17 at 11:57
  • 1
    `"txt/txttst"` is an *odd* file name... something like `@"c:\mydata.txt"` is expected – Dmitry Bychenko May 25 '17 at 11:58
  • There is no exception, it just doesn't output anything. If I type txt = "\"hello"; instead of txt = File.ReadAllText("txt/txttst"); then it works. In Unity3d any non-full path relate to the Asset folder. I can read the file no problem, but I cant detect the quotes. – cubecube May 25 '17 at 12:04
  • Put *break point* on `for`, inspect `txt` value. Is it valid text? Put *break point* on `Debug.Log`. Have you stopped on this break point? – Dmitry Bychenko May 25 '17 at 12:06
  • Are you sure that you file really contains `"`? Also, what encoding is used in that file? If it's something like ISO 8859-7, reading it with default encoding (UTF-16) is not a good idea. @zar-shaikh has shown you how to read file using UTF-8 encoding, you could use his snipped to load file with proper encoding. Also, @dmitry-bychenko has a nice advice - try to inspect the value of `txt` (either by debugging or printing it out to console). – d12frosted May 25 '17 at 12:39

3 Answers3

0

How about this

  string[] lines = File.ReadAllLines(@"txt/txttst");

        for (int i=0;i<lines.Length;i++)
        {
            string line = lines[i];

            // ASCII Code of Quotes is 34

           var bytes = Encoding.UTF8.GetBytes(line.ToCharArray()).ToList();

            if(bytes.Count(b=> b.ToString()=="34")>0)
                Console.WriteLine("\"" + "at line " + (i + 1));

        }
Azar Shaikh
  • 445
  • 9
  • 26
  • I am on mac, maybe it is a mac problem? can anyone try this code line with Unity and let me know if it works on your system? cheers – cubecube May 25 '17 at 12:38
  • - When i read the text in the file out on the console the quotes appear. - One of my friend tried the code on his mac and it worked fine. - Maybe it has to do with the file's encryption then I will try to figure this out. – cubecube May 25 '17 at 14:58
  • `// ASCII Code of Quotes is 34`: I think you mean that, in UTF-8, the code units for a [quotation mark](http://www.fileformat.info/info/unicode/char/0022/index.htm) character are { 34 }—and it's nice that it's only one and that 34 is not included in the code units for any other character—so you can search just for it. – Tom Blodget May 25 '17 at 16:45
0

This is how you can do it, please see the code and screenshot below. Hope it helps.

namespace TestConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string txt = File.ReadAllText(@"C:\Users\Public\TestFolder\test.txt");

            string[] lines = File.ReadAllLines(@"C:\Users\Public\TestFolder\test.txt");
            var reg = new Regex("\"");

            Console.WriteLine("Contents of test.txt are; ");

            foreach (string line in lines)
            {
                Console.WriteLine(line);


                var matches = reg.Matches(line);
                foreach (var item in matches)
                {
                    Console.WriteLine("Quotes at "+ ((System.Text.RegularExpressions.Capture)item).Index);
                }
            }
        }

    }
}

enter image description here

Nurhak Kaya
  • 1,470
  • 1
  • 18
  • 20
0

Ok I found the problem, my text editor did a subtle auto-correct from " to “ . Cheers.

cubecube
  • 1
  • 1