none
How can i find a specific string in a text file using string.substring ?

    Question

  • int i;
             int index;
            index=14;
            i = 8;
            string t;
           
             sr = new StreamReader(Options_File);
             Options_File = sr.ReadToEnd();
    t = Options_File.Substring(index, i);

    Options_File is a text file .

    So now its working good im getting in t the string i need .

     

    But i have another string name Key wich contain inside "Set-Directory"

    How do i get the string of Key from Options_File ?

    What i want is to take out from Options_File only the Key part wich is Set-Directory"

    Second how do i make it automatic so i will not have to put manualy in index and in i the values . I want to find Key in Options-File automatic .

     

    Thanks .

     


    danieli
    Wednesday, April 07, 2010 5:32 PM

Answers

  • It would be easier if I knew how the content of your file looks like and what you want to retrieve from it.

    But let's say your options file contains  Set-Directory=C:\mydirectory; Let's also say that the C:\mydirectory is the part you want to retrieve.

    string key = "Set-Directory";
    int keyIndex = Options_File.IndexOf(key);
    int semiColonIndex = Options_File.IndexOf(';', keyIndex);
    string t = Options_File.Substring(keyIndex + key.Length + 1, semiColonIndex - keyIndex - key.Length - 1);
    

    Another way is using a regular expression

    string t = System.Text.RegularExpressions.Regex.Match(Options_File, "Set-Directory=([^;]*);").Groups[1].Value;
    • Marked as answer by Liliane Teng Tuesday, April 13, 2010 2:11 AM
    Wednesday, April 07, 2010 5:56 PM
  • You can use the .IndexOf() function to retrieve the index (i.e. finding a string in another string). For example suppose you want to find a 4-character word which is after the string: "Info: ". To do this:

                string source = "sth, sss Info: 1234 something else.";
                string key = "Info: ";
                int index = source.IndexOf(key) + key.Length;
                string subStr = source.Substring(index, 4);
                MessageBox.Show(subStr);

    If you don't know the length of the string (in this case 4), but you know that it is after key and before some other key, you could use this:

                string source = "sth, sss Info: 1234 something else.";
                string key1 = "Info: ";
                string key2 = " something";
                int start = source.IndexOf(key1) + key1.Length;
                int end = source.IndexOf(key2);
                int length = end - start;
                string subStr = source.Substring(start, length);
                MessageBox.Show(subStr);
    But of course there's a more beautiful and efficient way to do this and it's regular expressions. (Regex in C#)


    CPU: Core2 Due E7300 2.66GHz, RAM: 4GB DDR2, OS:Windows 7 Ultimate, HDD: 1TB SATA II, VGA: 1GB ATI , Compiler: VS 2008 Pro Sp1
    • Marked as answer by Liliane Teng Tuesday, April 13, 2010 2:12 AM
    Wednesday, April 07, 2010 6:04 PM

All replies

  • Can you post the content of that text file here. Also highlight the strings you want to parse from it.
    Thanks,
    A.m.a.L
    [MVP Visual C#]
    Dot Net Goodies
    Don't hate the hacker, hate the code
    Wednesday, April 07, 2010 5:51 PM
  • It would be easier if I knew how the content of your file looks like and what you want to retrieve from it.

    But let's say your options file contains  Set-Directory=C:\mydirectory; Let's also say that the C:\mydirectory is the part you want to retrieve.

    string key = "Set-Directory";
    int keyIndex = Options_File.IndexOf(key);
    int semiColonIndex = Options_File.IndexOf(';', keyIndex);
    string t = Options_File.Substring(keyIndex + key.Length + 1, semiColonIndex - keyIndex - key.Length - 1);
    

    Another way is using a regular expression

    string t = System.Text.RegularExpressions.Regex.Match(Options_File, "Set-Directory=([^;]*);").Groups[1].Value;
    • Marked as answer by Liliane Teng Tuesday, April 13, 2010 2:11 AM
    Wednesday, April 07, 2010 5:56 PM
  • Can you post the content of that text file here. Also highlight the strings you want to parse from it.
    Thanks,
    A.m.a.L
    [MVP Visual C#]
    Dot Net Goodies
    Don't hate the hacker, hate the code

    MVP, man good job...

    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Wednesday, April 07, 2010 5:57 PM
  • You can use the .IndexOf() function to retrieve the index (i.e. finding a string in another string). For example suppose you want to find a 4-character word which is after the string: "Info: ". To do this:

                string source = "sth, sss Info: 1234 something else.";
                string key = "Info: ";
                int index = source.IndexOf(key) + key.Length;
                string subStr = source.Substring(index, 4);
                MessageBox.Show(subStr);

    If you don't know the length of the string (in this case 4), but you know that it is after key and before some other key, you could use this:

                string source = "sth, sss Info: 1234 something else.";
                string key1 = "Info: ";
                string key2 = " something";
                int start = source.IndexOf(key1) + key1.Length;
                int end = source.IndexOf(key2);
                int length = end - start;
                string subStr = source.Substring(start, length);
                MessageBox.Show(subStr);
    But of course there's a more beautiful and efficient way to do this and it's regular expressions. (Regex in C#)


    CPU: Core2 Due E7300 2.66GHz, RAM: 4GB DDR2, OS:Windows 7 Ultimate, HDD: 1TB SATA II, VGA: 1GB ATI , Compiler: VS 2008 Pro Sp1
    • Marked as answer by Liliane Teng Tuesday, April 13, 2010 2:12 AM
    Wednesday, April 07, 2010 6:04 PM