Several string processing functions

Source: Internet
Author: User
Some string processing functions are often used. I wrote some strings some time ago. I would like to add a few more here, although simple, however, it is a waste of time to rewrite it when you cannot remember it.
The global class contains several static methods for string processing, which can be called globally.
Strlfix, strrfix, strrep
These two functions can be used as functions that can be filled with specific characters. When the number of digits is less than the number, they can be filled with specific characters. For example, if the number is less than three digits, they can be filled with front 0 or back 0. If you want to use 0 to complete the above, use strrfix. If you want to use 0 to complete the following, use strlfix. Look Code Right:
When the number length does not meet three digits, use the front 0 to fill up, for example, 3 to fill up to 003, if it is 13 to fill up to 013, if it is 130, it will remain unchanged. Static   Void Strrfixdemo (ARGs _ ARGs)
{
;
Print strrfix ( ' 3 ' , 3 , ' 0 ' );
Print strrfix ( ' 13 ' , 3 , ' 0 ' );
Print strrfix ( ' 130 ' , 3 , ' 0 ' );
 
Pause;

}

The first parameter is the string to be processed, the second parameter is the number of digits to which the string is to be filled, and the third parameter is the character to be filled.
What if the length of the string to be processed is greater than that of the second parameter? Let's take a look at this method Source code Right: Static STR strrfix (STR _ STR, Int _ Length, Char _ Char =   '   ' )
{
ReturnStrrep (_ char, _ Length-Strlen (_ Str ))+Substr (_ STR,1, _ Length );
}

The substr function is easy to understand as part of the string. What is the strrep method? In many languages, this method is a string replacement function, that is, replacing part of the string with the specified string, but this function is implemented using the strreplace function in ax, the strrep function is actually a copy function. Rep should be short for replicate, for example, the following code:Static   Void Strrepdemo (ARGs _ ARGs)
{
;
Print strrep ("Hello World",2);
Pause;
}

the output is helloworld, and the second parameter indicates the number of copies. Here it is set to 2, so the source string is copied twice.
as we can see, strrfix actually consists of two parts: strrep and substr. If the length of the second parameter when calling the strrfix function is shorter than that of the source string, obviously, the second parameter of strrep is a negative number. In this way, the return value of this function is null, and only the return value of substr is returned. This is actually equivalent to substr, but it is intercepted from the left of the source string.

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.