How do I know if one string contains another? [duplicate]

-1

I have List<string> with four items:

C:\Users\Producao\OneDrive\VisualStudio2017\siteRelatorios\bpa\Content\Upload\LFCES004.txtb9109712-d3f2-4151-bbac-7fbc82ed99de
C:\Users\Producao\OneDrive\VisualStudio2017\siteRelatorios\bpa\Content\Upload\LFCES018.txteeba927c-47c9-41ff-9643-4a0556244b26
C:\Users\Producao\OneDrive\VisualStudio2017\siteRelatorios\bpa\Content\Upload\LFCES021.txt84a0effb-a3a3-4790-a8e4-62d80e23b0ad
C:\Users\Producao\OneDrive\VisualStudio2017\siteRelatorios\bpa\Content\Upload\LFCES037.txta1189537-b589-4161-9ce6-f62b2aecbab9 

Each line of this is a .txt

I want to know about them, which line has the word LFCES004 , LFCES018 , LFCES021 and LFCES037

Eg: if (linha1 contém 'LFCES004')

    
asked by anonymous 09.08.2017 / 20:32

3 answers

3

To check if a string is contained in the other, just use Contains :

if (linha1.Contains("LFCES004"))
    
09.08.2017 / 20:34
4

The String class has an instance method called Contains that meets your need.

The method receives a string, and indicates whether the given string is a substring of the instance in which it was called.

i.e.:

string foo = "abc", bar = "ab", ni = "ad";

foo.Contains(bar); // o retorno será true
foo.Contains(ni); // o retorno será false

Note that a string is always substring of itself.

    
09.08.2017 / 20:34
2

Just use Contains

var Value1 = "ddabcgghh";

if (Value1.Contains("abc"))
{
    [..]
}

To check with a List try the following:

foreach(string item in minhaLista)
{
  if(item.Contains("ABCABC"))
       return item;
}
    
09.08.2017 / 20:35