200

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

1
  • 36
    You're deleting the last two characters then. Commented Nov 29, 2010 at 23:00

5 Answers 5

515

You can also try this in plain 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

Sign up to request clarification or add additional context in comments.

2 Comments

we are (at least i am) using so much jQuery nowadays, sometimes i forget how to do in plain javascript =X
To clarify things up (as this post might be useful mostly for beginners) : .slice() will return the result. So one should use : var result = "1234".slice(0,-1);
43

Why use jQuery for this?

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

Of course if you must:

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

5 Comments

str.substring(0, str.count()-1)
@GolezTrol: str.count() is not a function. str.length return number of chars in string
@ skajfes BTW this is a better example I'll edit mine above to use length
Good example with jQuery. Thanks for posting Jason. Best
Because jQuery is often easier to use and makes more sense for a lot of people.
10

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

1 Comment

I prefer substring myself. Slice for me is way too close to array slice
5

You can do it with plain JavaScript:

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

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

1 Comment

The slice(0, -1) solution is better because you don't need to know the string length in advance.
1

This page comes first when you search on Google "remove last character jquery"

Although all previous answers are correct, somehow did not helped me to find what I wanted in a quick and easy way.

I feel something is missing. Apologies if i'm duplicating

jQuery

$('selector').each(function(){ 
  var text = $(this).html();
  text = text.substring(0, text.length-1);
  $(this).html(text);
});

or

$('selector').each(function(){ 
  var text = $(this).html();
  text = text.slice(0,-1);
  $(this).html(text);
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.