Java determines whether it is a Chinese character

Source: Internet
Author: User

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

}
 
}

Related Article

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.