开发者

String manipulation in C#: split on `/`

I need to extract headstone data from an inscription file (structured text file). From this file I am supposed to extract the name of a deceased person, date of birth (or age) and also personal messages. The application should be able to analyse a raw text file and then extract information and display it in tabular form.

The raw text files looks like this:

In loving memory of/JOHN SMITH/wh开发者_如何学Co died on 13.02.07/at age 40/we will miss you/In loving memory of/JANE AUSTIN/who died on 10.06.98/born on 19.12.80/hope you are well in heaven.

Basically / is a delimiter and the name of a deceased person is always in capital letters. I have tried to use String.Split() and substring methods but I can't get it to work for me; I can only get raw data without the delimiter (Environment.Newline) but I don't know how to extract specific information.


You'll need something like:

  • Open your data file (System.IO)
  • For each line, do (tip: pick a stream where you can read line by line)
    • Split them by "/" getting a string[]
    • Arrays in C# starts at 0; so splitted[1] will be that name, ans so on
    • Store that information in a managed collection (maybe to use generics?)
  • Display that collection in a tabular view


Check out How to: Use Regular Expressions to Extract Data Fields. I know the example is in C++, but the Regex and Match classes should help you get started

Here is another good example of parsing out individual sentences.


I would have thought something like this would do the trick

 private static void GetHeadStoneData()
 {
    using(StreamReader read = new StreamReader("YourFile.txt"))
    {
       const char Delimter = '/';
       string data;
       while((data = read.ReadLine()) != null)
       {
            string[] headStoneInformation = data.Split(Delimter);
            //Do your stuff here, e.g. Console.WriteLine("{0}", headStoneInformation[0]);
       }
    }
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜