cutting var

I know nothing of Java Script. I am customizing a program that allows users to modify java script in the program. I need to know if there is a way to cut the firt character in a varible I.E. if my varible was "0987878" how can I cut the "0"? Any help would be greatly appreciated.

Roger

Comments

  • : I know nothing of Java Script. I am customizing a program that allows users to modify java script in the program. I need to know if there is a way to cut the firt character in a varible I.E. if my varible was "0987878" how can I cut the "0"? Any help would be greatly appreciated.
    :
    : Roger
    :

    Hi there,

    Try this...

    var aString = "0987878";
    var newString = "";

    newString = aString.substring(1, aString.length);

    This will remove the first character of anything inside aString and store the result in newString.

    You could go one better and do this...

    var aString = "0987878";

    aString = aString.substring(1, aString.length);

    This will overwrite the value of aString with the original value minus the first character!

    Hope this helps!
    Bradley q:)

  • Thanks for your responce... I found out only after I had posted that message the first character only needs to be stripped when it is zero, and sometimes there are more than one "0". I eventullay came to the following code which uses the same substring function. Although I suppose I could clean it up a bit and remove some varibles.

    [code]
    if (strTemp.charAt(0) == "0"){
    var newTerm
    while (strTemp.charAt(0) == "0") {
    newTerm = strTemp.substring(1, strTemp.length)
    strTemp = newTerm;
    }
    [/code]

    ~rlc

  • Yes:

    The first two lines of code are not necessary. What you do now is: first check if the first character is a 0 and then loop checking if the first character is a zero.
    Also, you don't need to store it in newTerm first. See the changes:

    [code]
    // if (strTemp.charAt(0) == "0"){ [green] These two lines are
    [/green] // var newTerm [green] obsolete [/green]
    while (strTemp.charAt(0) == "0") {
    // [green] Notice the change of newTerm to strTemp [/green]
    strTemp = strTemp.substring(1, strTemp.length)
    // strTemp = newTerm; [green]You can delete this too then [/green]
    }
    [/code]

    Of course, to make it even better, you could change the name of strTemp to a more useful name like phonenumber

    :
    : Thanks for your responce... I found out only after I had posted that message the first character only needs to be stripped when it is zero, and sometimes there are more than one "0". I eventullay came to the following code which uses the same substring function. Although I suppose I could clean it up a bit and remove some varibles.
    :
    : [code]
    : if (strTemp.charAt(0) == "0"){
    : var newTerm
    : while (strTemp.charAt(0) == "0") {
    : newTerm = strTemp.substring(1, strTemp.length)
    : strTemp = newTerm;
    : }
    : [/code]
    :
    : ~rlc
    :
    :

  • Thanks, i did remove the extra var already. However, I couldn't remove the "if" loop. When I just used a "While" loop the script always checks the condition after it has run through the loop once. I.E. it would delete the first character no matter what, then look to see if "0" was first. The "if" loop checks before it runs and stopped this problem for me. I didn't see a way to do this with one loop, but if there is let me know.


    ~rlc



    : Yes:
    :
    : The first two lines of code are not necessary. What you do now is: first check if the first character is a 0 and then loop checking if the first character is a zero.
    : Also, you don't need to store it in newTerm first. See the changes:
    :
    : [code]
    : // if (strTemp.charAt(0) == "0"){ [green] These two lines are
    : [/green] // var newTerm [green] obsolete [/green]
    : while (strTemp.charAt(0) == "0") {
    : // [green] Notice the change of newTerm to strTemp [/green]
    : strTemp = strTemp.substring(1, strTemp.length)
    : // strTemp = newTerm; [green]You can delete this too then [/green]
    : }
    : [/code]
    :
    : Of course, to make it even better, you could change the name of strTemp to a more useful name like phonenumber
    :
    : :
    : : Thanks for your responce... I found out only after I had posted that message the first character only needs to be stripped when it is zero, and sometimes there are more than one "0". I eventullay came to the following code which uses the same substring function. Although I suppose I could clean it up a bit and remove some varibles.
    : :
    : : [code]
    : : if (strTemp.charAt(0) == "0"){
    : : var newTerm
    : : while (strTemp.charAt(0) == "0") {
    : : newTerm = strTemp.substring(1, strTemp.length)
    : : strTemp = newTerm;
    : : }
    : : [/code]
    : :
    : : ~rlc
    : :
    : :
    :
    :

  • [b][red]This message was edited by the compuchip at 2002-6-8 8:13:1[/red][/b][hr]
    Well, maybe it is a difference between our computers, but this code works with me
    [code]
    while (strTemp.charAt(0) == "0") {
    strTemp = strTemp.substring(1, strTemp.length)
    }
    [/code]

    You may be confused with the following use of while, which ALWAYS removes the first character, no matter what:
    [code]
    do {
    strTemp = strTemp.substring(1, strTemp.length)
    } while (strTemp.charAt(0) == "0");
    [/code]

    I thought this was the same on every computer, but I'd be glad if anyone could confirm that.

    (EDIT)
    Addition: you don't need the {braces}, just like in C++. So you can even write
    [code]
    while (strTemp.charAt(0) == "0")
    strTemp = strTemp.substring(1, strTemp.length);
    [/code]
    because the while-loop consists only of one statement.

    : Thanks, i did remove the extra var already. However, I couldn't remove the "if" loop. When I just used a "While" loop the script always checks the condition after it has run through the loop once. I.E. it would delete the first character no matter what, then look to see if "0" was first. The "if" loop checks before it runs and stopped this problem for me. I didn't see a way to do this with one loop, but if there is let me know.
    :
    :
    : ~rlc
    :
    :
    :
    : : Yes:
    : :
    : : The first two lines of code are not necessary. What you do now is: first check if the first character is a 0 and then loop checking if the first character is a zero.
    : : Also, you don't need to store it in newTerm first. See the changes:
    : :
    : : [code]
    : : // if (strTemp.charAt(0) == "0"){ [green] These two lines are
    : : [/green] // var newTerm [green] obsolete [/green]
    : : while (strTemp.charAt(0) == "0") {
    : : // [green] Notice the change of newTerm to strTemp [/green]
    : : strTemp = strTemp.substring(1, strTemp.length)
    : : // strTemp = newTerm; [green]You can delete this too then [/green]
    : : }
    : : [/code]
    : :
    : : Of course, to make it even better, you could change the name of strTemp to a more useful name like phonenumber
    : :
    : : :
    : : : Thanks for your responce... I found out only after I had posted that message the first character only needs to be stripped when it is zero, and sometimes there are more than one "0". I eventullay came to the following code which uses the same substring function. Although I suppose I could clean it up a bit and remove some varibles.
    : : :
    : : : [code]
    : : : if (strTemp.charAt(0) == "0"){
    : : : var newTerm
    : : : while (strTemp.charAt(0) == "0") {
    : : : newTerm = strTemp.substring(1, strTemp.length)
    : : : strTemp = newTerm;
    : : : }
    : : : [/code]
    : : :
    : : : ~rlc
    : : :
    : : :
    : :
    : :
    :
    :



  • Hi all,

    Yep I've just confirmed that in IE6 on WinXP the code that compuchip suggested worked for me...

    I just wrote a quick simple page to show it in action for you...

    [code]





    function doIt() {
    var strTemp = document.a.strTemp.value;
    while (strTemp.charAt(0) == "0") {
    strTemp = strTemp.substring(1, strTemp.length)
    }
    document.a.strTemp.value = strTemp;
    }










    [/code]

    Cya
    Bradley q:)

    : [b][red]This message was edited by the compuchip at 2002-6-8 8:13:1[/red][/b][hr]
    : Well, maybe it is a difference between our computers, but this code works with me
    : [code]
    : while (strTemp.charAt(0) == "0") {
    : strTemp = strTemp.substring(1, strTemp.length)
    : }
    : [/code]
    :
    : You may be confused with the following use of while, which ALWAYS removes the first character, no matter what:
    : [code]
    : do {
    : strTemp = strTemp.substring(1, strTemp.length)
    : } while (strTemp.charAt(0) == "0");
    : [/code]
    :
    : I thought this was the same on every computer, but I'd be glad if anyone could confirm that.
    :
    : (EDIT)
    : Addition: you don't need the {braces}, just like in C++. So you can even write
    : [code]
    : while (strTemp.charAt(0) == "0")
    : strTemp = strTemp.substring(1, strTemp.length);
    : [/code]
    : because the while-loop consists only of one statement.
    :
    : : Thanks, i did remove the extra var already. However, I couldn't remove the "if" loop. When I just used a "While" loop the script always checks the condition after it has run through the loop once. I.E. it would delete the first character no matter what, then look to see if "0" was first. The "if" loop checks before it runs and stopped this problem for me. I didn't see a way to do this with one loop, but if there is let me know.
    : :
    : :
    : : ~rlc
    : :
    : :
    : :
    : : : Yes:
    : : :
    : : : The first two lines of code are not necessary. What you do now is: first check if the first character is a 0 and then loop checking if the first character is a zero.
    : : : Also, you don't need to store it in newTerm first. See the changes:
    : : :
    : : : [code]
    : : : // if (strTemp.charAt(0) == "0"){ [green] These two lines are
    : : : [/green] // var newTerm [green] obsolete [/green]
    : : : while (strTemp.charAt(0) == "0") {
    : : : // [green] Notice the change of newTerm to strTemp [/green]
    : : : strTemp = strTemp.substring(1, strTemp.length)
    : : : // strTemp = newTerm; [green]You can delete this too then [/green]
    : : : }
    : : : [/code]
    : : :
    : : : Of course, to make it even better, you could change the name of strTemp to a more useful name like phonenumber
    : : :
    : : : :
    : : : : Thanks for your responce... I found out only after I had posted that message the first character only needs to be stripped when it is zero, and sometimes there are more than one "0". I eventullay came to the following code which uses the same substring function. Although I suppose I could clean it up a bit and remove some varibles.
    : : : :
    : : : : [code]
    : : : : if (strTemp.charAt(0) == "0"){
    : : : : var newTerm
    : : : : while (strTemp.charAt(0) == "0") {
    : : : : newTerm = strTemp.substring(1, strTemp.length)
    : : : : strTemp = newTerm;
    : : : : }
    : : : : [/code]
    : : : :
    : : : : ~rlc
    : : : :
    : : : :
    : : :
    : : :
    : :
    : :
    :
    :
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

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

Categories