Removing all spaces from user input - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Removing all spaces from user input

EmilsEmils Posts: 2Member
I need to come up with a script that can edit user input, and remove all spaces contained therein.

Any ideas?

Comments

  • Josh CodeJosh Code Posts: 675Member
    [b][red]This message was edited by Josh Code at 2002-10-8 19:43:29[/red][/b][hr]
    : I need to come up with a script that can edit user input, and remove all spaces contained therein.
    :
    : Any ideas?
    :

    Make a function to remove spaces from a given string parameter.

    declare a variable to store the index of the space character in the string.

    set this variable to the location of the space using the string's method "indexOf".

    While this index is greater than 0, keep removing the space characters.

    Here is the code:

    [code]
    function clearspaces(s)
    {
    var i1; // stores the index
    i1=s.indexOf(" ",1);
    while (i1>0)
    {
    s = s.slice(0,i1)+s.slice(i1+1,999); // remove the space
    i1=s.indexOf(" ",1);
    }
    return s;
    }
    [/code]


Sign In or Register to comment.