• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

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
0
wint100
Asked:
wint100
  • 8
  • 6
  • 3
  • +1
2 Solutions
 
sergiobg57Commented:
Just like it?


sUrl = "http://server.domain.com/Web/graphics/page.asp";
            MessageBox.Show(sUrl.Substring(28));

Open in new window

0
 
wint100Author Commented:
It could be anything before the /Graphics....

So, I need to trim anything before /Graphics.... no matter what comes before it.
0
 
wint100Author Commented:
And also return anything that may come after it. /Graphics is the key, I just need any characters starting with /Graphics/anything/anything/anypage.asp
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Rajkumar GsSoftware EngineerCommented:
Function to reverse string
    public static string Reverse(string s) 
    { 
        char[] charArray = s.ToCharArray(); 
        Array.Reverse(charArray); 
        return new string(charArray); 
    }

Open in new window

Main code
string 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]);

Open in new window


Raj
0
 
Rajkumar GsSoftware EngineerCommented:
Oh!

I have seen latest comments just now
Raj
0
 
sergiobg57Commented:
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));

Open in new window

0
 
Fernando SotoRetiredCommented:
Hi wint100;

Something like this should work.

string testStr = "http://server.domain.com/Web/graphics/page.asp";

testStr = System.Text.RegularExpressions.Regex.Match( testStr, @".*/(.+/.+\.asp)" ).Groups[ 1 ].Value;

MessageBox.Show( testStr );


Fernando
0
 
sergiobg57Commented:
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.


String sUrl = "http://server.domain.com/Web/graphics/page.asp";
sUrl = sUrl.Remove(0, sUrl.IndexOf("/graphics"));

Open in new window

0
 
wint100Author Commented:
Fernando, what would be the output there?
0
 
sergiobg57Commented:
My code?
It would be the one you requested.

/graphics/page.asp

Open in new window


See for yourself.
 
String sUrl = "http://server.domain.com/Web/graphics/page.asp";
sUrl = sUrl.Remove(0, sUrl.IndexOf("/graphics"));
MessageBox.Show(sUrl);

Open in new window

0
 
sergiobg57Commented:
Oh, i didn't read the name fernando nor his comment. =p
Forget the comment above.
0
 
wint100Author Commented:
sergio, I think you nailed it, that seems very simple and will give anything after and including /Graphics, right?

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); 

Open in new window


I'd output - "graphics/folder/folder/page.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.
0
 
Rajkumar GsSoftware EngineerCommented:
string input = "http://server.domain.com/Web/graphics/test/testpage/page.asp";
string output = input.Substring(input.IndexOf("/graphics/"), input.Length - input.IndexOf("/graphics/"));

Open in new window


I have tested it various inputs
http://server.domain.com/Web/graphics/page.asp
http://server.domain.com/Web/graphics/test/page.asp
http://server.domain.com/Web/graphics/test/testpage/page.asp

Hope this helps
Raj
0
 
sergiobg57Commented:
Yeah, it will cut everything before /graphics/ and let the /graphics/ and whatever is after in the string.
0
 
wint100Author Commented:
I think I'll have to split points between Segio and Raj, is this OK?
0
 
sergiobg57Commented:
I've made a very simple test for you to see.

 test
0
 
wint100Author Commented:
Also, I've seen conflicting Index, do I need IndexOf("/graphics")); or IndexOf("/graphics/")); (with the extra /)?

0
 
sergiobg57Commented:
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/" .
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 8
  • 6
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now