0

yet another string/substring topic:

I'm trying to write a function that looks for the substrings "id" and "imagePath" in a string that's generated from a very long text file. The characters between the 2 substrings should be written in a rich text box. I cannot see what I'm doing wrong, but the loop never ends and always prints only the first occurrence of said characters, resulting in a million lines that are identical being shown in the text box. There are at least 10 occurrences of both substrings in my file, and my code is below:

private void textToFind ( string file, string catalogType)
        {
            richTextBox1.Clear();
            catalogType.ToUpper();
            string contentOfFile = File.ReadAllText(file);
            contentOfFile.TrimEnd();
            int startPosition, endPosition;
            while (contentOfFile.Length > 20)
            {
                startPosition = contentOfFile.IndexOf("id");
                endPosition = contentOfFile.IndexOf("imagePath");
                if (startPosition >= 0 && endPosition > startPosition)
                {
                    startPosition = startPosition + 4;
                    endPosition = endPosition -3;
                    string dataToExtract = contentOfFile.Substring(startPosition, endPosition);
                    richTextBox1.AppendText(dataToExtract + "\r\n");
                    contentOfFile.Remove(startPosition-4, endPosition+12);
                }
                else
                { 
                    richTextBox1.AppendText("fail " + catalogType + "\r\n");
                    contentOfFile.Remove(contentOfFile.Length/2);
                }
            }
            return;
        }

Can anyone please share why this is not working as intended?? I think contentOfFile.Remove is not working and the main string doesn't get trimmed, but I don't understand why.

Many thanks!

6
  • 4
    This is an excellent time to become intimately familiar with the debugger. That being said, strings are immutable. You need to capture the value returned from many of those methods and act on that. Microsoft's documentation is also your friend. Commented Oct 17, 2022 at 15:41
  • First look at the Visual Studio Debugger Commented Oct 17, 2022 at 15:46
  • Thanks guys! Yet another thing learned today Commented Oct 17, 2022 at 15:46
  • 1
    Debugging is an essential skill. If you have to ask questions like this regularly, you're going to have a rough go at it. Debugging is self-service. Commented Oct 17, 2022 at 15:47
  • It's also important that you read the documentation for everything you use. The documentation for Remove clearly states, "Returns a new string in which a specified number of characters from the current string are deleted." The fact that it returns a string rather than void should also be an indication that you need to do something with it. Commented Oct 17, 2022 at 15:51

1 Answer 1

2

contentOfFile.Remove doesn't actually modify the value of contentOfFile it just returns a string that has the specified portion removed.

So you need to do contentOfFile = contentOfFile.Remove(startPosition - 4, endPostion + 12);

and same thing with the TrimEnd call before the while loop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.