Js remove the space instance Trim of LTrim of RTrim of

  • 2020-03-30 01:15:32
  • OfStack

1, js to remove the string space
// to the left space;
The function ltrim (s)
. {

Return s.r eplace (/ (^ s *)/g, "");
}
// go to the right space;
The function rtrim (s)
. {
Return s.r eplace (/ * $(s)/g, "");
}
// left and right Spaces;
The function trim (s)... {
/ / s.r eplace (/ (^ s *) | (s * $)/g, "");
Return rtrim (ltrim (s));

}

2. A function that removes Spaces from both sides of a string
// parameter: string passed by mystr
// returns: string mystr
The function trim (mystr) {
While ((mystr indexOf (" ") = = 0) && (mystr. Length> 1)) {
Mystr = mystr. Substring (1, mystr. Length);
}// remove the preceding space
While ((mystr lastIndexOf (" ") = = mystr) length - 1) && (mystr. Length> 1)) {
Mystr = mystr. Substring (0, mystr. Length - 1);
}// remove the space behind
{if (mystr = = "")
Mystr = "";
}
Return mystr;
}


Related articles: