Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cut a string

Tags:

c#

I have a strings like "/LM/W3SVC/1216172363/root/175_Jahre_STAEDTLER_Faszination_Schreiben"

And I need only "175_Jahre_STAEDTLER_Faszination_Schreiben" where "root" is separator. How can I do this?

like image 592
Juri Bogdanov Avatar asked Jul 26 '10 20:07

Juri Bogdanov


4 Answers

"/LM/W3SVC/1216172363/root/175_Jahre_STAEDTLER_Faszination_Schreiben".Split("/root/")[1] should give you "175_Jahre_STAEDTLER_Faszination_Schreiben"

like image 115
NickAldwin Avatar answered Oct 21 '22 07:10

NickAldwin


Another method:

String newstring = file_path.Substring(file_path.LastIndexOf('/') + 1);
like image 28
krs1 Avatar answered Oct 21 '22 06:10

krs1


Check out the System.IO.Path methods - not quite files and folders but with the / delimiter it just might work!

like image 29
n8wrl Avatar answered Oct 21 '22 07:10

n8wrl


If you're looking to extract a part of a string based on an overall pattern, regular expressions can be a good alternative in some situations.

string s = "/LM/W3SVC/1216172363/root/175_Jahre_STAEDTLER_Faszination_Schreiben";
Regex re = new Regex(@"/root/(?<goodPart>\w+)$");
Match m = re.Match(s);
if (m.Success) {
    return m.Groups["goodPart"].ToString();
}
like image 42
John M Gant Avatar answered Oct 21 '22 06:10

John M Gant