Closed Thread Icon

Preserved Topic: Removing some characters from a string (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=18410" title="Pages that link to Preserved Topic: Removing some characters from a string (Page 1 of 1)" rel="nofollow" >Preserved Topic: Removing some characters from a string <span class="small">(Page 1 of 1)</span>\

 
kuckus
Bipolar (III) Inmate

From: Berlin (almost)
Insane since: Dec 2001

posted posted 06-09-2002 22:26

Hi everyone,

I'm trying to make a script Mozilla-compatible, and since I don't know that much about JavaScript yet I have a little question regarding string manipulation:

Depending on the visitor's browser, a variable has a value of either "48" or "48px". As I want to substract another value from that one I need to check if the variable contains a string, find the "px" and remove it from the string, leaving nothing but the number.

I already tried the replace() function and I'm not sure if it's me or the funtion who isn't smart enough to do the trick:

this.currentY = this.currentY.replace("px","");

As I said in the beginning, this.currentY is "48" in IE and "48px" in Mozilla and should become "48" after the operation. Something like this can't be that difficult, can it? But I'm clueless anyway

kuckus (cell #282)

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 06-09-2002 22:32

The parseInt function does this nicely. It takes a string as an argument, and looks for numbers in the beginning of the string. It takes all the numbers up to either the end of the string or the first non-number character, and returns them as a number.

parseInt('48') returns 48.
parseInt('48px') returns 48.
parseInt(48) returns 48 also, since the number is automatically converted into a string.

Once you're done manipulating the number, just add + 'px' onto the end of it again. (Remember that even though IE doesn't *give* it to you with 'px' on the end, it will *accept* it from you with 'px' on the end.)

kuckus
Bipolar (III) Inmate

From: Berlin (almost)
Insane since: Dec 2001

posted posted 06-09-2002 23:18

Thanks a lot, Slime, that's exactly what I was looking for!

kuckus (cell #282)

« BackwardsOnwards »

Show Forum Drop Down Menu