Split string: keep split chars separators C#
Hello,
I was wondering how to keep the split chars separators when you split a string.
For example:
UserString = "This is my first sentence. This is my second sentence!";
SentencesList = UserString.Split(new string[] { ".", "!", "?" }, System.StringSplitOptions.None);
My result will be: SentcenceList[0] = "This is my first sentence" SentcenceList[1] = "This is my second sentence"
How I can keep the the split chars separators? (in this context "." and "!")
Answer by Ahndrakhul · May 17, 2016 at 11:10 PM
I'm kind of surprised that this isn't an option with string.split. I'm not very good with regex, but I know that you can do this sort of thing with Regex.Split. You would have to include System.Text.RegularExpressions, and then do something like this:
string userString = "This is my first sentence. This is my second sentence!";
string pattern = "(?<=[.!?])";
string[] sentencesList = Regex.Split(userString, pattern);
In this case, every sentence beyond the first one will have at least one preceding space that you will want to remove. There is probably a way to do this with Regex, but I don't know how. You could also go through the resulting sentenceList array and call trim() on each string.
for(int i = 0; i < sentenceList.Length; i++)
{
sentenceList[i] = sentenceList[i].Trim();
}
Performance-wise, this is not very good, but if you aren't doing it often, it probably doesn't matter. If you don't want to use Regex, you can use a custom split method. Maybe something like this.
public string[] SplitString(string stringToSplit, char[] delimiters, bool includeDelimiter, bool trimEnds)
{
int index = -1;
List<string> stringList = new List<string>();
StringBuilder sBuilder = new StringBuilder();
while(++index < stringToSplit.Length)
{
sBuilder.Append(stringToSplit[index]);
foreach(char c in delimiters)
{
if(stringToSplit[index] == c)
{
if(!includeDelimiter)
{
sBuilder.Remove(sBuilder.Length -1, 1);
}
if(trimEnds)
{
stringList.Add(sBuilder.ToString().Trim());
}
else
{
stringList.Add(sBuilder.ToString());
}
sBuilder.Clear();
break;
}
}
}
return stringList.ToArray();
}
}
This isn't very pretty, but it will probably work for what you want to do. Hopefully someone else can respond with a better way of doing this.
Hello.
Yes, I'm amazed too there is not a direct option in the split. But your example is fitting well with my problem. The space is not a problem in my case. And you explain it perfectly! Thanks to take so much of your time to give me a good solution and an alternative one! I didn't expect so much!