C # split the string, but keep separate characters / delimiters

I split the string into three different characters, but I want the output to include the characters that I split. Is there an easy way to do this?

+80
string c #
Feb 06 '09 at 17:02
source share
12 answers

If the delimiters were,, . and ; I would try:

 string[] parts = Regex.Split(originalString, @"(?<=[.,;])") 

(?<=PATTERN) - positive appearance for PATTERN . It should match anywhere where the preceding text matches PATTERN , so there should be a match (and separation) after each occurrence of any of the characters.

+129
Feb 06 '09 at 17:08
source share

Based on BFree's answer, I had the same goal, but I wanted to split an array of characters similar to the original Split method, and I also have several sections per line:

 public static IEnumerable<string> SplitAndKeep(this string s, char[] delims) { int start = 0, index; while ((index = s.IndexOfAny(delims, start)) != -1) { if(index-start > 0) yield return s.Substring(start, index - start); yield return s.Substring(index, 1); start = index + 1; } if (start < s.Length) { yield return s.Substring(start); } } 
+20
Jun 29 '10 at 17:08
source share

Just in case someone wants this answer ...

Instead of string[] parts = Regex.Split(originalString, @"(?<=[.,;])") You can use string[] parts = Regex.Split(originalString, @"(?=yourmatch)") , where yourmatch is your separator.

Suppose the original string was

777- cat

777 - dog

777 - mouse

777 - rat

777 - Wolf

Regex.Split(originalString, @"(?=777)") will return

777 - cat

777 - dog

etc.

+19
Apr 18 '11 at 17:20
source share

This seems to work, but it has not been tested much.

 public static string[] SplitAndKeepSeparators(string value, char[] separators, StringSplitOptions splitOptions) { List<string> splitValues = new List<string>(); int itemStart = 0; for (int pos = 0; pos < value.Length; pos++) { for (int sepIndex = 0; sepIndex < separators.Length; sepIndex++) { if (separators[sepIndex] == value[pos]) { // add the section of string before the separator // (unless its empty and we are discarding empty sections) if (itemStart != pos || splitOptions == StringSplitOptions.None) { splitValues.Add(value.Substring(itemStart, pos - itemStart)); } itemStart = pos + 1; // add the separator splitValues.Add(separators[sepIndex].ToString()); break; } } } // add anything after the final separator // (unless its empty and we are discarding empty sections) if (itemStart != value.Length || splitOptions == StringSplitOptions.None) { splitValues.Add(value.Substring(itemStart, value.Length - itemStart)); } return splitValues.ToArray(); } 
+2
Aug 27 '09 at 11:22
source share
 result = originalString.Split(separator); for(int i = 0; i < result.Length - 1; i++) result[i] += separator; 

( EDIT is a bad answer - I misunderstood his question and did not see him break up a few characters.)

(EDIT - The correct version of LINQ is inconvenient because the delimiter must not be concatenated with the last line in the split array.)

+1
Feb 06 '09 at 17:04
source share

I recently wrote an extension method for this:

 public static class StringExtensions { public static IEnumerable<string> SplitAndKeep(this string s, string seperator) { string[] obj = s.Split(new string[] { seperator }, StringSplitOptions.None); for (int i = 0; i < obj.Length; i++) { string result = i == obj.Length - 1 ? obj[i] : obj[i] + seperator; yield return result; } } } 
+1
Feb 06 '09 at 17:06
source share

Iterating over the character of a string by character (this is what a regular expression is all the same). When you find the splitter, then release the substring.

pseudo code

 int hold, counter; List<String> afterSplit; string toSplit for(hold = 0, counter = 0; counter < toSplit.Length; counter++) { if(toSplit[counter] = /*split charaters*/) { afterSplit.Add(toSplit.Substring(hold, counter)); hold = counter; } } 

This type of C #, but not really. Obviously, select the appropriate function names. In addition, I think there might be an offside error.

But that will do what you ask.

+1
Feb 06 '09 at 19:42
source share

Lots of answers to this! I knocked one to break it into different lines (the original answer only matches characters, that is, a length of 1). This has not been fully tested.

 public static IEnumerable<string> SplitAndKeep(string s, params string[] delims) { var rows = new List<string>() { s }; foreach (string delim in delims)//delimiter counter { for (int i = 0; i < rows.Count; i++)//row counter { int index = rows[i].IndexOf(delim); if (index > -1 && rows[i].Length > index + 1) { string leftPart = rows[i].Substring(0, index + delim.Length); string rightPart = rows[i].Substring(index + delim.Length); rows[i] = leftPart; rows.Insert(i + 1, rightPart); } } } return rows; } 
+1
Mar 25 '15 at 9:33
source share

Regex.Split looks as if it can do what you want, possibly.

0
Feb 06 '09 at 17:06
source share
 using System.Collections.Generic; using System.Text.RegularExpressions; namespace ConsoleApplication9 { class Program { static void Main(string[] args) { string input = @"This;is:a.test"; char sep0 = ';', sep1 = ':', sep2 = '.'; string pattern = string.Format("[{0}{1}{2}]|[^{0}{1}{2}]+", sep0, sep1, sep2); Regex regex = new Regex(pattern); MatchCollection matches = regex.Matches(input); List<string> parts=new List<string>(); foreach (Match match in matches) { parts.Add(match.ToString()); } } } } 
0
Feb 06 '09 at 17:35
source share

Java Code:

 public static class String_Ext { public static string[] SplitOnGroups(this string str, string pattern) { var matches = Regex.Matches(str, pattern); var partsList = new List<string>(); for (var i = 0; i < matches.Count; i++) { var groups = matches[i].Groups; for (var j = 0; j < groups.Count; j++) { var group = groups[j]; partsList.Add(group.Value); } } return partsList.ToArray(); } } var parts = "abcde \tfgh\tikj\r\nlmno".SplitOnGroups(@"\s+|\S+"); for (var i = 0; i < parts.Length; i++) Print(i + "|" + Translate(parts[i]) + "|"); 

Output:

 0|abcde| 1| \t| 2|fgh| 3|\t| 4|ikj| 5|\r\n| 6|lmno| 
0
Oct 23 2018-10-23 at
source share

See Use Regex.Split () . I think u / veggerby answer is the best. Note that it uses the regex group syntax (for example, '(' and ')') surrounding the matched delimiters to store them in the return split.

0
Feb 01 '17 at 17:11
source share



All Articles