質問

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 ?

役に立ちましたか?

解決

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

他のヒント

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.

This should do it

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

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);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top