Question

I have a problem where I need to get specific strings out of a text file. This file is a settings file to something so most of the time what I need is contained in one line. I need to copy these unknow strings into textBoxes, but I know the text right before the strings themselves (something like name = cannon where cannon is the string I need) How can I copy from the "=" to the end of the line? (I copied it out into a richTextBox)

Was it helpful?

Solution

Try this:

string settings = string.Empty;
IEnumerable<string> lines = File.ReadLines(myPath); //reads all lines of text file
foreach (string s in lines) //iterate thru all lines
{
    if s.Contains("=")
    {
        settings = s.substring(s.IndexOf("=")); //get substring from "=" to end of line
        break; //break out of the loop
    }
}

This is basically an expansion of slobodan's answer. Your question is how to copy from "=" to the end of the line, which this does. Your comment on his answer is contradictory, however, as you mention there that it's on multiple lines instead. Let me know what you need and I'll try to change my answer.

OTHER TIPS

Something like text.substring(text.IndexOf("="))

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top