Marsh
Marsh

Reputation: 173

how to split the string data based on date in c#

I have string with date, want to split as separate string based on date

For example : I have this type of string data

"21/04/21, 1:54 pm - Person1: Really a very useful website to know the products.\n\nhttps://www.google.com\n21/04/21, 2:08 pm - Person2: this is second text\n21/04/21, 2:53 pm - Person3: Really a very useful website for question and answers.\n\nhttps://www.stackoverflow.com\n"

and i want to split it based on date like as

"21/04/21, 1:54 pm - Person1: Really a very useful website to know the products.\n\nhttps://www.google.com"
"21/04/21, 2:08 pm - Person2: this is second text"
"22/04/21, 2:53 pm - Person3: Really a very useful website for question and answers.\n\nhttps://www.stackoverflow.com\n"

i have tried below code but it is splitting as newline instead of date wise

string[] lines = text.Split(new[] { "\r\n", "\r", "\n" },StringSplitOptions.None);

can anyone suggest how to split this..

Upvotes: 2

Views: 1157

Answers (3)

Kartik Maheshwari
Kartik Maheshwari

Reputation: 357

string x = "21/04/21, 1:54 pm - Person1: Really a very useful website to know the products.\n\nhttps://www.google.com\n21/04/21, 2:08 pm - Person2: this is second text\n21/04/21, 2:53 pm - Person3: Really a very useful website for question and answers.\n\nhttps://www.stackoverflow.com\n";
        List<string> lines = x.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None).ToList();
        lines.RemoveAll(s => string.IsNullOrWhiteSpace(s));

        for (int i=0; i < lines.Count(); i++)
        {
           
            if (lines[i].StartsWith("http"))
            {
                lines[i - 1] = lines[i - 1] + "  " + lines[i];
                lines.Remove(lines[i]);
            }
            
        }
        for (int i = 0; i < lines.Count(); i++)
        {
            lines[i] = lines[i].Substring(lines[i].IndexOf(": ")).Remove(0,2);
            Console.WriteLine(lines[i]);
        }

enter image description here

Upvotes: 0

Olivier Duhart
Olivier Duhart

Reputation: 437

I go for a full answer

 static void Main(string[] args)
        {
            Regex regex = new Regex("^[0-9]{2}/[0-9]{2}/[0-9]{2}, [0-9]:[0-9]{2} (pm|am)", RegexOptions.Multiline);
            var source = @"
21/04/21, 1:54 pm - Person1: Really a very useful website to know the products.

https://www.google.com
21/04/21, 2:08 pm - Person2: this is second text
21/04/21, 2:53 pm - Person3: Really a very useful website for question and answers.
https://www.stackoverflow.com";
            var matches = regex.Matches(source);
            foreach (Match match in matches)
            {
                var g = match.Groups[0];
                Console.WriteLine($"{g.Index} : {g.Value}");
            }

            for (int i = 0; i < matches.Count - 1; i++)
            {
                int start = matches[i].Index;
                int end = matches[i + 1].Index;
                var item = source.Substring(start, end - start);
                Console.WriteLine("---------");
                Console.WriteLine(item);
            }

            var lastitem = source.Substring(matches.Last().Index);
            Console.WriteLine("---------");
            Console.WriteLine(lastitem);
        }

this will output

---------
21/04/21, 1:54 pm - Person1: Really a very useful website to know the products.

https://www.google.com

---------
21/04/21, 2:08 pm - Person2: this is second text

---------
21/04/21, 2:53 pm - Person3: Really a very useful website for question and answers.
https://www.stackoverflow.com

Upvotes: 2

Olivier Duhart
Olivier Duhart

Reputation: 437

Regular expression may be your solution.

  1. find date pattern position with a regex : ^[0-9]{2}/[0-9]{2}/[0-9]{2}, [0-9]:[0-9]{2} (pm|am)
  2. split string using date positions

something like this :

Regex regex = new Regex("^[0-9]{2}/[0-9]{2}/[0-9]{2}, [0-9]:[0-9]{2} (pm|am)", RegexOptions.Multiline);
            var source = @"
21/04/21, 1:54 pm - Person1: Really a very useful website to know the products.

https://www.google.com
21/04/21, 2:08 pm - Person2: this is second text
21/04/21, 2:53 pm - Person3: Really a very useful website for question and answers.
https://www.stackoverflow.com";
            var matches = regex.Matches(source);
            foreach (Match match in matches)
            {
                var g = match.Groups[0];
                Console.WriteLine($"{g.Index} : {g.Value}");
            }

will produce following output which is my first step's proposal :

2 : 21/04/21, 1:54 pm
109 : 21/04/21, 2:08 pm
159 : 21/04/21, 2:53 pm

I let you complete it with sring splitting

Upvotes: 1

Related Questions