SoFunction
Updated on 2025-03-04

js remove space instances Trim LTrim RTrim

1. JS removes the spaces of strings
//Get the left space;
function ltrim(s)
...{

return (/(^s*)/g, "");
}
//Remove the right space;
function rtrim(s)
...{
return (/(s*$)/g, "");
}
//Get the left and right spaces;
function trim(s)...{
//(/(^s*)|(s*$)/g, "");
return rtrim(ltrim(s));

}

2. Functions that remove spaces on both sides of string
//Article: string passed in by mystr
//Return: string mystr
function trim(mystr){
while (((" ")==0) && (>1)){
mystr=(1,);
}//Remove the previous spaces
while (((" ")==-1)&&(>1)){
mystr=(0,-1);
}//Remove the spaces behind
if (mystr==" "){
mystr="";
}
return mystr;
}