简体   繁体   English

将字符串转换为 c# 中的字符串数组

[英]Convert a string to an array of strings in c#

Can I get an array of string with CRLF or LF or CR included, only with linq?我能否仅使用 linq 获得包含 CRLF 或 LF 或 CR 的字符串数组? I don't want to use a loop.我不想使用循环。 I have a string like this "Hello \r\n Stackverflow\n How are you doing \r" , and i have to convert it to be like this我有一个像这样的字符串"Hello \r\n Stackverflow\n How are you doing \r" ,我必须把它转换成这样

mystring[0] = "Hello \r\n"
mystring[1] = "Stackverflow\n"
mystring[2] = "How are you doing \r"

Any ideas?有任何想法吗?

This is slightly complicated by the fact that we have to handle "\r", "\n" AND "\r\n".由于我们必须处理“\r”、“\n”和“\r\n”这一事实,这有点复杂。 I'm assuming you don't need to handle "\n\r" - if you do, you'll have to add a case for it in the code below.我假设你不需要处理 "\n\r" - 如果你这样做,你必须在下面的代码中为它添加一个案例。

Obviously there are many ways to solve this;显然有很多方法可以解决这个问题; here's a low-level approach that doesn't use anything other than a basic loop:这是一种低级方法,除了基本循环之外不使用任何东西:

public static IEnumerable<string> SplitByAndKeepLineSeparators(string input)
{
    if (input.Length == 0)
        yield break;

    int i = 0, j = 0;

    while (true)
    {
        if (i == input.Length - 1) // Last char?
        {
            yield return input.Substring(j, i - j + 1);
            break;
        }

        switch (input[i])
        {
            case '\r' when input[i+1] == '\n':
                yield return input.Substring(j, i - j + 2);
                i += 2;
                j =  i;
                break;

            case '\r':
                yield return input.Substring(j, i - j + 1);
                j = ++i;
                break;

            case '\n':
                yield return input.Substring(j, i - j + 1);
                j = ++i;
                break;

            default:
                ++i;
                break;
        }
    }
}

Note: If using an older version of C# you won't be able to use that kind of switch , so your code would have to be:注意:如果使用旧版本的 C# 您将无法使用这种switch ,因此您的代码必须是:

public static IEnumerable<string> SplitByAndKeepLineSeparators(string input)
{
    if (input.Length == 0)
        yield break;

    int i = 0, j = 0;

    while (true)
    {
        if (i == input.Length - 1) // Last char?
        {
            yield return input.Substring(j, i - j + 1);
            break;
        }

        if (input[i] == '\r' && input[i + 1] == '\n')
        {
            yield return input.Substring(j, i - j + 2);
            i += 2;
            j =  i;
        }
        else if (input[i] == '\r')
        {
            yield return input.Substring(j, i - j + 1);
            j = ++i;
        }
        else if (input[i] == '\n')
        {
            yield return input.Substring(j, i - j + 1);
            j = ++i;
        }
        else
        {
            ++i;
        }
    }
}

Try the below line please:-请尝试以下行:-

var arr = mystring.Split('\n');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM