Public class stringutil {
/**
* Determines whether it is a Chinese character.
* @ Param C
* @ Return
*/
Private Static Boolean ischinese (char c ){
// General_punctuation
// Cjk_symbols_and_punctuation judge Chinese characters. No.
// Halfwidth_and_fullwidth_forms indicates the number of Chinese characters.
Character. unicodeblock UB = character. unicodeblock. of (c );
If (UB = character. unicodeblock. cjk_uniied_ideographs
| UB = character. unicodeblock. cjk_compatibility_ideographs
| UB = character. unicodeblock. cjk_uniied_ideographs_extension_a
| UB = character. unicodeblock. general_punctuation
| UB = character. unicodeblock. cjk_symbols_and_punctuation
| UB = character. unicodeblock. halfwidth_and_fullwidth_forms ){
Return true;
}
Return false;
}
/**
* Convert to as400 character array
* @ Param srcstr
* @ Param Len
* @ Return
*/
Private Static char [] getas400char (string srcstr, int Len ){
Char [] chararray = srcstr. tochararray ();
Int dblength = 0;
Boolean start = false; // Chinese
Boolean end = false; // Chinese end
Boolean duration = false; // continuous Chinese
Int charlength = 0;
For (INT I = 0; I <chararray. length; I ++ ){
// Determine whether it is a Chinese character
If (ischinese (chararray [I]) {
If (duration)
Start = false;
Else
Start = true;
Duration = true;
End = false;
Dblength ++; // The Chinese character must be one more character than the common character.
}
Else if (duration) {// when a non-Chinese character is encountered, if the Chinese character has been started, the end
End = true;
}
Dblength ++; // occupies the length of a single digit
// If the Chinese character starts, add one
If (start ){
Dblength ++;
Start = false;
}
// If the Chinese character ends, add one
If (duration & End ){
Dblength ++;
Duration = false;
}
// Exit if the database length exceeds the limit
If (dblength> Len)
Break;
Else if (duration) {// if it is a Chinese character, because a length will be added at any time next time. If the length exceeds, exit
If (dblength + 1)> Len)
Break;
}
Charlength ++;
}
Char [] targetarr = new char [charlength];
For (INT I = 0; I <charlength; I ++ ){
Targetarr [I] = chararray [I];
}
Return targetarr;
}
/**
* Obtain a string of the specified length that complies with the as400 storage specifications.
* @ Param srcstr
* @ Param Len
* @ Return
*/
Public static string getas400str (string srcstr, int Len ){
If (srcstr = NULL)
Return NULL;
Return string. valueof (getas400char (srcstr, Len ));
}
}