Click to See Complete Forum and Search --> : Return Right characters of String up to a /
chabian
February 20th, 2010, 08:23 AM
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,
memeloo
February 20th, 2010, 01:33 PM
use the String.LastIndexOf method + String.Substring or the String.Split method and get the last item from the returned array.
@kimlundgren_
February 21st, 2010, 01:08 AM
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];
}
}
memeloo
February 21st, 2010, 01:14 AM
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 :lol:
what about the code tags? don't you like them?
@kimlundgren_
February 21st, 2010, 04:15 AM
not bad :lol:
what about the code tags? don't you like them?
Thx :)
what code tags? Im really new here :P started my acc last night.
TheGreatCthulhu
February 21st, 2010, 08:28 AM
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...
The 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.
@kimlundgren_
February 21st, 2010, 02:09 PM
Here I'm just using the HTML tags to make the CODE tags visible...
The 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 :D
and yes your defenitly right on that, mine wasn´t the best solution. next time Im in this situation I shall try String.Split().
rliq
February 21st, 2010, 06:42 PM
Incidentally, this is an opportunity to write some extensions (.Net 3.x+) to the String class. So you can do more readable things like:
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:
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.
boudino
February 22nd, 2010, 07:43 AM
You can use regular expressions:
string s = "http://www.mydomain.com/abc";
Match m = Regex.Match(s, ".*/(.*)$");
string s = m.Groups[1].Value // 0 is whole string
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.