Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to take last character out of string if character is a space... 1

Status
Not open for further replies.

spewn

Programmer
May 7, 2001
1,034

basically, i know who to check each character using charAt(i), where i equals a progressive number.

but how do i remove individual elements of the string?

any help?

- g
 
Code:
function removeChar(pos)
{
  this = this.substring(0,pos-1) + this.substring(pos, this.length);
}
String.prototype.removeChar = removeChar;
Put this somewhere on your page, and call:
Code:
var teststr = "abcdefghij";
teststr.removeChar(5);
alert(teststr) // alerts "abcdfghij"

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
Or, you could do:

Code:
var str = "hello there";
str.replace( " ", "" );
alert( str );

*cLFlaVA
----------------------------
[tt]insert funny quotation here.[/tt]
 
Or, answering the topic, to remove the last character of a string only if it is a space, you could do:
Code:
var str = "hey ";
str = (str.charAt(str.length-1) == " ") ? str.slice(0,str.length-1) : str;
alert (str);
hope that helps

"Insane people are always sure that they are fine. It is only the sane people who are willing to admit that they are crazy." - Nora Ephron
 
wise guy, eh?

*cLFlaVA
----------------------------
[tt]insert funny quotation here.[/tt]
 
just tryin to help out :D

"Insane people are always sure that they are fine. It is only the sane people who are willing to admit that they are crazy." - Nora Ephron
 
Then may I continue on this crusade to help by suggestion a shorter line:

Code:
str = (str.charAt(str.length-1) == " ") ? str.slice(0,[blue]-1[/blue]) : str;

*cLFlaVA
----------------------------
[tt]insert funny quotation here.[/tt]
 
Nice.

Forgot about that negative number thing.

"Insane people are always sure that they are fine. It is only the sane people who are willing to admit that they are crazy." - Nora Ephron
 
Or to answer the topic (again):
Code:
str = str.replace(/( )$/,"");

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
If we're trying to do tighter code :), why not just do:

Code:
str = str.replace(/\s$/,"");

Yeah, it is one character shorter. LOL.

Though I kinda prefer

Code:
if (str.charAt(str.length-1)=" ") str=str.slice(0,-2);

... since it would seem to execute faster -- only a simple test on the last position, and only if needed does it do a complete string value replacement (which could be relatively slower if it's a long string).

That's my 2 cents worth.

Merry Christmas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top