简体   繁体   English

查找字符串在字符串中的位置

[英]Finding where a string is in a string

So I have a string for example 0010101101010101010101010111100010101010011010 and I have this string 111000 I want to find out where this string is in the string for example, 111000 would be around 26 numbers in. 所以我有一个字符串,例如001010110101010101010101011111110110101010011010,我有这个字符串111000,我想找出这个字符串在字符串中的位置,例如111000大约是26个数字。

0010101101010101010101010111100010101010011010 0010101101010101010101010111100010101010011010

-------------------------------------------111000 ------------------------------------------- 111000

                         ^26

I would then take that 26 and do something with it 然后我会拿那26来做点什么

Do I need to do the string searching manually, by taking the first leter off and doing string.startswith(111000) or is there a way to find out like a built in method? 我是否需要通过删除第一个leter并执行string.startswith(111000)来手动进行字符串搜索,或者有没有办法像内置方法那样进行查找? regex perhaps? 正则表达式? string builder? 字符串生成器?

string longString = "the stirng with zeros and ones";
string searchString = "111000";
int startIndex = longString.IndexOf(searchString);

If you apply this to your examples, startIndex will be 25 as it is zero based. 如果将其应用于示例,则startIndex将为25,因为它是基于零的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM