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:37

Recently I wrote an extension method do to 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;
            }
        }
    }
查看更多
无色无味的生活
3楼-- · 2019-01-01 04:39
result = originalString.Split(separator);
for(int i = 0; i < result.Length - 1; i++)
    result[i] += separator;

(EDIT - this is a bad answer - I misread his question and didn't see that he was splitting by multiple characters.)

(EDIT - a correct LINQ version is awkward, since the separator shouldn't get concatenated onto the final string in the split array.)

查看更多
浮光初槿花落
4楼-- · 2019-01-01 04:41

This seems to work, but its 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();
}
查看更多
高级女魔头
5楼-- · 2019-01-01 04:44

Regex.Split looks like it might be able to do what you want perhaps.

查看更多
残风、尘缘若梦
6楼-- · 2019-01-01 04:44

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