문제

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)

도움이 되었습니까?

해결책

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.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top