Remove last character or word from string with Javascript

fromrachel

Member
Joined
Dec 14, 2013
Messages
231
Points
18
Hi everyone,

I have a string :
I want to remove last character
string1 = Aaron,Abner,Abraham,Adam,Addison;
The result should be: Aaron,Abner,Abraham,Adam,Addison

I want to remove last word
string2 = Aaron,Abner,Abraham,Adam,Addison
The result should be: Aaron,Abner,Abraham,Adam,

How can I remove only the last character or word from the strings above using Javascript?
 

skullofskill

Member
Joined
Nov 16, 2016
Messages
39
Points
8
Hello fromRachel, for removing the last character use >> var res = str.slice(0, 32); and for removing the the last word use, >> var res = str.slice(0, 24);

str.slice(0, 32); = String Slice, and the first number in parenthesis is the first character in the string that will start the extracting, so 0 (zero) is the first character to be sliced. And the second set of digits are how many characters to the ending to slice.

Hope that helps you some.
 

Rob Whisonant

Moderator
Joined
May 24, 2016
Messages
2,489
Points
113
If all words are always comma delimited you should be able to use this to remove the last word and the comma.

str = str.substring(0, str.lastIndexOf(",") - 1);

To remove the last character in a string you can use one of these.

str = str.substring(0, str.length - 1);

str = str.slice(0, -1);

str being the original string.
 

fromrachel

Member
Joined
Dec 14, 2013
Messages
231
Points
18
Hello fromRachel, for removing the last character use >> var res = str.slice(0, 32); and for removing the the last word use, >> var res = str.slice(0, 24);

str.slice(0, 32); = String Slice, and the first number in parenthesis is the first character in the string that will start the extracting, so 0 (zero) is the first character to be sliced. And the second set of digits are how many characters to the ending to slice.

Hope that helps you some.
I tried to run your codes

Code:
<script>
string1 = "Aaron,Abner,Abraham,Adam,Addison";
str =  string1.slice(0, 32);
document.write(str);
</script>
The output is

Code:
Aaron,Abner,Abraham,Adam,Addison
It didn't remove the last character 'n'

Running this script
Code:
<script>
string1 = "Aaron,Abner,Abraham,Adam,Addison";
str =  string1.slice(0, 24);
document.write(str);
</script>
It worked, removed ,Adam
Code:
Aaron,Abner,Abraham,Adam
But last word is splitted by ,Adam?


If all words are always comma delimited you should be able to use this to remove the last word and the comma.

str = str.substring(0, str.lastIndexOf(",") - 1);

To remove the last character in a string you can use one of these.

str = str.substring(0, str.length - 1);

str = str.slice(0, -1);

str being the original string.
Code:
<script>
string1 = "Aaron,Abner,Abraham,Adam,Addison";
str = string1.substring(0, string1.lastIndexOf(",") - 1);
document.write(str);
</script>
The output is
Code:
Aaron,Abner,Abraham,Ada
but it is removing last character, not last word.
The last word didn't work as expected.

trying this
Code:
<script>
string1 = "Aaron,Abner,Abraham,Adam,Addison";
str = string1.substring(0, string1.length - 1);
document.write(str);
</script>
the output
Code:
Aaron,Abner,Abraham,Adam,Addiso
it worked for this case.
 

Rob Whisonant

Moderator
Joined
May 24, 2016
Messages
2,489
Points
113
Change

str = string1.substring(0, string1.lastIndexOf(",") - 1);

to

str = string1.substring(0, string1.lastIndexOf(","));
 

skullofskill

Member
Joined
Nov 16, 2016
Messages
39
Points
8
If you do:

var str = "Aaron,Abner,Abraham,Adam,Addison;";
var res = str.slice(0, 31);

^^ that will remove the ';', plus the 'n'. ^^

var res = str.slice(0, 32); <--- will remove only the ';' , Obviously, you can always just not type the ';' if it's not being used. :lol:

On your original post, there was a ';' added to the ending. Now if there IS suppose to be a ';' at the end, then to remove only the ';' it's---> str.slice(0, 32); -- str.slice(0, 31) for the ; and the n to be removed, and not extracted.

RE-CAP
To remove ',Addison', and the ';' from the end, then it will be >> str.slice(0, 24);

To extract only the 'n', if there was never supposed to be a ';' after 'n' ---> str.slice(-1);
Or, are you wanting to extract only the second to last character, that being 'n', with ';' being at the end of 'Addison' and NOT the one that's after the " ?

Addison;";
or
Addison";
?

What are you doing to my brain?:bash::lol:
 

fromrachel

Member
Joined
Dec 14, 2013
Messages
231
Points
18
Change

str = string1.substring(0, string1.lastIndexOf(",") - 1);

to

str = string1.substring(0, string1.lastIndexOf(","));
It worked as a charm, removed last word and commas before it

The output

Code:
Aaron,Abner,Abraham,Adam

If you do:

var str = "Aaron,Abner,Abraham,Adam,Addison;";
var res = str.slice(0, 31);

^^ that will remove the ';', plus the 'n'. ^^
Running your codes above, the output


Code:
Aaron,Abner,Abraham,Adam,Addiso
It does not remove ; that removing n

What is 24, 31, 32 in these commands, it is number of characters you counted?

var res = str.slice(0, 32); <--- will remove only the ';' , Obviously, you can always just not type the ';' if it's not being used. :lol:

On your original post, there was a ';' added to the ending. Now if there IS suppose to be a ';' at the end, then to remove only the ';' it's---> str.slice(0, 32); -- str.slice(0, 31) for the ; and the n to be removed, and not extracted.

RE-CAP
To remove ',Addison', and the ';' from the end, then it will be >> str.slice(0, 24);

To extract only the 'n', if there was never supposed to be a ';' after 'n' ---> str.slice(-1);
Or, are you wanting to extract only the second to last character, that being 'n', with ';' being at the end of 'Addison' and NOT the one that's after the " ?

Addison;";
or
Addison";
?

What are you doing to my brain?:bash::lol:

Code:
  var str = "Aaron,Abner,Abraham,Adam,Addison";
I wanted to remove Addison and , before it mean removing the last word separated by ',' and it should remove ',' before the last word.
but I think Rob solved my question.
 

achat

New member
Joined
Jan 2, 2017
Messages
16
Points
0
You can use the substring function:
Code:
var str = "12345.00";
str = str.substring(0, str.length - 1);
This is the accepted answer, but as per the conversations below, the slice syntax is much clearer:
Code:
var str = "12345.00";
str = str.slice(0, -1);
 
Newer threads
Replies
6
Views
5,223
Replies
12
Views
7,849
Replies
5
Views
8,153
Replies
7
Views
6,450
wms
Recommended threads
Replies
16
Views
6,142
Replies
4
Views
2,105
Replies
10
Views
5,212
Replies
3
Views
2,655

Referral contests

Referral link for :

Sponsors

Popular tags

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top