1

I have the following string below that is causing me a lot of grief.

"John","\Jane","\Jerry"

The declaration in Visual C++ for the above string is as follows:

String ^mynames="John\",\"\\\Jane\",\"\\Jerry";

The problem is I am not able to split each name by the characters ","\. I need to store the result in an array:

array<String^>^ data_line;

But the following line doesn't work

data_line=mynames->Split('\",\"\\\');
4

2 回答 2

1

那么这是 Visual C++ CLI/Managed C++ 中的一个有效解决方案。

split 函数不接受直接根据用户输入的一组字符来拆分字符串。为了解决这个问题,需要声明一个 String 数组并将其传递给 split 函数。有点愚蠢和乏味,但这是一个工作代码示例。

String ^mynames="John\",\"\\\Jane\",\"\\Jerry";
array<String^>^ data_line;
array<String^>^ stringtocompare = gcnew array<String^>(1);  //Declare an array to do the comparison
stringtocompare[0]="\",\"\\";       //Compare against the character sequence ","\
data_line=mynames->Split(stringtocompare, StringSplitOptions::None);
于 2013-08-14T11:57:30.143 回答
1

如果您没有收到编译器错误,您的代码将恢复为String.Split(Char[]), 并接受", ,,"\作为分隔符。

您需要","\用作分隔符,因此请使用:

data_line=test->Split(new string[] {"\",\"\\\"}, StringSplitOptions.None);
于 2013-08-14T09:48:17.027 回答