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 6 of 6

Thread: String Question

  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    178
    Thanks
    138
    Thanked 0 Times in 0 Posts

    String Question

    Hello,

    I need your help.

    Let's say for example if I have the following file number: 2011-1234567 and if the value of the variable is already the same (ie. var fileNo = 2011-1234567) how can I go about automatically adding a number at the end?

    ex.1.)
    var fileNo = "2011-1234567"
    if (fileNo already exists) { fileNo = 2011-1234567-2 }

    ex.2.)
    var fileNo = "2011-1234567-2"
    if (fileNo already exists) { fileNo = 2011-1234567-3 }

    ect.

    Much thanks and appreciation for all your help.

    Jay

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    You'll need the concatenate operator for strings (which is "+")

    Example
    Code:
    var myString = "2011-1234567";
    myString = myString + "-2";
    // myString will now be 2011-1234567-2

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    178
    Thanks
    138
    Thanked 0 Times in 0 Posts
    Thanks very much for this Dev Null,

    How can I have it auto increment the number everytime the same fileno exists:


    var myString = "2011-1234567";
    myString = myString + "-2";

    if (2011-1234567-2 already exists) inc end# and output new file number as:

    2011-1234567-3

    Thanks a bunch

    Jay

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,774
    Thanks
    55
    Thanked 518 Times in 515 Posts
    you can try this:

    Code:
    <body>
    
    input number: <input type="text" id="box"> <input type ="button" value="check number" onclick="checkNum()">
    <div id="results"></div>
    <script type="text/javascript">
    nums=[100, 200, 300]
    function checkNum() {
    var inp= document.getElementById("box").value;
    for(var j = 0; j<nums.length; j++){
    if (nums[j]==inp) {
    inp=inp+"-2"
        }
      }
    document.getElementById("results").innerHTML="Unique reference is "+inp;
    nums.push(inp);
    }
    </script>
    
    </body>
    but this is a simple example and you will want to test it - say you input 100, that will be changed to 100-2. But if you then input 100-2 that will be changed to 100-2-2 and so on... it can be developed further (changing the second input to 100-3), if that's what's required...

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,774
    Thanks
    55
    Thanked 518 Times in 515 Posts
    oops - I think we posted at the same time. There's probably a fancier way to do this, but I am not very good at regex. This seems to work, provided that your original numbers will always have the dash in that location like this: 2011-1234567

    Code:
    <body>
    
    input number: <input type="text" id="box"> <input type ="button" value="check number" onclick="checkNum()">
    <div id="results"></div>
    <script type="text/javascript">
    var inp;
    nums=[100, 200, 300]
    function checkNum() {
    inp= document.getElementById("box").value;
    for(var j = 0; j<nums.length; j++){
    if (nums[j]==inp) {
    if (inp.lastIndexOf("-")==4){
    inp=inp+"-2"
        }else {
    newnum=Number(inp.match(/[^\-]*$/))+1
    inp=inp.replace(/[^\-]*$/,newnum)
        }
    	}
      }
      
    document.getElementById("results").innerHTML="Unique reference is "+inp;
    nums.push(inp);
    }
    </script>
    
    </body>
    Last edited by xelawho; 12-23-2011 at 10:53 PM.

  • #6
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,662
    Thanks
    41
    Thanked 189 Times in 188 Posts
    i would think you would want to go about it by passing parameters (I can't give exact js code bc i am not well versed but the logic behind it i would think could be)
    Code:
    function(obj){
    foreach string filename in C:\\  *i have no clue where r r comparing if string.exists
    {
    if (filename.length == obj.length)
    string[] test = null;
    string[] compare=null;
    test[] = filename.split('-');
    compare[]=obj.split('-');
      if (test.length == compare.length)
        if(test.length == 1)
          {test[0] = test[0] + '-1';}
        else if () // need to compare the arrays... can you just do a "does array1 == array2"                   
                    //  in js? i dont think so in which case just make a for loop comparing each
                    // spot in the array... 
      else if (test[test.length-1] == compare[compare.length-1])
      {
      int i = Convert.ToInt(test[test.length-1]);
      i++;
      var _i = i.tostring();
      _i = '-' + i;
      test[test.length-1] = _i;
      var SaveFileName = test.tostring();
      }
    }
    need to convert that to javascript it is a hybrid monster right now... but essentially you pass the string number and split it on '-' and then whatever/where-ever your comparing against do the same... if they have no '-' then their lengths will be equal and if their langth is == 1 then they are a one size number otherwise you drop down and look at the last spot on the array and see if it is equal, if it is add one and convert back to string concat the '-' and convert the array back to string format

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Posting Permissions

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