Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home Javascript Javascript LTrim,RTrim,Trim function

Javascript LTrim,RTrim,Trim function

LTrim(),RTrim(), Trim() function are very popular functions in many programming languages such as C#,Java… which can delete leading or trailing spaces of any string. But Javascript does not support these function,it’s so sad:(!

In this article, I show you the way of making these function by creating simple algorithm to replace spaces based on only function SubString() which Javacript supports.

Function LTrim() – Delete leading spaces of a string

   function LTrim(text) {
        while (text.substring(0, 1) == ' ') {
            text = text.substring(1, text.length);
        }
        return text;
    }

function LTrim(text) { while (text.substring(0, 1) == ' ') { text = text.substring(1, text.length); } return text; }

    var text = "     4rapiddev.com";
    text = LTrim(text)
    // Output : text = "4rapiddev.com";

var text = " 4rapiddev.com"; text = LTrim(text) // Output : text = "4rapiddev.com";

Function RTrim() – Delete trailing spaces of a string

   function RTrim(text) {
        while (text.substring(text.length - 1, text.length) == ' ') {
            text = text.substring(0, text.length - 1);
        }
        return text;
    }

function RTrim(text) { while (text.substring(text.length - 1, text.length) == ' ') { text = text.substring(0, text.length - 1); } return text; }

    var text = "4rapiddev.com      ";
    text = RTrim(text)
    // Output : text = "4rapiddev.com";

var text = "4rapiddev.com "; text = RTrim(text) // Output : text = "4rapiddev.com";

Function Trim() – delete leading or trailing spaces of a string

    function Trim(text) {
 
        return LTrim(RTrim(text));
    }

function Trim(text) { return LTrim(RTrim(text)); }

    var text = "     4rapiddev.com      ";
    text = Trim(text)
    // Output : text = "4rapiddev.com";

var text = " 4rapiddev.com "; text = Trim(text) // Output : text = "4rapiddev.com";

Only function Trim() in case you do not need LTrim and RTrim function

    function Trim(text) {
        // Left Trim
        while (text.substring(0, 1) == ' ')
        {
            text = text.substring(1, text.length);
        }
        // Right Trim
        while (text.substring(text.length - 1, text.length) == ' ')
        {
            text = text.substring(0, text.length - 1);
        }
        return text;
   }

function Trim(text) { // Left Trim while (text.substring(0, 1) == ' ') { text = text.substring(1, text.length); } // Right Trim while (text.substring(text.length - 1, text.length) == ' ') { text = text.substring(0, text.length - 1); } return text; }

Javascript Trim Function Source code for testing

Feb 24, 2012quynhnguyen
Where Are ASPStateTempApplications and ASPStateTempSessions Tables?ASP.NET Store Session In MS SQL Server Database
You Might Also Like:
  • MSSQL Trim Function
  • MSSQL Split Function returns table data
  • Javascript remove vietnamese accents
  • String To Upper Case In PHP, JavaScript And .Net (CSharp)
  • String To Lower Case In PHP, JavaScript And .Net (CSharp)
  • Javascript Problem Set focus textbox on Firefox
  • Get Image Width Height With JQuery And JavaScript
  • Truncate And Limit Displayed Text With Selected Maximum Length PHP Function
  • JQuery Create Textbox With Count Down Limit Characters
  • Simple Truncate Words Function In C#
quynhnguyen
10 years ago JavascriptJavaScript, JQuery260
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,451 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,832 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,650 views
JQuery Allow only numeric characters or only alphabet characters in textbox
14,985 views
C# Read Json From URL And Parse/Deserialize Json
11,712 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development