0

I am using a flat text file as a database and so i need to be able to parse a .txt. The first thing i need to do is be able to pull out the exact string i'm looking for which i am confused about. The other answers i have seen to this the strings are the same every time... but i am looking for how to pull out a text for example between (beginning) and (end) where everything in between is a different length every time. any help? Here is an example of what i want

String line = sr.ReadToEnd();
// line to get all the text from my file
string whatIWant = stringEditor("beginning", "end", line);

string whatIWant(string first, string second, string whatToParse)
{
    //what do I put here to return
    //the text from line between first and second
    return whatever;
} 

3 Answers 3

3

You should use String.Substring()

string whatIWant(string first, string second, string whatToParse)
{
    return whatToParse.Substring(first.Length, whatToParse.Length - first.Length - second.Length);
}

DotNetFiddle: https://dotnetfiddle.net/h5lNIB

6
  • Hey this was the only one that worked the only thing is, is that it is returning two more letters then it should. For example it is supposed to return 5, it returns 5(e which is the first two characters after 5. How do i just remove the last 2 at the end.
    – cwsl26
    Commented May 5, 2016 at 20:14
  • Ahh, thats because windows uses "\r\n" for new lines, so we need to remove that as well. Add "\r\n" to the end of the string being passed into the second variable. I updated the fiddle: dotnetfiddle.net/sS6GdH Commented May 5, 2016 at 20:19
  • Oh wow you are write. whatToParse has all my text then \r\n... nice catch! i assume i just have to minus 2 extra for that also. Thanks!
    – cwsl26
    Commented May 5, 2016 at 20:22
  • Yup, -2 from the second parameter in whatToParse.Substring would also work. Commented May 5, 2016 at 20:24
  • Thanks so much dude really appreciate it.
    – cwsl26
    Commented May 5, 2016 at 20:25
2

Took the solution from this answer , on how to extend the native string class to get a string betwen to strings.

Create the method Between in all strings:

public static class Ext
{
    public static string Between(this string source, string left, string right)
    {
        return System.Text.RegularExpressions.Regex.Match(
                System.Text.RegularExpressions.Regex.Excape(source),
                string.Format("{0}(.*){1}", left, right))
            .Groups[1].Value;
    }
}

Then it gets really easy:

"beginning123456end".Between("beginning", "end")

123456

If you're always using beginning and end, let's go further:

public static class Ext
{
    public static string BetweenBeginningAndEnd(this string source)
    {
        return System.Text.RegularExpressions.Regex.Match(
                System.Text.RegularExpressions.Regex.Excape(source),
                string.Format("{0}(.*){1}", "beginning", "end"))
            .Groups[1].Value;
    }
}

"beginning123456end".BetweenBeginningAndEnd()

123456

EDIT: As @codenoire said, you'll also need to escape possible Regex characters

1
  • You're almost correct here. What if the inputs contain regex control characters? You need to escape them using Regex.Escape().
    – Xavier J
    Commented May 5, 2016 at 20:02
1

Get the index of beginning and the index of end, then use substring to get the the string using the indexes.

string whatIWant(string beginning, string end, string whatToParse)
{
      return whatToParse.Substring(whatToParse.IndexOf(beginning) + beginning.Length,whatToParse.IndexOf(end) -(whatToParse.IndexOf(beginning) + beginning.Length));
}
2
  • Can you show an example of this? Sorry im kinda new to programming
    – cwsl26
    Commented May 5, 2016 at 19:34
  • @cwsl26 I've edited the answer to include the example.
    – nobody
    Commented May 5, 2016 at 21:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.