C# split string but keep split chars / separators

2019-01-01 03:57发布

This question already has an answer here:

I'm splitting a string by three different characters but I want the output to include the characters I split by. Is there any easy way to do this?

标签: c# string
11条回答
不再属于我。
2楼-- · 2019-01-01 04:18

If the split chars were ,, ., and ;, I'd try:

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

(?<=PATTERN) is positive look-behind for PATTERN. It should match at any place where the preceding text fits PATTERN so there should be a match (and a split) after each occurrence of any of the characters.

查看更多
怪性笑人.
3楼-- · 2019-01-01 04:23

A lot of answers to this! One I knocked up to split by various strings (the original answer caters for just characters i.e. length of 1). This hasn't 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;
}
查看更多
孤独总比滥情好
4楼-- · 2019-01-01 04:26

Just in case anyone wants this answer aswell...

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

Supposing the original string was

777- cat

777 - dog

777 - mouse

777 - rat

777 - wolf

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

777 - cat

777 - dog

and so on

查看更多
春风洒进眼中
5楼-- · 2019-01-01 04:27
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());
            }
        }
    }
}
查看更多
ら面具成の殇う
6楼-- · 2019-01-01 04:28

Iterate through the string character by character (which is what regex does anyway. When you find a splitter, then spin off a 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;
   }
}

That's sort of C# but not really. Obviously, choose the appropriate function names. Also, I think there might be an off-by-1 error in there.

But that will do what you're asking.

查看更多
十年一品温如言
7楼-- · 2019-01-01 04:33

Building off from BFree's answer, I had the same goal, but I wanted to split on an array of characters similar to the original Split method, and I also have multiple splits per string:

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);
    }
}
查看更多
登录 后发表回答