Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to trim words before '/'
This is my input:
string Path = "D:/new/image.jpg")
My out will be:
string path=image.jpg
Posted

You should not trim anything. You are extracting part of the path; there is .NET FCL API for that. This is what you need:
https://msdn.microsoft.com/en-us/library/system.io.path.getfilename(v=vs.110).aspx[^].

—SA
 
Share this answer
 
Comments
Abhinav S 14-Apr-15 11:53am    
5.
Sergey Alexandrovich Kryukov 14-Apr-15 12:02pm    
Thank you, Abhinav.
—SA
Hi,

C#
string path = "D:/new/image.jpg";
int pos = path.LastIndexOf("/") + 1;
Console.WriteLine(path.Substring(pos, path.Length - pos)); // prints "image.jpg"
 
Share this answer
 
You will not trim here. Trim removes whitespaces and other characters.

You will use string.indexOf and string.substring.
Alternatively, you can use string.split.

Here are some examples -
IndexOf[^]
SubString[^]
Split[^]
 
Share this answer
 
v2
Comments
RENJITH VS 14-Apr-15 5:47am    
Thanks Abhinav
try this
C#
string Path = "D:/new/image.jpg";
string[] tempArr=Path.Split('/');
string path = tempArr[tempArr.Length - 1].ToString();

happy coding

good luck ;-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900