1

I am trying to parse a long text separated by *. That text has to be shown like the second example. What is the simplest way of parsing this sample text in order to sort the text for easy reading.

Text sample

*01/21/2008//David//Los Angeles//Manager* He is in *01/21/2008//David//Los Angeles//Manager* He is Out for lunch *01/21/2008//David//Los Angeles//Manager* He came back from lunch *01/21/2008//David//Los Angeles//Manager* He is out for the day

The way has to be shown in a TextBox

*01/21/2008//David//Los Angeles//Manager* He is in  
*01/21/2008//David//Los Angeles//Manager* He is Out for lunch 
*01/21/2008//David//Los Angeles//Manager* He came back from lunch
*01/21/2008//David//Los Angeles//Manager* He is out for the day

3 Answers 3

3

You can simply use string.Split('*') and use Array.Sort to sort the resulting array.

Alternatively, use the TextFieldParser class to parse the file, using * as the delimiter.

Sign up to request clarification or add additional context in comments.

1 Comment

Wouldn't this version split on the second "" in the "good" string? The output would be Ln1: *01/21/2008//David//Los Angeles//Manager Ln2: * He is in Ln3: *01/21/2008//David//Los Angeles//Manager...etc. Combination of Split and/or regex to look for a "" followed by a digit (or the format of the date string) would be better, no?
1

Here's another version for you. Excuse the fact that I'm a c# guy, so I hope this looks acceptable in VB:

Dim re As New Regex("(\*\d+\/\d+\/\d+(?:\/\/[\w\s]+){3}\*[\w\s]+)")
Dim original As String = "*01/21/2008//David//Los Angeles//Manager* He is in *01/21/2008//David//Los Angeles//Manager* He is Out for lunch *01/21/2008//David//Los Angeles//Manager* He came back from lunch *01/21/2008//David//Los Angeles//Manager* He is out for the day"

Dim processed As String = re.Replace(original, "$1" + vbCrLf)
Dim lines As String() = processed.Split(vbCrLf)

For Each l As String In lines
        Console.WriteLine(l)
Next

output:

*01/21/2008//David//Los Angeles//Manager* He is in 

*01/21/2008//David//Los Angeles//Manager* He is Out for lunch 

*01/21/2008//David//Los Angeles//Manager* He came back from lunch 

*01/21/2008//David//Los Angeles//Manager* He is out for the day

Then you can just add processed variable to the textbox.

DEMO: http://www.ideone.com/ICwID

Comments

0

Try this: Link: http://ideone.com/bGLo8

using System;
using System.Collections.Generic;
using System.Linq;

public class Test
{
        public static void Main()
        {
            string str = "*01/21/2008//David//Los Angeles//Manager* He is in *01/21/2008//David//Los Angeles//Manager* He is Out for lunch *01/21/2008//David//Los Angeles//Manager* He came back from lunch *01/21/2008//David//Los Angeles//Manager* He is out for the day";
            int cnt = 1;
            Action<char> PrintAction = delegate(char x)
                {
                    if (x == '*') { cnt = (cnt + 1) % 2; if (cnt == 0) Console.WriteLine(); }
                    Console.Write(x);
                };
            str.ToCharArray().ToList().ForEach(X => PrintAction(X));    
        }
}   

Comments

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.