SIGN UP MEMBER LOGIN:    
ARTICLE

Validation of a String Using Trim Operations

Posted by Ghanashyam Nayak Articles | Learn .NET April 11, 2011
Trim function validation using three functions : I) Left Trim II) Right Trim III) Trim All.
Reader Level:
Download Files:
 


Description :

I am showing use of Trim functions for strings using Javascript.

The main three types of Trim are:

  1. Left Trim
  2. Right Trim
  3. Trim All

1. Left Trim :
  • It will trim leading white spaces characters from a given string.
  • Pass a parameter for string in which we perform a Left Trim operation.
  • Returns a string after removing the leading white spaces from a string.

Example :

Suppose a string is " Ghanashyam".

Here 3 white spaces preced the Ghanashyam.

So using this, we can remove that white space and then the string will be "Ghanashyam".

function LeftTrim(LeftTrim_String)
{
                var RegExpobj = /^(\s*)(\b[\w\W]*)$/;
                if(RegExpobj.test(LeftTrim_String))
                {
                                //remove leading a whitespace characters From "LeftTrim_String"
                                LeftTrim_String = LeftTrim_String.replace(RegExpobj, '$2');
                }
                return LeftTrim_String;
}

2. Right Trim :

  • It will Trim trailing white space characters from a given string.

  • Pass a parameter for a string in which we perform a Right Trim operation.

  • Returns a string after removing trailing white spaces from a string.

Example :

Suppose a string is "Ghanashyam ".

There are 3 white spaces after the Ghanashyam.

So using the following, we can remove that white space and then the string will be "Ghanashyam".


function RightTrim(RightTrim_String)
{
                var RegExpobj = /^([\w\W]*)(\b\s*)$/;
                if(RegExpobj.test(RightTrim_String))
                {
                                //remove leading a whitespace characters From "RightTrim_String"
                                RightTrim_String = RightTrim_String.replace(RegExpobj, '$1');
                }
                return RightTrim_String;
}


3. Trim All :

  • Will Trim leading & trailing white space characters from a given string.

  • Pass a parameter for a string in which we perform a Trim All operation.

  • Returns a string after removing leading & trailing white spaces from a string.

Example :

Suppose String Is " Ghanashyam ".

Here there are 3 white spaces before the Ghanashyam & after the Ghanashyam.

So using this, we can remove the white spaces and then the string will be "Ghanashyam".

function TrimAll(TrimAll_String)
{
                var RegExpobj = /^(\s*)$/;

                //Check For All Whitespaces
                if(RegExpobj.test(TrimAll_String))
                {
                                TrimAll_String = TrimAll_String.replace(RegExpobj, '');
                                if( TrimAll_String.length == 0)
                                                return TrimAll_String;
                }

                //Check For Leading & Trailing Whitespaces
                RegExpobj = /^(\s*)([\W\w]*)(\b\s*$)/;
                if(RegExpobj.test(TrimAll_String))
                {
                                //Remove Leading And Trailing White Space Characters
                                TrimAll_String = TrimAll_String.replace(RegExpobj, '$2');
                }
                return TrimAll_String;
}
 

Login to add your contents and source code to this article
share this article :
post comment
 

Hey Mayur, Thanx Buddy...

Posted by Ghanashyam Nayak Apr 11, 2011

Very nice scripts

Posted by Mayur Gujrathi Apr 11, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor