-2
string name = "BLK000012345summary.pdf";
string fileName = "20150929111111zp2zq23BLK000012345summary.pdf";

I have a string name which I have to find in fileName, if it exists it should return true otherwise false.

ASh
  • 34,632
  • 9
  • 60
  • 82
user3106445
  • 341
  • 1
  • 4
  • 13

3 Answers3

2
string name = "BLK000012345summary.pdf";
string fileName = "20150929111111zp2zq23BLK000012345summary.pdf";

bool value = fileName.Contains(name);
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Nalaka
  • 1,165
  • 7
  • 12
2

A simple string.Contains will do the job:

bool contains = fileName.Contains(name);

It will check if the fileName contains name and will return true if so. Note that Contains will match case-sensitive. It has to match exactly. If you want to match case insensitive, a regular expression or this post might be helpful, which tells you to use the current culture to do an IndexOf.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 1
    @user3106445 this is the only answer i would even upvote to such a trivial question and the others without any comment / explaination gets accepted. i would accept this answer because SQ should force high quality answers and this one has most of it – fubo Sep 30 '15 at 07:45
  • @fubo It doesn't really matter to me, but thanks for your support. – Patrick Hofman Sep 30 '15 at 07:48
0

Try this

fileName.Contains(name);
Sateesh Pagolu
  • 9,282
  • 2
  • 30
  • 48