Solved

Trim string C#

Posted on 2011-02-27
18
318 Views
Last Modified: 2012-05-11
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
Comment
Question by:wint100
  • 8
  • 6
  • 3
  • +1
18 Comments
 
LVL 3

Expert Comment

by:sergiobg57
ID: 34992168
Just like it?


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

Open in new window

0
 
LVL 1

Author Comment

by:wint100
ID: 34992178
It could be anything before the /Graphics....

So, I need to trim anything before /Graphics.... no matter what comes before it.
0
 
LVL 1

Author Comment

by:wint100
ID: 34992183
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34992191
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
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34992198
Oh!

I have seen latest comments just now
Raj
0
 
LVL 3

Expert Comment

by:sergiobg57
ID: 34992204
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
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 34992211
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
 
LVL 3

Expert Comment

by:sergiobg57
ID: 34992214
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
 
LVL 1

Author Comment

by:wint100
ID: 34992215
Fernando, what would be the output there?
0
 
LVL 3

Accepted Solution

by:
sergiobg57 earned 250 total points
ID: 34992223
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
 
LVL 3

Expert Comment

by:sergiobg57
ID: 34992228
Oh, i didn't read the name fernando nor his comment. =p
Forget the comment above.
0
 
LVL 1

Author Comment

by:wint100
ID: 34992244
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
 
LVL 23

Assisted Solution

by:Rajkumar Gs
Rajkumar Gs earned 250 total points
ID: 34992245
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
 
LVL 3

Expert Comment

by:sergiobg57
ID: 34992256
Yeah, it will cut everything before /graphics/ and let the /graphics/ and whatever is after in the string.
0
 
LVL 1

Author Comment

by:wint100
ID: 34992261
I think I'll have to split points between Segio and Raj, is this OK?
0
 
LVL 3

Expert Comment

by:sergiobg57
ID: 34992273
I've made a very simple test for you to see.

 test
0
 
LVL 1

Author Comment

by:wint100
ID: 34992275
Also, I've seen conflicting Index, do I need IndexOf("/graphics")); or IndexOf("/graphics/")); (with the extra /)?

0
 
LVL 3

Expert Comment

by:sergiobg57
ID: 34992296
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question