如何在 C# 中使用 BCL 拆分字符串而不丢失分隔符?

发布于 2024-11-26 17:15:47 字数 300 浏览 0 评论 0原文

我需要根据某些分隔符字符数组分割字符串,并且不要丢失字符串中的这些分隔符。即:

string: "Hello world!"
separators: " !"
result: ("Hello", " ", "world", "!")

当然,我可以编写一些东西来遍历该字符串并返回我需要的结果,但是是否已经有一些东西允许我这样做,比如神奇地配置了String.Split

更新:我需要在没有正则表达式的情况下解决问题,因为它对我来说非常慢。

I need to split a string based on some character array of separators and not lose these separators in string. I.e.:

string: "Hello world!"
separators: " !"
result: ("Hello", " ", "world", "!")

Of course, i can write something that goes through that string and returns me needed result, but isn't there something already allowing me to do this, like magically configured String.Split?

Upd: I need to solution without regexp, because it is very slow for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

べ繥欢鉨o。 2024-12-03 17:15:47

使用正则表达式:

string[] parts = Regex.Split(myString, yourPattern);

测试:

string[] parts = Regex.Split("Hello World!", "(!| )");

输出:

Hello
" "//just space
World
!
""//empty string

Use regular expression:

string[] parts = Regex.Split(myString, yourPattern);

Test:

string[] parts = Regex.Split("Hello World!", "(!| )");

output:

Hello
" "//just space
World
!
""//empty string
尽揽少女心 2024-12-03 17:15:47

一个linq解决方案:

var s = "Hello world!";
char[] separators = { ' ', '!' };

string current = string.Empty;
List<string> result = s.Aggregate(new List<string>(), (list, ch) =>
    {
        if (separators.Contains(ch))
        {
            list.Add(current);
            list.Add(ch.ToString());
            current = string.Empty;
        }
        else current += ch;
        return list;
    }, list => list);

A linq solution:

var s = "Hello world!";
char[] separators = { ' ', '!' };

string current = string.Empty;
List<string> result = s.Aggregate(new List<string>(), (list, ch) =>
    {
        if (separators.Contains(ch))
        {
            list.Add(current);
            list.Add(ch.ToString());
            current = string.Empty;
        }
        else current += ch;
        return list;
    }, list => list);
尤怨 2024-12-03 17:15:47

这将是一个纯粹的程序解决方案:

private static IEnumerable<string> Tokenize(string text, string separators)
{
    int startIdx = 0;
    int currentIdx = 0;

    while (currentIdx < text.Length)
    {
        // found a separator?
        if (separators.Contains(text[currentIdx]))
        {
            // yield a substring, if it's not empty
            if (currentIdx > startIdx)
                yield return text.Substring(startIdx, currentIdx - startIdx);

            // yield the separator
            yield return text.Substring(currentIdx, 1);

            // mark the beginning of the next token
            startIdx = currentIdx + 1;
        }

        currentIdx++;
    }
}

请注意,此解决方案避免返回令牌。例如,如果输入为:

string input = "test!!";

调用 Tokenize(input, "!") 将返回三个标记:

test
!
!

如果要求两个相邻分隔符之间应有一个空标记,则 if (currentIdx > startIdx) 条件应该被删除。

This would be a purely procedural solution:

private static IEnumerable<string> Tokenize(string text, string separators)
{
    int startIdx = 0;
    int currentIdx = 0;

    while (currentIdx < text.Length)
    {
        // found a separator?
        if (separators.Contains(text[currentIdx]))
        {
            // yield a substring, if it's not empty
            if (currentIdx > startIdx)
                yield return text.Substring(startIdx, currentIdx - startIdx);

            // yield the separator
            yield return text.Substring(currentIdx, 1);

            // mark the beginning of the next token
            startIdx = currentIdx + 1;
        }

        currentIdx++;
    }
}

Note that this solution avoids returning empty tokens. For example, if the input is:

string input = "test!!";

calling Tokenize(input, "!") will return three tokens:

test
!
!

If the requirement is that two adjacent separators should have an empty token between them, then the if (currentIdx > startIdx) condition should be removed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文