How to delete last character from a string using jQuery?

后端 未结 5 1226
夕颜
夕颜 2020-12-22 16:58

How to delete last character from a string for instance in 123-4- when I delete 4 it should display 123- using jQuery

5条回答
  •  误落风尘
    2020-12-22 17:27

    @skajfes and @GolezTrol provided the best methods to use. Personally, I prefer using "slice()". It's less code, and you don't have to know how long a string is. Just use:

    //-----------------------------------------
    // @param begin  Required. The index where 
    //               to begin the extraction. 
    //               1st character is at index 0
    //
    // @param end    Optional. Where to end the
    //               extraction. If omitted, 
    //               slice() selects all 
    //               characters from the begin 
    //               position to the end of 
    //               the string.
    var str = '123-4';
    alert(str.slice(0, -1));
    

提交回复
热议问题