我显然在这里遗漏了一些东西..
我正在编写一个函数,它返回由特定字符串分隔的子字符串的数量。这是相当简单的功能 -
public static FuncError DCount(String v1, String v2, ref Int32 result) {
result = 0;
if (String.IsNullOrEmpty(v1)) {
return null;
}
if (String.IsNullOrEmpty(v2)) {
return null;
}
int ct = 1;
int ix = 0;
int nix = 0;
do {
nix = v1.IndexOf(v2, ix);
if (nix >= 0) {
ct++;
System.Diagnostics.Debug.Print(
string.Format("{0} found at {1} count={2} result = {3}",
v2, nix, ct, v1.Substring(nix,1)));
ix = nix + v2.Length;
}
} while (nix >= 0);
result = ct;
return null;
}
当我使用在特定情况下用作分隔符的特殊字符进行调用时,问题就出现了。它返回了很多误报。从 Debug.Print 开始,第一个和最后一个参数应该始终相同。
þ found at 105 count=2 result = t
þ found at 136 count=3 result = t
þ found at 152 count=4 result = þ
þ found at 249 count=5 result = t
þ found at 265 count=6 result = t
þ found at 287 count=7 result = t
þ found at 317 count=8 result = t
þ found at 333 count=9 result = þ
þ found at 443 count=10 result = þ
þ found at 553 count=11 result = þ
þ found at 663 count=12 result = þ
þ found at 773 count=13 result = þ
þ found at 883 count=14 result = þ
þ found at 993 count=15 result = þ
如果我将 þ 作为字符传递,它可以正常工作。如果我使用 þ 作为分隔符拆分字符串,它将返回正确数量的元素。至于错误识别的't',结果中还有其他't'没有被拾取,所以不是字符转换问题。
使困惑 ...
谢谢