|
-
February 20th, 2010, 09:23 AM
#1
Return Right characters of String up to a /
I am trying to parse the following to get all characters to the right of the last "/". Can someone help me?
http://www.mydomain.com/abc
would return 'abc'
http://www.mydomain.com/abc/MyOtherFolder/Thg3kd
Would return 'Thg3kd'
Thanks,
-
February 20th, 2010, 02:33 PM
#2
Re: Return Right characters of String up to a /
use the String.LastIndexOf method + String.Substring or the String.Split method and get the last item from the returned array.
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback  you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
February 21st, 2010, 02:08 AM
#3
Re: Return Right characters of String up to a /
string originalString = "http://www.mydomain.com/abc/MyOtherFolder/Thg3kd";
string newString = "";
int length = originalString.Length;
for (int i = 0; i < length; i++)
{
if (originalString[i] == '/')
{
newString = "";
}
else
{
newString = newString + originalString[i];
}
}
-
February 21st, 2010, 02:14 AM
#4
Re: Return Right characters of String up to a /
 Originally Posted by @kimlundgren_
string originalString = "http://www.mydomain.com/abc/MyOtherFolder/Thg3kd";
string newString = "";
int length = originalString.Length;
for (int i = 0; i < length; i++)
{
if (originalString[i] == '/')
{
newString = "";
}
else
{
newString = newString + originalString[i];
}
}
not bad
what about the code tags? don't you like them?
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback  you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
February 21st, 2010, 05:15 AM
#5
Re: Return Right characters of String up to a /
 Originally Posted by memeloo
not bad
what about the code tags? don't you like them?
Thx 
what code tags? Im really new here :P started my acc last night.
-
February 21st, 2010, 09:28 AM
#6
Re: Return Right characters of String up to a /
 Originally Posted by @kimlundgren_
Thx 
what code tags? Im really new here :P started my acc last night.
Here I'm just using the HTML tags to make the CODE tags visible...
HTML Code:
The [CODE][/CODE] tags.
P.S. It's a nice solution - parsing style, but for what the OP wanted to do, I think String.Split() would do fine; don't get me wrong - what you suggested is perfectly valid, and, what's most important, it works, it's just that, in case that there is a lot of this data to be processed, a potential problem could come from the fact that the String class is immutable - every assignment operation creates a new instance which replaces the original instance. This is why, in such cases, it is recommended for the coder to use the StringBuilder class. Also, the speed could be impacted because the indexer returns a char, which must be converted to a string in each iteration.
-
February 21st, 2010, 03:09 PM
#7
Re: Return Right characters of String up to a /
 Originally Posted by TheGreatCthulhu
Here I'm just using the HTML tags to make the CODE tags visible...
HTML Code:
The [CODE][/CODE] tags.
P.S. It's a nice solution - parsing style, but for what the OP wanted to do, I think String.Split() would do fine; don't get me wrong - what you suggested is perfectly valid, and, what's most important, it works, it's just that, in case that there is a lot of this data to be processed, a potential problem could come from the fact that the String class is immutable - every assignment operation creates a new instance which replaces the original instance. This is why, in such cases, it is recommended for the coder to use the StringBuilder class. Also, the speed could be impacted because the indexer returns a char, which must be converted to a string in each iteration.
thx 
and yes your defenitly right on that, mine wasn´t the best solution. next time Im in this situation I shall try String.Split().
-
February 21st, 2010, 07:42 PM
#8
Re: Return Right characters of String up to a /
Incidentally, this is an opportunity to write some extensions (.Net 3.x+) to the String class. So you can do more readable things like:
Code:
String a = "This is some string of characters";
String b = a.Left(4); // This
String c = a.Mid(7, 11); // some string .... character 7 for 11 characters
String d = a.Right(10); // characters
If you write your own extensions you can overcome the short falls of the Substring(..) method (where it requires indices to actually be in the String), which can be confusing and lead to horrible looking code like:
Code:
String b = a.Substring(a.Length - 10, 10);
Which crashes if 'a' is less than 10 characters in length. Whereas using an extension method you can choose to return 'up to 10' characters etc.
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
February 22nd, 2010, 08:43 AM
#9
Re: Return Right characters of String up to a /
You can use regular expressions:
Code:
string s = "http://www.mydomain.com/abc";
Match m = Regex.Match(s, ".*/(.*)$");
string s = m.Groups[1].Value // 0 is whole string
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|