0

我有以下字符串:

string myString = " 测试目标。\vVision\v* 交付测试目标\v** 综合\v** 控制\v* 与跨股票策略保持一致\vApproach\v* 加速"

我正在尝试拆分“\v”

我试过这个,但它似乎不起作用:

char[] delimiters = new char[] { '\v' };
string[] split = myString.Split(delimiters);
for (int i = 0; i < split.Length; i++) {
} 

split.Length显示为 1。有什么建议吗?

4

4 回答 4

5

"\v" is two characters, not one, in your original string (which is not counting the \ as an escape character as a literal C# string does).

You need to be splitting on literal "\v" which means you need to specify the overload of Split that takes a string:

string[] split = narrative.Split(new string[] {"\\v"}, StringSplitOptions.None);

Note how I had to escape the "\" character with "\\"

Your '\v' is a single control character, not two characters.

I think your question itself is slightly misleading...

Your example string, if entered into C# will actually work like you expected, because a \v in a verbatum C# string will be escaped to a special character:

string test = " The objective for test.\vVision\v* Deliver a test goals\v** Comprehensive\v** Control\v* Alignment with cross-Equities strategy\vApproach\v*An acceleration ";

char[] delimiters = new char[] { '\v' };
Console.WriteLine(test.Split(delimiters).Length); // Prints 8

However, I think your actual string really does have backslash-v in it rather than escaped \v:

string test = " The objective for test.\\vVision\\v* Deliver a test goals\\v** Comprehensive\\v** Control\\v* Alignment with cross-Equities strategy\\vApproach\\v*An acceleration ";

char[] delimiters = new char[] { '\v' };
Console.WriteLine(test.Split(delimiters).Length); // Prints 1, like you say you see.

So you can fix it as described above by using an array of strings to split the string:

string test = " The objective for test.\\vVision\\v* Deliver a test goals\\v** Comprehensive\\v** Control\\v* Alignment with cross-Equities strategy\\vApproach\\v*An acceleration ";

string[] delimiters = new [] { "\\v" };
Console.WriteLine(test.Split(delimiters, StringSplitOptions.None).Length); // Prints 8
于 2013-07-06T19:27:19.757 回答
0

Use something like this

string[] separators = {@"\v"};
      string value = @"The objective for test.\vVision\v* Deliver a test goals\v** Comprehensive\v** Control\v* Alignment with cross-Equities strategy\vApproach\v*An acceleration";
      string[] words = value.Split(separators, StringSplitOptions.RemoveEmptyEntries);
于 2013-07-06T19:25:29.363 回答
-1

像这样使用,

string myString = " The objective for test.\vVision\v* Deliver a test goals\v** Comprehensive\v** Control\v* Alignment with cross-Equities strategy\vApproach\v*An acceleration";
string[] delimiters = new string[] { "\v" };
string[] split = myString.Split(delimiters, StringSplitOptions.None);
for (int i = 1; i < split.Length; i++)
{
}
于 2013-07-06T19:31:29.223 回答
-1

如果您不需要生成的数组供以后使用,您可以将拆分和循环组合到一个调用中。

foreach (string s in myString.Split(new[] { "\v" }, StringSplitOptions.RemoveEmptyEntries))
{
   //s is the string you can now use in your loop
}
于 2013-07-06T19:32:26.967 回答