wint100
asked on
Trim string C#
Hopefully a simple one. I'm trying to trim the following string, so I end up with the characters as shown:
Original string:
http://server.domain.com/Web/graphics/page.asp
Required string:
/graphics/page.asp
Original string:
http://server.domain.com/Web/graphics/page.asp
Required string:
/graphics/page.asp
ASKER
It could be anything before the /Graphics....
So, I need to trim anything before /Graphics.... no matter what comes before it.
So, I need to trim anything before /Graphics.... no matter what comes before it.
ASKER
And also return anything that may come after it. /Graphics is the key, I just need any characters starting with /Graphics/anything/anythin g/anypage. asp
Function to reverse string
Raj
public static string Reverse(string s)
{
char[] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
Main codestring input = "http://server.domain.com/Web/graphics/page.asp";
input = Reverse(input);
string[] temp = input.Split(Convert.ToChar("/"));
string output = Reverse(temp[0] + "/" + temp[1]);
Raj
Oh!
I have seen latest comments just now
Raj
I have seen latest comments just now
Raj
Well, the safest way is this then:
String sUrl, sIndex = "/graphics/";
sUrl = "http://server.domain.com/Web/graphics/page.asp";
sUrl = sUrl.Remove(0, sUrl.IndexOf(sIndex));
Hi wint100;
Something like this should work.
string testStr = "http://server.domain.com/Web/graphics/page.asp";
testStr = System.Text.RegularExpress ions.Regex .Match( testStr, @".*/(.+/.+\.asp)" ).Groups[ 1 ].Value;
MessageBox.Show( testStr );
Fernando
Something like this should work.
string testStr = "http://server.domain.com/Web/graphics/page.asp";
testStr = System.Text.RegularExpress
MessageBox.Show( testStr );
Fernando
RajkumarGS, don't worry.
I'm not here for points, i'm just here to practice my english. :)
Anyway, you can use just one string if you like.
I'm not here for points, i'm just here to practice my english. :)
Anyway, you can use just one string if you like.
String sUrl = "http://server.domain.com/Web/graphics/page.asp";
sUrl = sUrl.Remove(0, sUrl.IndexOf("/graphics"));
ASKER
Fernando, what would be the output there?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oh, i didn't read the name fernando nor his comment. =p
Forget the comment above.
Forget the comment above.
ASKER
sergio, I think you nailed it, that seems very simple and will give anything after and including /Graphics, right?
So If I had :
I'd output - "graphics/folder/folder/pa ge.asp"
Sorry, I'm not able to test any code at the moment, but if you say it will give the above output, that's good for me.
So If I had :
String sUrl = "http://server.domain.com/Web/graphics/folder/folder/page.asp";
sUrl = sUrl.Remove(0, sUrl.IndexOf("/graphics"));
MessageBox.Show(sUrl);
I'd output - "graphics/folder/folder/pa
Sorry, I'm not able to test any code at the moment, but if you say it will give the above output, that's good for me.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yeah, it will cut everything before /graphics/ and let the /graphics/ and whatever is after in the string.
ASKER
I think I'll have to split points between Segio and Raj, is this OK?
ASKER
Also, I've seen conflicting Index, do I need IndexOf("/graphics")); or IndexOf("/graphics/")); (with the extra /)?
It doesn't matter that much in fact.
IndexOf will return the position of the first char of the searched string.
If you use "/graphics" or "/graphics/" will do little difference
But for a matter of fact, it's better to use "/graphics/" as if you have a folder named "/graphics_something/" my code will interpret wrongly as /graphics/" .
IndexOf will return the position of the first char of the searched string.
If you use "/graphics" or "/graphics/" will do little difference
But for a matter of fact, it's better to use "/graphics/" as if you have a folder named "/graphics_something/" my code will interpret wrongly as /graphics/" .
Open in new window