Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    In a house
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How do i replace a space in a string with a +?

    I am trying to link to a site called Mapquest directly via a URL. To do this, the spaces in the variables need to be changed to + signs, and i'm not really good enough to go through all that. I was wondering if someone had a script or could suggest somewhere to find one that would do this. Thanks all

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A combination of a Regular Expression and string.replace() would do the job. Here is an example:
    re1=/ /gi
    myvar1=" "
    myvar2=myvar1.replace(re1, "+")

    Here is some info on how they work:
    http://developer.netscape.com/docs/m...ide/regexp.htm

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Very true requestcode, but isn't that a waste of variable space? Why not just this?
    Code:
    var myvar1="this and this";
    myvar1 = myvar1.replace(/ /g, "+");
    And the 'i' modifier isn't necessary, since spaces only have 1 case

  • #4
    New Coder
    Join Date
    Jul 2002
    Location
    In a house
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks guys, the link works OK now. Much appreciated

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I know just typing a space works, but isn't this more "correct"?:

    var myvar1="this and this";
    myvar1 = myvar1.replace(/\s/g,"+");

    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Duh... adios is right... I'm still thinking in vbscript (and not well at that! hehe since you could use Server.URLEncode(var) in VBScript...).

    escape(var) is what you should use...
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #8
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    i don't think so. D2K2 needs +'s, not %20's. i think this is still the right way to go:

    var myvar1="this and this";
    myvar1 = myvar1.replace(/\s/g,"+");
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #9
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Whammy - well, perhaps

    \s is the same as [ \t\v\n\r\f]

    so \s will also replace tabs, vertical tabs, newlines, carriage returns, and formfeeds. So \s would be accurate if D2K2 intends to replace all those with +'s as well.

    Being that this is a URL, it's likely that spaces will be the only whitespace characters present anyhow. I just wanted to point out the difference.

    If it makes you feel better you can always go with /[ ]/g
    Last edited by beetle; 08-28-2002 at 01:39 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •