我在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#中编写上述代码。
我在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#中编写上述代码。
我相信你正在寻找 string.Split 函数:
string str = "Test string" + (char)13 + "some other string";
string[] splitted = str.Split((char)13);
或者您可以使用:
string[] splitted = str.Split('\r');
splitted
数组中获得两个字符串。sSplit2 = Split(g, Chr(32))
的等效代码是:
string[] sSplit2 = g.Split('\n');
int index = sourceStr.IndexOf((char)13);
String[] splittArr = sourceStr.Split((char)13);
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);