remove the last word from a string? - Javascript String Operation

Javascript examples for String Operation:String Remove

Description

remove the last word from a string?

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <script>
             var str = "I am from 123 433."
             var res = "";
                var spl = str.split(' ');
                var len = spl.length;
                for(var i=0; i<len - 1;i++) {
                    if(i == len-2 )
                        res +=spl[i] +".";
                    else// w ww  .j  a  va 2s  . c om
                        res +=spl[i] +" ";
                }
            console.log(res);
        
      </script>  
   </body>
</html>

Related Tutorials