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?

  • You can get guidance from this topic: [Split string by last occurrence][1] [1]: http://stackoverflow.com/questions/22630592/split-string-by-last-occurence – Gautam Jain Apr 05 '14 at 05:08

3 Answers3

2

Try this with String.Substring:

string str = "svn://mcdssrv/repos/currecnt/class/MBackingBean.java";
str = str.Substring(0,str.LastIndexOf('/')+1);
Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61
1

Do this with String.Remove()

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

DEMO

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
  • returned string will `"svn://mcdssrv/repos/currecnt/class"` and not `"svn://mcdssrv/repos/currecnt/class/"` – Chirag Apr 05 '14 at 05:33
  • change line `Console.WriteLine(s.Remove(s.LastIndexOf('/')+"/");` to `Console.WriteLine(s.Remove(s.LastIndexOf('/'))+"/");` – Chirag Apr 05 '14 at 05:40
1

One more way to do that:

string str = "svn://mcdssrv/repos/currecnt/class/MBackingBean.java";
string s = Regex.Match(str, ".*/").Value;
Ulugbek Umirov
  • 12,719
  • 3
  • 23
  • 31