Question

i'm looking for most efficient method of lifting text between certain SWIFT tags.

My string contains the following

:59:/SOMETEXT
MORETEXT
EVEN MORE TET
:71A:/some other text

So in the example above, i need to store all text lines between :59: and :70: however, tag :59: can also be represent as follows:

:59a:/SOMETEXT
MORETEXT
EVEN MORE TET
:71A:/some other text

I was thinking of looping through each and saving current tag and previous tag, and then checking if the previous tag was 71A, but there could be further 'Optional' tags in-between tag 59a and tag 71a

Any suggestions on how to handle this?

Was it helpful?

Solution

I would use Regular Expressions because they are easier to maintain than loops:

    public string GetText(string text, string tag1, string tag2)
    {
        return Regex.Match(text, String.Format(":{0}[^:]?:(?<text>(\n|.)*):{1}[^:]?:", tag1, tag2)).Groups["text"].Value;
    }

OTHER TIPS

var str = ":59:/SOME TEXT\n" +
          "MORE TEXT\n" +
          "EVEN MORE TEXT\n" +
          ":71A:/some other text\n";

var text = str.Split("\n\r".ToCharArray()).SkipWhile(l => !l.StartsWith(":59")).TakeWhile(l => !l.StartsWith(":71"));

var result = text.Select(l => new string(l.SkipWhile(c => Char.IsPunctuation(c) || Char.IsDigit(c)).ToArray()));

foreach (var l in result)
    Console.WriteLine(l); 

// output: SOME TEXT
//         MORE TEXT
//         EVEN MORE TEXT
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top