简体   繁体   English

尽管子字符串存在,但找不到c#substring

[英]c# substring not found although the substring exists

I have strings which look like 我有一些看起来像的字符串

string1 = "~01301~^~DATA1,DATA2 DATA3~^15.87^717^0.85^81.11^2.11^0.06^0" string1 =“~01301~ ^ ~DATA1,DATA2 DATA3~ ^ 15.87 ^ 717 ^ 0.85 ^ 81.11 ^ 2.11 ^ 0.06 ^ 0”

string2 = "~01341~^~DATA3,DATA2 DATA1 DATA4~^15.87^717^0.85^81.11^2.11^0.06^0" string2 =“~01341~ ^ ~DATA3,DATA2 DATA1 DATA4~ ^ 15.87 ^ 717 ^ 0.85 ^ 81.11 ^ 2.11 ^ 0.06 ^ 0”

string3 = "~01347~^~DATA1 DATA2,DATA3~^15.87^717^0.85^81.11^2.11^0.06^0" string3 =“~01347~ ^ ~DATA1 DATA2,DATA3~ ^ 15.87 ^ 717 ^ 0.85 ^ 81.11 ^ 2.11 ^ 0.06 ^ 0”

and so on. 等等。

Out of these strings, I need to find which strings contain let's say "DATA1" substring. 在这些字符串中,我需要找到哪些字符串包含让我们说“DATA1”子字符串。 In C#, contains - indexOf - lastIndexOf methods cannot find DATA1 in string1 but they all find DATA1 in string2 and string3. 在C#中,contains - indexOf - lastIndexOf方法在string1中找不到DATA1,但它们都在string2和string3中找到DATA1。

What could be the reason for this? 这可能是什么原因? First DATA1 is surrounded with tilde and comma but I guess those shouldn't affect or am I wrong? 第一个DATA1用波浪号和逗号包围,但我猜那些不应该影响或者我错了?

EDIT: The relevant part of the code is trivial, that's why I didn't post it. 编辑:代码的相关部分是微不足道的,这就是为什么我没有发布它。 But still here is the relevant part of the code: 但仍然是代码的相关部分:

while((line = isoFileReader.ReadLine())!=null)
{
    if (line.IndexOf(input)!=-1)
    {
        matchList.Add(line);
    }
}

or 要么

while((line = isoFileReader.ReadLine())!=null)
{
    if (line.Contains(input))
    {
        matchList.Add(line);
    }
}

Most likely an issue when you make the call. 拨打电话时很可能是个问题。 string1.Contains("DATA1"); string1.Contains( “DATA1”); will return true for the string you specified. 将为您指定的字符串返回true。

Contains is case-sensitive, so perhaps you've accidentally typed the wrong case of one of the letters, or added a space before/after. 包含区分大小写,因此您可能不小心输入了其中一个字母的错误大小写,或者在之前/之后添加了空格。

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

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