Esat IBIS
Esat IBIS

Reputation: 374

How Can I split A String from the end to some character I want

How can I split a string from the end to some character I want. Let me explain in example

"C:\Users\Esat\Desktop\BilimResimler\1620855_759701257391419_1132489417_n.jpg"

and I want to cut this part 1620855_759701257391419_1132489417_n.jpg but I have a lot of image and image names always changing so i can not use substring metod.So how can i do this ?

Upvotes: 1

Views: 69

Answers (5)

Dhaval Patel
Dhaval Patel

Reputation: 7591

You can also get your file name using below code.

var path = @"C:\Users\Esat\Desktop\BilimResimler\1620855_759701257391419_1132489417_n.jpg";
        string ImgPath = path.Substring(path.LastIndexOf(@"\") + 1);

Upvotes: 0

Alaa Mohammed
Alaa Mohammed

Reputation: 392

string FileName = Path.GetFileName(Path)

Upvotes: 0

NDJ
NDJ

Reputation: 5194

just to add to the answers - if this refers to a file that physically exists on disk, then why not let fileinfo do the work for you?

    var path = @"C:\Users\Esat\Desktop\BilimResimler\1620855_759701257391419_1132489417_n.jpg";

System.IO.FileInfo myImageFile = new System.IO.FileInfo(path);

Console.WriteLine(myImageFile.Name); // gives 1620855_759701257391419_1132489417_n.jpg

Upvotes: 2

user3373870
user3373870

Reputation: 1466

This should do it

string imageNameAndPath=@"C:\Users\Esat\Desktop\BilimResimler\1620855_759701257391419_1132489417_n.jpg"
    imageNameAndPath=imageNameAndPath.Substring(0, imageNameAndPath.LastIndexOf('/'));

Upvotes: 0

Dragos
Dragos

Reputation: 296

You can search for the last "\" character and eliminate everything from it, including him.

OR

From 0 to the index of the length of "C:\Users\Esat\Desktop\BilimResimler\" - 1 (37 - 1 if I counted correctly) keep the string and eliminate everything else.

Upvotes: 1

Related Questions