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 sho开发者_如何学Cwn 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
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.
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
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));
}
}
精彩评论