StartsWith方法C#不返回TRUE

vik*_*for 13 c# string startswith

我从MS SQL数据库中读取了一些值,我喜欢对字符串进行一些操作.这是我用来检查某些字符串是否以另一个字符串开头的代码:

String input = "????????? j???? ????????????? ??????? ??????? ??????? ?????? ?? (59) ?? ?????? ?????? ???????? ?????? ?? ???????? ???? ?????? ???????? ???? „????? ???? ?????? ??????????? ?? ?????? ? ?????? ?? ???????????“ ? „??????????? ????? ?? ???? ????????? ?? ??????????? ???????“";
String subString = "????????? ????? ?????????????";
if (input.StartsWith(subString))
{
    Response.Write("OK");
}
Run Code Online (Sandbox Code Playgroud)

但是input.StartsWith(subString)不会返回true.有人知道为什么吗?

das*_*ght 24

不同之处在于j位置10 中的字符:输入中的代码为106,但在子字符串中为1112(0x458参见演示).

你的第二个j来自Unicode第4页

?   1112    458 0xD1 0x98   CYRILLIC SMALL LETTER JE
Run Code Online (Sandbox Code Playgroud)

它看起来一样,但有不同的代码.

重新键入jsubstring修复了这个问题.


小智 10

输入和subString中的第二个单词不匹配.将字符串放在notepad ++中并一次选择每个单词.subString中的第一个和最后一个单词匹配但不匹配中间单词.

此示例演示了此问题:

void Main()
{
    var test = "????????? ????? ?????????????";
    var tost = "????????? j???? ?????????????";

    for(var i = 0; i < test.Length; i++){
        Console.WriteLine(string.Format("1: {0}, 2: {1}, Equal: {2}", test[i], tost[i], test[i] == tost[i]));
        if(test[i] != tost[i]){ Console.WriteLine (string.Format("1: {0}, 2: {1}", (int) test[i], (int) tost[i])); }
    }

    Console.WriteLine (test == tost);
}
Run Code Online (Sandbox Code Playgroud)

相关产出:

1: ?, 2: j, Equal: False
1: 1112, 2: 106
Run Code Online (Sandbox Code Playgroud)


Son*_*nül 6

您发布的字符串不相等.做这个:

string s1 = "????????? ????? ?????????????";
string s2 = "????????? j???? ?????????????";
var bt = Encoding.UTF8.GetBytes(s1);
var bt_1 = Encoding.UTF8.GetBytes(s2);
Run Code Online (Sandbox Code Playgroud)

输出将类似于以下内容:

56
55
Run Code Online (Sandbox Code Playgroud)

实际差异如下.第一个字符串中的"j"是:

[19]    209 byte
[20]    152 byte
Run Code Online (Sandbox Code Playgroud)

而第二个字符串中的"j"是:

[19]    106 byte
Run Code Online (Sandbox Code Playgroud)

第一个?0xD1 0x98十六进制代码表示,第二个j0x6A十六进制代码表示.