PDA

View Full Version : Changing a space to a + in a string


Tangent12
07-20-2005, 12:43 AM
Hi all, I think my problem should be easy for you guys; I've searched heavily accross the forums for an answer and can't find my actual problem.

What I want to do is is to format a UK postcode. I want the user to be able to enter a UK postcode eg. "L40 9HS" and then for the code to replace the space with a "+" so my finished variable would be "L40+9HS"

I am very inexperienced with expressions and from my searchings on here I think that an expression is what I need.

Any ideas; your help would be truly appreciated.

T12

benny2168
07-20-2005, 12:52 AM
hey try this (but it only works if your string is the same length every time)

string = "L40 9HS"

//creates "L40"
string2 = string.slice(0,3)

//creates"9HS"
string3 = string.slice(4.7)

//creates"L40+9HS"
newString = string2 + "+" + string3

im sure you get the idea enough to change that to suit your code

srija
07-20-2005, 02:16 AM
try this:

code="L40 9hs"or whatever value
postcode=code.split(" ").join("+");