如何使用jQuery从字符串中删除最后一个字符?

时间:2022-11-29 10:46:06

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

如何从字符串中删除最后一个字符,比如123-4——当我删除4时,它应该显示123——使用jQuery。

4 个解决方案

#1


389  

You can also try this in plain javascript

您也可以在普通的javascript中尝试这一点

"1234".slice(0,-1)

the negative second parameter is an offset from the last character, so you can use -2 to remove last 2 characters etc

第二个负参数是与最后一个字符的偏移量,因此可以使用-2删除最后两个字符,等等

#2


29  

Why use jQuery for this?

为什么要使用jQuery呢?

str = "123-4"; 
alert(str.substring(0,str.length - 1));

Of course if you must:

当然,如果你必须:

Substr w/ jQuery:

Substr w / jQuery:

//example test element
 $(document.createElement('div'))
    .addClass('test')
    .text('123-4')
    .appendTo('body');

//using substring with the jQuery function html
alert($('.test').html().substring(0,$('.test').html().length - 1));

#3


6  

@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:

@skajfes和@GolezTrol提供了最好的使用方法。我个人更喜欢使用“slice()”。它的代码更少,而且你不需要知道一个字符串有多长。只使用:

//-----------------------------------------
// @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));

#4


5  

You can do it with plain JavaScript:

你可以使用简单的JavaScript:

alert('123-4-'.substr(0, 4)); // outputs "123-"

This returns the first four characters of your string (adjust 4 to suit your needs).

这将返回字符串的前四个字符(根据需要调整4)。

#1


389  

You can also try this in plain javascript

您也可以在普通的javascript中尝试这一点

"1234".slice(0,-1)

the negative second parameter is an offset from the last character, so you can use -2 to remove last 2 characters etc

第二个负参数是与最后一个字符的偏移量,因此可以使用-2删除最后两个字符,等等

#2


29  

Why use jQuery for this?

为什么要使用jQuery呢?

str = "123-4"; 
alert(str.substring(0,str.length - 1));

Of course if you must:

当然,如果你必须:

Substr w/ jQuery:

Substr w / jQuery:

//example test element
 $(document.createElement('div'))
    .addClass('test')
    .text('123-4')
    .appendTo('body');

//using substring with the jQuery function html
alert($('.test').html().substring(0,$('.test').html().length - 1));

#3


6  

@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:

@skajfes和@GolezTrol提供了最好的使用方法。我个人更喜欢使用“slice()”。它的代码更少,而且你不需要知道一个字符串有多长。只使用:

//-----------------------------------------
// @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));

#4


5  

You can do it with plain JavaScript:

你可以使用简单的JavaScript:

alert('123-4-'.substr(0, 4)); // outputs "123-"

This returns the first four characters of your string (adjust 4 to suit your needs).

这将返回字符串的前四个字符(根据需要调整4)。