Connecting Tech Pros Worldwide Forums | Help | Site Map

how to parse to string

Member
 
Join Date: Nov 2008
Posts: 46
#1: Nov 21 '08
hallo,

I have a url string like this "http://www.cwe-projects.eu/bscw/bscw.cgi/695646". how can find final slash and retreive only the no ?.. (i mean the no 695646).

thank you.

Dinesh.

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Nov 21 '08

re: how to parse to string


Several options. One natural one is to read the thing from the end and stop when you hit the first /. Methods in the String class will of particular interest to you here.
PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 702
#3: Nov 21 '08

re: how to parse to string


take a look at this:
Expand|Select|Wrap|Line Numbers
  1. string sss = @"http://www.cwe-projects.eu/bscw/bscw.cgi/695646";
  2.                 int i = sss.LastIndexOf(@"/");
  3.                 string g = sss.Substring(i+1, sss.Length-(i+1));
  4.  
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#4: Nov 21 '08

re: how to parse to string


You could also consider using the Uri object, it handles the parsing of http:// links like that. Then you can simply pick out the piece(s) that you want
balabaster's Avatar
Moderator
 
Join Date: Mar 2007
Location: Canada
Posts: 757
#5: Nov 21 '08

re: how to parse to string


Or you could use Regular Expressions (Add the using/imports to System.Text.RegularExpressions)

Expand|Select|Wrap|Line Numbers
  1. Dim Match As String = Regex.Match(MyUrlString, "(?<=\/)[^\/]+(?=$)").Value
It's not necessarily as easy to read as the SubString method, and may not be quite as intuitive as the URI method suggested by the previous guys, but it's tidy and simple enough.

There's many ways to skin this cat - it's really down to a matter of your coding style, performance and ease of maintenance. It's really your call on which method you choose to complete this task.
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#6: Nov 21 '08

re: how to parse to string


One more option includes the String.Split method, which will break your string up into an array of strings around a set delimiter, in your case: the '/' char. Then just use the last string in the array.

So, yeah....you have a lot of options ;)

This MSDN link may be of interest to you.
balabaster's Avatar
Moderator
 
Join Date: Mar 2007
Location: Canada
Posts: 757
#7: Nov 21 '08

re: how to parse to string


Quote:

Originally Posted by DeepBlue

take a look at this:

Expand|Select|Wrap|Line Numbers
  1. string sss = @"http://www.cwe-projects.eu/bscw/bscw.cgi/695646";
  2.                 int i = sss.LastIndexOf(@"/");
  3.                 string g = sss.Substring(i+1, sss.Length-(i+1));
  4.  

When you're after the substring at the end of a string, you can omit the length parameter:

Expand|Select|Wrap|Line Numbers
  1. Dim sIn As String = _
  2.     "http://www.cwe-projects.eu/bscw/bscw.cgi/695646"
  3.  
  4. Dim sOut As String = sIn.Substring(sIn.LastIndexOf("/") + 1)
Reply