在文本中查找 Chr(13) 并在 C# 中拆分它们

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13677908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:25:17  来源:igfitidea点击:

Find Chr(13) in a text and split them in C#

c#

提问by dralialadin

I have written two lines of code below in vb6. The code is :

我在vb6中写了下面两行代码。代码是:

d = InStr(s, data1, Chr(13), 1) ' Fine 13 keycode(Enter) form a text data.

sSplit2 = Split(g, Chr(32))     ' Split with 13 Keycode(Enter)

But I can't write above code in C#. Please help me out. How can I write the above code in C#.

但是我不能用 C# 编写上面的代码。请帮帮我。如何在 C# 中编写上述代码。

采纳答案by Habib

I believe you are looking for string.Split:

我相信您正在寻找string.Split

string str = "Test string" + (char)13 + "some other string";
string[] splitted = str.Split((char)13);

Or you can use:

或者你可以使用:

string[] splitted = str.Split('\r');

For the above you will get two strings in your splittedarray.

对于上述内容,您将在splitted数组中获得两个字符串。

回答by John Woo

the equivalnt code for sSplit2 = Split(g, Chr(32))is

的等效代码sSplit2 = Split(g, Chr(32))

string[] sSplit2 = g.Split('\n');

回答by Frank59

int index = sourceStr.IndexOf((char)13);
String[] splittArr = sourceStr.Split((char)13);

回答by Daniel PP Cabral

        const char CarriageReturn = (char)13;
        string testString = "This is a test " + CarriageReturn + " string.";
        //find first occurence of CarriageReturn
        int index = testString.IndexOf(CarriageReturn);
        //split according to CarriageReturn
        string[] split = testString.Split(CarriageReturn);

If you want to encapsulate the carriage return depending on whether you are running in a unix or non unix environment you can use Environment.NewLine . See http://msdn.microsoft.com/en-us/library/system.environment.newline(v=vs.100).aspx.

如果你想根据你是在 unix 还是非 unix 环境中运行来封装回车,你可以使用 Environment.NewLine 。请参阅http://msdn.microsoft.com/en-us/library/system.environment.newline(v=vs.100).aspx

        string testString2 = "This is a test " + Environment.NewLine + " string.";
        //find first occurence of Environment.NewLine
        int index2 = testString2.IndexOf(Environment.NewLine);
        //split according to Environment.NewLine
        string[] split2 = testString2.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);