CopyCode The Code is as follows: // function: Space check, yes-true, no-false
Function isspace (STR: string): Boolean {
Switch (STR ){
Case string. fromcharcode (32 ):
// Space
Break;
Case string. fromcharcode (9 ):
// Tab key
Break;
Case string. fromcharcode (12288 ):
// Chinese double-byte Space
Break;
Case string. fromcharcode (13 ):
// Linefeed
Break;
Default:
Return false;
}
Return true;
}
//
// Function: calculates the number of consecutive spaces after the start.
Function countstartspace (STR: string): number {
VaR numloop: Number = Str. length;
For (VAR I = 0; I <numloop; I ++ ){
// Exit immediately when a non-space character is encountered
If (isspace (Str. charat (I) = false ){
Return I;
}
}
// All are spaces
Return I;
}
// Function: calculates the number of consecutive spaces before the end.
Function countendspace (STR: string): number {
VaR numloop: Number = Str. length;
For (VAR I = numLoop-1; I> = 0; I --){
// Exit immediately when a non-space character is encountered
If (isspace (Str. charat (I) = false ){
Return I;
}
}
// All are spaces
Return I;
}
//
// Function: removes the leading space of the string and returns the truncated string.
Function lefttrim (STR: string): String {
VaR Newstart = countstartspace (STR );
Return Str. Slice (Newstart );
}
//
// Function: removes spaces at the end of the string and returns the truncated string.
Function righttrim (STR: string): String {
VaR newend = countendspace (STR) + 1;
Return Str. Slice (0, newend );
}
//
// Function: removes spaces at both ends of the string and returns the truncated string.
Function alltrim (STR: string): String {
VaR rightstr: String = lefttrim (STR );
If (rightstr. Length = 0 ){
Return "";
// Return NULL;
} Else {
Return righttrim (rightstr );
}
}
// Test
VaR STR: String = new string ("123 5 ");
Trace (lefttrim (STR ));
Trace (righttrim (STR ));
Trace (alltrim (STR ));