tags:

views:

56

answers:

1

Hi friends,

I has a button when the user clicks it then it must go to specified URL

But i have to create my URL out of the values coming from database and most important is i need to modify the values coming from database before i make a URL out of it

Suppose the values from database is

                                country- France

                                hotel - Hotel Movenpick

Now first i have to turn the capitals from above values to small then spaces to '-' sign

then i will have to create my URL with this modified values as below

http://www.travel.com/france/hotel-movenpick

I never done this before .please provide me some reference for doing this task.I was coding in c#

Thanks in advance

+1  A: 

How about:

string fixedCountry = country.ToLower(CultureInfo.InvariantCulture);
                             .Replace(" ", "-");
string fixedHotel = hotel.ToLower(CultureInfo.InvariantCulture)
                    .Replace(" ", "-");

string url = "http://www.travel.com/" + fixedCountry + "/" + fixedHotel;

Note that this won't fix up any accented characters or other symbols. It becomes more complicated if you want to do that. It will depend on how much you trust your data to not contain that sort of thing.

If you need to make this any more complicated, or need to do it anywhere else, I suggest you create a "string fixing" method which munges it appropriately, then call it for each of your fields.

EDIT: Removing accented characters is interesting. .NET makes this fairly easy, but I don't know what it will do for your "ae" situation - you may need to special-case that. Try this though, as a starting point:

static string RemoveAccents (string input) 
{ 
    string normalized = input.Normalize(NormalizationForm.FormKD); 
    Encoding removal = Encoding.GetEncoding 
        (Encoding.ASCII.CodePage, 
         new EncoderReplacementFallback(""), 
         new DecoderReplacementFallback("")); 
    byte[] bytes = removal.GetBytes(normalized); 
    return Encoding.ASCII.GetString(bytes); 
}
Jon Skeet
I'd argue you need a couple of calls to Uri.EscapeDataString in there, to protect from invalid characters url in the country or hotel names.
David Kemp
i need to even convert some special language specified characters as well for example ä to ae . Do u think i can do them as above
subha
Editing to cover that...
Jon Skeet
@David: That's what I meant about trusting the data. I suspect it's more of a case of "if there are non-urlsafe characters, I want to throw an exception rather than continue with an escaped form." It depends on the use case though.
Jon Skeet
thanks your help made me to get started
subha