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 “!”)
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 _= sentenceList*.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 stringList = new List();
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.