Home > database >  How to make that all fragments are retrieved by index?
How to make that all fragments are retrieved by index?

Time:06-08

Good afternoon! I need to extract text from a specific word to a specific character. Here is the code

while (!streamReader.EndOfStream)  
{
    string lines = streamReader.ReadToEnd();
    StringBuilder builder = new StringBuilder();
       
    int index1 = lines.IndexOf("color");
    int index = lines.IndexOf("}");
    if (index != -1) 
       builder.AppendLine(lines.Substring(index1, index));
       
    textBox1.Text = builder   "";               
}

The fact is that there are several such passages in the text. I need to extract all such passages from a word to a character. But it extracts only the first passage. How can I extract all passages?

CodePudding user response:

You can split lines by color as a delimiter and then find index of } from each split string,

string[] splitLines = lines.Split("color");
foreach(var splitLine in splitLines)
{
    int index = splitLine.IndexOf("}");
    if(index != -1)
      builder.AppendLine($"color{splitLine.Substring(0, index)}");
}
 textBox1.Text = builder.ToString()   "";
  •  Tags:  
  • c#
  • Related