0

I have some question about "find specific string from between two string and get into list".

I have a string like that :

-begin- 
code:[264]
Name:[1] 
Name:[2] 
Name:[3]
-end- 
code:[264]
Name:[1] 
Name:[4] 
code:[264]
Name:[6]
-begin- 
Name:[6] 
code:[264]
Name:[7] 
Name:[8] 
Name:[1]
-end-

I want to split string "Name:" between "-begin-" to "-end-" into List like as below,

list<1>
Name:[1] 
Name:[2] 
Name:[3]
list<2>
Name:[6] 
Name:[7] 
Name:[8]
Name:[1]

Now I can only split text between "-begin-" to "-end-" into list.

int first = 0;
int last = 0;
int number = 0;
int start = 0;
  do {
      first = text.IndexOf("-begin-", start);
      last = text.IndexOf("-begin-", first + 1);
         if (first >= 0 && last >= 0)
           {
                number = (last - first);
                AVPline.Add(text.Substring(first,number).Trim());
                start = last + 1;
           }
     } while (position > 0);

I have no idea to split string "Name:" after split text between "-begin-" to "-end-".

Can somebody help me with this.

Thank You very much.

2
  • If you have a line "Name:[3]" you use Split(':')[1] to get the [3] portion. Is that what you're asking? Commented Oct 2, 2016 at 16:47
  • We still don't have enough information. For instance, the "-begin-" and "-end-" sequences don't seem to match up in any way with the position of the Name fields. Some Name fields are inside of a begin/end block, others are not. It's rather haphazard. If your sample data is accurate, then you simply must sit down and write some detailed parsing logic on your own - there is no technical problem (other than the parsing logic) that we can help you with. Commented Oct 2, 2016 at 17:08

3 Answers 3

1

String.Split will parse the string into an array substrings. Then you process the substrings.

string[] parseStr = text.Split('\n');
Sign up to request clarification or add additional context in comments.

1 Comment

Thank a lot jim31415. Your code made easy way to split string to each line.
1

You can achieve much cleaner and maintainable code making use of enumerators and yield returns:

First of all, lets get the signature right. You need to create a collection of lists from an input string: public static IEnumerable<IList<string>> ProcessText(string input) looks about right.

Now, lets make our life easier. First of all, lets split the input string in lines:

var lines = input.Split(new[] { Environment.NewLine });

Now, lets iterate through the lines but lets not use a foreach to do this. Lets get a little dirtier and use the enumerator directly for reasons that will become clear in a bit:

using (var enumerator = lines.GetEnumerator())
{
    while (enumerator.MoveNext()
    {
    }
}

Ok, now we just need to find the begining of each sublist and build it accordingly. Thats easy:

while (enumerator.MoveNext()
{
    if (enumerator.Current == "-begin-"
        yield return createSubList(enumerator).ToList();
}

And now you see why I use the Enumerator directly. This way I can call another method easily keeping track where we are in the input text:

static IEnumerable<string> createSubList(IEnumerator<string> enumerator)
{
    while (enumerator.MoveNext()
    {
        if (enumerator.Current == "-end-")
        {
            yield break;
        }

        if (!enumerator.Current.StartsWith(...whatever strings you want to ignore))
        {
            yield return enumerator.Current;
        }      
    }
}

And we're done! Lets put it all together:

public IEnumerable<IList<string>> ProcessText(string input)
{
    var lines = input.Split(new[] { Environment.NewLine });

    using (var enumerator = lines.GetEnumerator())
    {
        while (enumerator.MoveNext()
        {
            if (enumerator.Current == "-begin-"
                yield return createSubList(enumerator).ToList();
        }
    }
}

private static IEnumerable<string> createSubList(IEnumerator<string> enumerator)
{
    while (enumerator.MoveNext()
    {
        if (enumerator.Current == "-end-")
        {
            yield break;
        }

        if (!enumerator.Current.StartsWith(...whatever strings you want to ignore))
        {
            yield return enumerator.Current;
        }      
    }
}

4 Comments

Great use of enumerator directly! +1 for that!
Works only if the input string contains NewLine as we assume by OP's sample text. Your solution looks good, but I'm afraid it's too complex for OP.
Thank a lot InBetween, after use your code I can pass this part of my program but I have more question.If I want to find string between line I can not use enumerator.Current.StartsWith can I use something like enumerator.Current.Indexof or not?
@JooHwang enumerator.Current is a string so you can use any method or extension method you'd use in a string. So, yes, you can call IndexOf.
0

Your try isn't too far away from a working solution.
I only have my mobile phone at hand, so I cannot give you tested code. But here's my advices:

  • you seem to want an array of List of String. Create a List of List of String, because you don't know how big your array will become. You can easily get an array from that list at the end.
  • you need a loop. You have that already.
  • inside the loop, find your start and end. You have almost done that. You should search for -begin- and -end-
  • take the desired data between -begin- and -end- and process it inside an inner (nested) loop

Nested loop:

  • create a List of string
  • take the data, search for starting Name and ending ] in the same way like in the outer loop.
  • add found elements into the list.

After nested loop, still inside outer loop:

  • add the list of string, which you have just filled, into the list of list of string.

End of outer loop.

Remember to use valid conditions for your outer loop. In your sample you never set 'position'.

1 Comment

Thank you very much your concept is very clear and easy to understand.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.