0

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?

1

3 Answers 3

2

Try this with String.Substring:

string str = "svn://mcdssrv/repos/currecnt/class/MBackingBean.java";
str = str.Substring(0,str.LastIndexOf('/')+1);
Sign up to request clarification or add additional context in comments.

Comments

1

One more way to do that:

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

2 Comments

alternate yes, but unadvisable. Regex is ugly.
@JoshW Regex clearly shows your intents, you can understand immediately that you want everything until last / including it.
1

Do this with String.Remove()

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

DEMO

2 Comments

returned string will "svn://mcdssrv/repos/currecnt/class" and not "svn://mcdssrv/repos/currecnt/class/"
change line Console.WriteLine(s.Remove(s.LastIndexOf('/')+"/"); to Console.WriteLine(s.Remove(s.LastIndexOf('/'))+"/");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.