Validation On String Using Trim Operation

By Goniey N (Mr. G)

Trim Function Validation Using Three Funtions : I) Left Trim II) Right Trim III) Trim All

I am Showing To Trim Functions For String Using Javascript...

Mainly Three Types Of Trim
Which Is Below :

1) Left Trim
2) Right Trim
3) Trim All





1) Left Trim :

->It Will Trim Leading White Spaces Characters From Given String.
->Pass
Parameter For String In Which We perform Left Trim Operation.
->Return String After Removing White Spaces From Leading Of The String.


Example :

Suppose String Is " Hello".
Here 3 White
Spaces Before The Hello.
So Using This, We Can Remove That White Spaces & Finally String Will Be "Hello".



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 spaces Characters From Given String.
->Pass
Parameter For String In Which We perform Right Trim Operation.
->Return String After Removing White Spaces From Trailing Of The String.


Example :

Suppose String Is "Hello ".
Here 3 White Spaces After The Hello.
So Using This, We Can Remove That White Spaces & Finally String Will Be "Hello".



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 :


->It Will Trim Leading & Trailing White Spaces Characters From Given String.
->Pass
Parameter For String In Which We perform Trim All Operation.
->Return String After Removing White Spaces From Leading & Trailing Of The String.


Example :

Suppose String Is " Hello ".
Here 3 White Spaces Before The Hello & After The Hello.
So Using This, We Can Remove That White Spaces & Finally String Will Be "Hello".



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

Validation On String Using Trim Operation  (910 Views)