5

我在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)

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

4

4 回答 4

8

我相信您正在寻找string.Split

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

或者您可以使用:

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

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

于 2012-12-03T05:58:50.093 回答
2

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

string[] sSplit2 = g.Split('\n');
于 2012-12-03T05:58:31.887 回答
1
int index = sourceStr.IndexOf((char)13);
String[] splittArr = sourceStr.Split((char)13);
于 2012-12-03T06:08:23.773 回答
1
        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);

如果您想根据您是在 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);
于 2012-12-03T06:21:51.317 回答