Results 1 to 4 of 4

Thread: How can I remove the last char using JavaScript

  1. #1
    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?

  2. #2
    Join Date
    May 2008
    Posts
    2,297

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

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    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.

  4. #4
    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;
    }

Similar Threads

  1. Array of char and int
    By Jensen Ackles in forum Software Development
    Replies: 5
    Last Post: 23-03-2010, 09:50 AM
  2. Converting Char to Int in PHP
    By GlassFish in forum Software Development
    Replies: 4
    Last Post: 05-03-2010, 08:06 PM
  3. How to Add and Remove HTML elements Dynamically In Javascript?
    By Level8 in forum Software Development
    Replies: 5
    Last Post: 20-02-2010, 06:48 PM
  4. How to convert char to hex
    By ASHER in forum Software Development
    Replies: 3
    Last Post: 26-08-2009, 06:49 PM
  5. How do i clear char array in c++
    By B_Hodge in forum Software Development
    Replies: 3
    Last Post: 16-05-2009, 09:35 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,891,646.17033 seconds with 16 queries