SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software

Kodyaz Development Resources

Development resources, articles, tutorials, samples, codes and tools for .Net, SQL Server, Windows, Windows Phone, SAP and ABAP, like SAP UI5, Screen Personas, etc.






Javascript String Trim with Javascript Trim Function Examples


Just as in many programming languages, in Javascript too, programmers and developers frequently require trimming functions and trim codes in order to manipulate string or text values and variables.
Although many programming and scripting languages have built-in methods for string trimming, Javascript does not have a built-in method or a Javascript trim function.

What is Trimming, TRIM, LTRIM, RTRIM Functions ?

To trim string is removing whitespace like spaces from a string variable.
If the code trim leading spaces then this trim function is called LTRIM.
If the code trim trailing white space then this trim function is called RTRIM.
LTRIM can be considered as Left Trim which is trimming a string or a text from left side.
RTRIM can be considered as Right Trim which is trimming a string or a text from right hand side.
So string TRIM function is the combination of these two LTRIM and RTRIM string strip functions.


General Look at Javascript TRIM Functions ?

It is possible to create a javascript trim function which loops through all of the characters in a given string and remove the white space from the string one by one until a valid string character is reached for LTRIM.
And by using the same approach, starting from the end of the string variable it is possible to remove the trailing spaces until a string character is found in the loop for RTRIM.
You can even reverse the string for the RTRIM and use the same javascript code you have built for LTRIM.
But instead of using a javascript loop which is not a preferred way of coding, I can suggest using regular expressions with Javascript to strip string values.





Javascript Trim Function using Loop in the Characters of the String with While

In the below javascript trim function, we develop string trimming code to trim string with one of the javascript loop commands While.

<script language="javascript">
  function trimBoth(str) {
    return trimRight(trimLeft(str));
  }
  function trimLeft(str) {
    var ListOfWhiteSpaceChars = " \f\n\r\t";

    var k = 0;
    while (k < str.length) {
      if (ListOfWhiteSpaceChars.indexOf(str.charAt(k)) == -1) {
        return str.substring(k, str.length);
      } else {
        k++;
      }
    }
  }
  function trimRight(str) {
    var ListOfWhiteSpaceChars = " \f\n\r\t";

    var k = str.length - 1;
    while (k >= 0) {
      if (ListOfWhiteSpaceChars.indexOf(str.charAt(k)) == -1) {
        return str.substring(0, k + 1);
      } else {
        k--;
      }
    }
  }
</script>

Javascript Trim Function Example

In the below Javascript trim method code example, the trim functions including ltrim and rtrim take the string or the text value as an argument of the trim function, and returns the text after white spaces are removed from the input string.

<script language="javascript">
function ltrim(str) {
  return str.replace(/^\s+/g, '');
}
function rtrim(str) {
  return str.replace(/\s+$/g, '');
}
function trimStr(str) {
  return str.replace(/^\s+|\s+$/g, '');
}
</script>

Javascript String Trim Prototype Example

It is also possible for Javascript developers to add the trim method to the String object's prototype.
Adding the trim functions to the String object prototype and including the script file as a global javascript include will enable developers to use trim methods everywhere in the web pages.

<script language="javascript">
  String.prototype.ltrim = function() {
    return this.replace(/^\s+/g, '');
  }
  String.prototype.rtrim = function() {
    return this.replace(/\s+$/g, '');
  }
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
  }
</script>

You can use these sample javascript trim functions in your button OnClick() actions, or in textbox 's onBlur() events, etc.






Javascript

Javascript Articles

Javascript Forums












Javascript Tutorials, Articles and Javascript Codes

Morris Number Sequence Puzzle Solution in Javascript
Javascript Array Puzzle: People with Sword Around a Circle
Sort Array in Random Order using Javascript Shuffle Function
Javascript MultiDimensional Array example codes
How many times a day a clock's hands overlap?
Javascript For Loop Sample Code
Use Javascript Replace Function Example to Replace Multi Space in a String with Single Space
Javascript String Trim with Javascript Trim Function Examples
Javascript String Split Function Example & Split Javascript Tutorial
Javascript Phone Format : Phone Number Format and Mask Telephone Numbers with Javascript
How to Enable / Disable Submit Button using Javascript According to any CheckBox is Checked on a WEB Page
Error Handling in JavaScript using Try...Catch Statement
How to display or unhide hidden table rows and hide or toggle a table row using Javascript functions
How to Replace All Occurrences of a String using Javascript Replace Function
How to autoformat and mask telephone numbers with Javascript
How to get key codes of characters using javascript code and key code list
Javascript Forums




Copyright © 2004 - 2017 Eralper YILMAZ. All rights reserved.
Community Server by Telligent Systems