How do I know if one string contains another?

Asked

Viewed 590 times

-1

I have a 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 

Every line in this is a file .txt

I want to know what line has the word LFCES004, LFCES018, LFCES021 and LFCES037

Ex.: if (linha1 contém 'LFCES004')

  • 1

    Guy only use . Contains(value)

  • 1

    I did not understand the negative votes or the closing vote for the question "not being clear enough". I think a proof of text interpretation should be a prerequisite for giving that kind of closing vote

  • I also didn’t understand @Renan, but do what right?

  • It was pretty obvious that a simple question like that would already have several answers, and it has even more.

  • is that I searched for one string inside another, I did not search by comparing strings, since my thinking was not that

3 answers

4

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

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

i and..:

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 itself.

4


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

if (linha1.Contains("LFCES004"))

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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.