Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 30-06-2009
Member
 
Join Date: Jan 2009
Posts: 150
How can I remove the last char using JavaScript

I have a sentence on my project build in JavaScript (for example) like this:

"Hello! Welcome Lord to ABC Companys"

As you can see the last word should be "Company" (without 's'). After the project has been completed, I noticed this on the welcome screen. I want to correct this mistake done by my developer who is out of the city for company work. But I don't know much of coding. How can I remove the last char (i.e. 's') using JavaScript?
Reply With Quote
  #2  
Old 30-06-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: How can I remove the last char using JavaScript

Its simple. The basic idea is assign the string to a variable. Using this string variable, create a new variable with value same as the previous variable but removing the last character. Now call this variable on your welcome screen.

HTML Code:
var str1 = 'Hello! Welcome Lord to ABC Companys';
var str2 = str1.substring(0, str1.length-1);
alert(str2);
Reply With Quote
  #3  
Old 30-06-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: How can I remove the last char using JavaScript

There is yet another way to achieve what you want. Use Left function. The code goes like this:

Code:
YourString = Left(YourString, Len(YourString) - 1)
This is similar to the previous one but replaces the three code line. Also since it overwrites the original string variable, there is no overload of calling the first variable, executing the code and then finally displaying the result. Thus reducing the time consumed to run your project.
Reply With Quote
  #4  
Old 30-06-2009
Member
 
Join Date: May 2008
Posts: 945
Re: How can I remove the last char using JavaScript

There are two ways to get this. First using substring function and second using replaceAll.

substring syntax

substring(from, [to]) - Returns the characters in a string between "from" and "to" indexes. Using "to" is optional, and can be omitted to get the string till the end.

replaceAll syntax

function replaceAll(yourstring, from, to)
{
var newStr = yourstring.indexOf(from);
while (newStr > -1) {
yourstring = yourstring.replace(from, to);
newStr = yourstring.indexOf(from);
}
return yourstring;
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How can I remove the last char using JavaScript"
Thread Thread Starter Forum Replies Last Post
Array of char and int Jensen Ackles Software Development 5 23-03-2010 09:50 AM
Converting Char to Int in PHP GlassFish Software Development 4 05-03-2010 08:06 PM
How to Add and Remove HTML elements Dynamically In Javascript? Level8 Software Development 5 20-02-2010 06:48 PM
How to convert char to hex ASHER Software Development 3 26-08-2009 06:49 PM
How do i clear char array in c++ B_Hodge Software Development 3 16-05-2009 09:35 AM


All times are GMT +5.5. The time now is 08:04 PM.