代码之家  ›  专栏  ›  技术社区  ›  jtsoftware

我使用字符串有什么问题吗。EndsWith?

  •  0
  • jtsoftware  · 技术社区  · 5 年前

    我不明白为什么EndsWith返回false。

    我有C代码:

    string heading = "yakobusho";
    bool test = (heading == "yakobusho");
    bool back = heading.EndsWith("​sho");
    bool front = heading.StartsWith("yak");
    bool other = "yakobusho".EndsWith("sho");
    Debug.WriteLine("heading = " + heading);
    Debug.WriteLine("test = " + test.ToString());
    Debug.WriteLine("back = " + back.ToString());
    Debug.WriteLine("front = " + front.ToString());
    Debug.WriteLine("other = " + other.ToString());
    

    heading = yakobusho
    test = True
    back = False
    front = True
    other = True
    

    0 回复  |  直到 5 年前
        1
  •  4
  •   Tor    5 年前

    在“sho”字符串之前包含一个不可见字符:

    bool back = heading.EndsWith("​sho");
    

    bool back = heading.EndsWith("sho");
    
        2
  •  1
  •   Panagiotis Kanavos    5 年前

    "​sho" 第三行中的字符串以 zero length space . "​sho".Length ((int)"​sho"[0]) 返回8203,零长度空间的Unicode值。

    您可以使用其十六进制代码将其键入字符串中,例如:

    "\x200Bsho"
    

    令人恼火的是,该字符不被视为空白,因此无法使用删除 String.Trim() .

        3
  •  0
  •   Dave    5 年前

    在你的 EndsWith

      class Program
      {
        static void Main(string[] args)
        {
          string heading = "yakobusho";
    
          string yourText = "​sho";
          bool back = heading.EndsWith(yourText);
    
          Debug.WriteLine("back = " + back.ToString());
          Debug.WriteLine("yourText length = " + yourText.Length);
    
          string newText = "sho";
          bool backNew = heading.EndsWith(newText);
    
          Debug.WriteLine("backNew = " + backNew.ToString());
          Debug.WriteLine("newText length = " + newText.Length);
    
        }
      }
    

    back = False
    yourText length = 4
    backNew = True
    newText length = 3
    

    长度 yourText 是4,所以这个字符串中有一些隐藏字符。

    希望这有帮助。

    推荐文章