簡體   English   中英

如何找到字符串的子字符串的第一個字符的索引?

[英]How to find the index of the first char of a substring of string?

我現在有一個類似“ Helloworld”的字符串和一個類似“ world”的子字符串,我如何在主字符串中找到該子字符串的第一個字符的索引? 在這種情況下,我想要“ Helloworld”中的“ w”索引

有人可以幫我嗎?

我建議使用string.IndexOf ,但是特別要使用接受StringComparison枚舉的重載

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

將返回5

"Helloworld".IndexOf("world");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM