Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String split using C#

Tags:

c#

I have the following string:

string text = "1. This is first sentence. 2. This is the second sentence. 3. This is the third sentence. 4. This is the fourth sentence."

I want to split it according to 1. 2. 3. and so on:

result[0] == "This is first sentence."
result[1] == "This is the second sentence."
result[2] == "This is the third sentence."
result[3] == "This is the fourth sentence."

Is there any way I can do it C#?

like image 337
fawad Avatar asked Oct 19 '11 14:10

fawad


Video Answer


3 Answers

Assuming that you can't encounter such a pattern in your sentences : X. (a integer, followed by a point, followed by a space), this should work:

String[] result = Regex.Split(text, @"[0-9]+\. ");
like image 98
Otiel Avatar answered Sep 24 '22 11:09

Otiel


is it possible that there will be numbers in the sentence too?

As I do not know you formatting, you already said you cannot do on EOL/New Line I would try something like...

List<string> lines = new List<string>();
string buffer = "";
int count = 1;

foreach(char c in input)
{
   if(c.ToString() == count.ToString())
   {
      if(!string.IsNullOrEmpty(buffer))
      {
         lines.Add(buffer);
         buffer = "";
      }
      count++;
   }
   buffer += c;
}

//lines will now contain your splitted data

You can then access each sentence like this...

string s1 = lines[0];
string s2 = lines[1];
string s3 = lines[2];

Important: Make sure you check the count of lines before getting sentence like...

string s1 = lines.Count > 0 ? lines[0] : "";

This makes a big assumption that you will not have the next lines number ID in a given sentance (i.e. sentence 2 will not contain the number 3)

If this does not help the provide you input in original format (do not add lines breaks if there are none)

EDIT: Fixed my code (wrong variable sorry)

like image 23
musefan Avatar answered Sep 24 '22 11:09

musefan


int index = 1; 
String[] result = Regex.Split(text, @"[0-9]+\. ").Where(i => !string.IsNullOrEmpty(i)).Select(i => (index++).ToString() + ". " + i).ToArray();

result will contain your sentences, including the "line number".

like image 41
Alex Avatar answered Sep 24 '22 11:09

Alex