首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我的String.EndsWith用法有什么问题?

我的String.EndsWith用法有什么问题?
EN

Stack Overflow用户
提问于 2019-03-07 22:30:06
回答 3查看 103关注 0票数 1

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

我有C#代码:

代码语言:javascript
复制
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());

输出为:

代码语言:javascript
复制
heading = yakobusho
test = True
back = False
front = True
other = True

EndsWith是怎么回事?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-03-07 22:42:48

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

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

代码语言:javascript
复制
"\x200Bsho"

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

票数 1
EN

Stack Overflow用户

发布于 2019-03-07 22:35:55

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

代码语言:javascript
复制
bool back = heading.EndsWith("​sho");

更正后的行:

代码语言:javascript
复制
bool back = heading.EndsWith("sho");
票数 4
EN

Stack Overflow用户

发布于 2019-03-07 22:46:16

EndsWith参数中有一个特殊字符。

正如您从这段代码中看到的:

代码语言:javascript
复制
  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);

    }
  }

输出:

代码语言:javascript
复制
back = False
yourText length = 4
backNew = True
newText length = 3

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

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55046203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档