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;
} 
AGB
  • 2,230
  • 1
  • 14
  • 21
cwsl26
  • 33
  • 6

3 Answers3

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

Vishal Madhvani
  • 322
  • 3
  • 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 May 05 '16 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: https://dotnetfiddle.net/sS6GdH – Vishal Madhvani May 05 '16 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 May 05 '16 at 20:22
  • Yup, -2 from the second parameter in `whatToParse.Substring` would also work. – Vishal Madhvani May 05 '16 at 20:24
  • Thanks so much dude really appreciate it. – cwsl26 May 05 '16 at 20:25
  • No worries.. Have fun bud :) – Vishal Madhvani May 05 '16 at 20:38
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

Community
  • 1
  • 1
Marco Scabbiolo
  • 7,203
  • 3
  • 38
  • 46
  • You're almost correct here. What if the inputs contain regex control characters? You need to escape them using Regex.Escape(). – Xavier J May 05 '16 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));
}
nobody
  • 10,892
  • 8
  • 45
  • 63