user1765862
user1765862

Reputation: 14165

read file name and find particular string

I have filename like

ChrysanthemumThumbnail82b09296a7c34373833050d362745a30.jpg 

How can I find this string ChrysanthemumThumbnail

Upvotes: 2

Views: 569

Answers (3)

Habib
Habib

Reputation: 223292

You can use Path.GetFileNameWithoutExtension method to read the file name without extension and then you can use string.Contains to check if it contains your string.

string fileName = Path.GetFileNameWithoutExtension("ChrysanthemumThumbnail82b09296a7c34373833050d362745a30.jpg");
if(fileName.Contains("ChrysanthemumThumbnail"))
{
  //name found
}

if you want to see if the fileName starts with your string then you can use string.StartsWith

if(fileName.StartsWith("ChrysanthemumThumbnail"))
{
 //file name starts with ChrysanthemumThumbnail
}

(if you have the FileName already in a string then you don't need Path.GetFileNameWithoutExtension it is only useful if you are trying to extract the filename from a path)

Upvotes: 3

TalentTuner
TalentTuner

Reputation: 17556

if the ChrysanthemumThumbnail always at start of the filename then just use 'StartWith' , this will have better over contains which checks whole string

"ChrysanthemumThumbnail82b09296a7c34373833050d362745a30.jpg".StartsWith("ChrysanthemumThumbnail") 

and if it can be at any location than use contains as suggested by Habib above

"ChrysanthemumThumbnail82b09296a7c34373833050d362745a30.jpg".Contains ("ChrysanthemumThumbnail")

Upvotes: 1

GJHix
GJHix

Reputation: 522

Did you want something like:

string filename = "ChrysanthemumThumbnail82b09296a7c34373833050d362745a30.jpg"
bool result = filename.Contains("ChrysanthemumThumbnail");

Upvotes: 1

Related Questions