Soheil Mamdouhi
Soheil Mamdouhi

Reputation: 31

Get a part of string in C#

I am trying to get a part of an string in C#
My string is:

svn://mcdssrv/repos/currecnt/class/MBackingBean.java

and I want this part of string:

svn://mcdssrv/repos/currecnt/class/

How can I get the above?

Upvotes: 0

Views: 130

Answers (3)

Amit Joki
Amit Joki

Reputation: 59292

Do this with String.Remove()

string s = "svn://mcdssrv/repos/currecnt/class/MBackingBean.java";
Console.WriteLine(s.Remove(s.LastIndexOf('/'))+"/");

DEMO

Upvotes: 1

Ulugbek Umirov
Ulugbek Umirov

Reputation: 12807

One more way to do that:

string str = "svn://mcdssrv/repos/currecnt/class/MBackingBean.java";
string s = Regex.Match(str, ".*/").Value;

Upvotes: 1

Mohammad Arshad Alam
Mohammad Arshad Alam

Reputation: 9862

Try this with String.Substring:

string str = "svn://mcdssrv/repos/currecnt/class/MBackingBean.java";
str = str.Substring(0,str.LastIndexOf('/')+1);

Upvotes: 2

Related Questions