【问题标题】:How to find the index of the first char of a substring of string?如何找到字符串子串的第一个字符的索引?
【发布时间】:2014-02-04 23:21:59
【问题描述】:

我有像“Helloworld”这样的字符串 和一个像“世界”这样的子字符串 现在,我怎样才能找到主字符串中子字符串的第一个字符的索引? 在这种情况下,我想要“Helloworld”中“w”的索引

谁能帮帮我?

【问题讨论】:

  • 你考虑过...IndexOf("world")?
  • String.IndexOf 是谷歌中的第一个匹配项,如果我在那里输入你的标题。

标签: c# string substring


【解决方案1】:

我建议使用string.IndexOf,但特别是接受StringComparison枚举的重载

string test = "HelloWorld";  // World in uppercase 
int pos = test.IndexOf("world", StringComparison.CurrentCultureIgnoreCase);
Console.WriteLine(pos.ToString());

【讨论】:

  • 是的,这是最好的答案:)
【解决方案2】:
var index = "Helloworld".IndexOf("world");  

将返回 5

【讨论】:

    【解决方案3】:
    "Helloworld".IndexOf("world");
    

    【讨论】:

    • 感谢您的帮助我的朋友! :)
    • @Mehdi Np,顺便说一句,史蒂夫的答案应该是公认的,这就是这样做的方法。
    猜你喜欢
    • 1970-01-01
    • 2021-07-29
    • 2020-08-02
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 2020-02-14
    相关资源
    最近更新 更多