how to find double code caracter in string

Marco Dell'Oca 41 Reputation points
2020-12-05T13:50:17.777+00:00

I need to find the position of the double quotation mark (") character in a text that contains several.
I read the text with this command:
StreamReader fileRead = File.OpenText (@filepath)

How can I do?
Thanks in advance

Marco Dell'Oca

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,457 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,286 Reputation points
    2020-12-05T15:03:30.287+00:00

    Hello @Marco Dell'Oca ,

    This is one idea to loop through lines in the file (assuming you are reading lines in a file)

        var fileName = "TextFile1.txt";  
      
        if (!File.Exists(fileName))  
        {  
            return;  
        }  
      
        var counter = 0;  
      
        string line;  
      
        using (var file = new StreamReader("TextFile1.txt"))  
        {  
            while ((line = file.ReadLine()) != null)  
            {  
      
                counter++;  
      
                if (!line.Contains("\"")) continue;  
                var result = line  
                    .Select((value, index) => new { item = value, position = index })  
                    .Where(item => item.item == '\"')  
                    .ToList();  
      
                if (result.Count <= 0) continue;  
                {  
                    Console.WriteLine($"{line}");  
                    foreach (var item in result)  
                    {  
                        Console.WriteLine(item.position);  
                    }  
                }  
            }  
             
        }  
    

    Text file with sample data

    Karen Payne  
    Jim Jones  
    Bob "Adams Frank"  
    Bill Smith  
    "Jane anne"  
    

    Output

    Bob "Adams Frank"  
    4  
    16  
    "Jane anne"  
    0  
    10  
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Marco Dell'Oca 41 Reputation points
    2020-12-06T10:00:33.883+00:00

    Many many thanks Karen,

    that's exactly what I was looking for.

    Thanks again

    Marco Dell'Oca

    0 comments No comments