Function for removing spaces in flash

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.