www.webdeveloper.com
Results 1 to 6 of 6

Thread: [RESOLVED] Increment Number In String

  1. #1
    Join Date
    Feb 2011
    Posts
    11

    resolved [RESOLVED] Increment Number In String

    I have a string "Slide1.jpg"

    I need to increment it by 1 to say "Slide2.jpg"

    I have split and sliced but I keep getting SlideNaN.jpg

    Rather then fix my mess of code... There has to be a simple way of doing this..

    Help, Thanks

    My Brain Is Fried..

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Code:
    for(i=1;i<10;i++) {
        fileName = "Slide"+i+".jpg";
        document.writeln(fileName+"<br />");
        }
    Rather simplistic and not exactly what you are looking for. But it might give some assistance in your quest.

    Was trying to do it with RegEx, but don't have time to get into that, right now.. sorry.
    Last edited by WolfShade; 06-27-2011 at 11:45 AM.
    |||||
    o . Q
    ___ "You live and you learn; or you don't live long." - Lazarus Long

    ^_^

  3. #3
    Join Date
    Feb 2011
    Posts
    11
    That would work if I knew the starting slide. I am getting the slide through AJAX and want to be able to locally increment by 1 to preview the next slide.

  4. #4
    Join Date
    Dec 2010
    Posts
    207
    This will increment the first integer found in a string:
    Code:
    <script type="text/javascript">
    
    function bumpStringInt( str )
    {
      var data = str.match(/(\D*)(\d+)(\D*)/), newStr = "";
    
      if( data )    
        newStr = data[ 1 ] + ( Number( data[ 2 ] ) + 1 ) + data[ 3 ];
      
      return newStr || str;
    }
    
    alert( bumpStringInt( "Slide1.jpg" ) );
    
    </script>


    Q: I found this code...
    A: Then find its author.

  5. #5
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Code:
    "Slide1.jpg".replace(/(\d+)\./,function(j,a){return a- -1 +".";})

  6. #6
    Join Date
    Feb 2011
    Posts
    11
    Quote Originally Posted by rnd me View Post
    Code:
    "Slide1.jpg".replace(/(\d+)\./,function(j,a){return a- -1 +".";})
    That's the ticket... Thanks

    If anyone else needs to do this, the final code using the above is:

    PHP Code:
           var newSlide currentSlide.replace(/(\d+)\./,function(j,a){return a- -+".";});
           
    document.myimagepre.src newSlide

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles