- Public Static boolean isletter (char c) {
- int k = 0x80;
- return c/k = = 0? true: false;
- }
- /**
- * Determine if the string is empty
- * @param str
- * @return
- */
- Public Static boolean isNull (String str) {
- if (str==null| | Str.trim (). Equals ("") | | Str.trim (). Equalsignorecase ("null")) {
- return true;
- }else{
- return false;
- }
- }
- /**
- * Gets the length of a string, the length of the display, a Chinese character or a Japanese-Korean length of 2, an English character length of 1
- * @param string s need to get length strings
- * The string length obtained @return int
- */
- public static int Length (String s) {
- if (s = = null)
- return 0;
- char[] C = S.tochararray ();
- int len = 0;
- For (int i = 0; i < c.length; i++) {
- len++;
- if (!isletter (C[i])) {
- len++;
- }
- }
- return len;
- }
- /**
- * Gets the length of a string, the length of the display, a Chinese character or a Japanese-Korean length of 1, an English character length of 0.5
- * @param string s need to get length strings
- * The string length obtained @return int
- */
- public static double getlength (String s) {
- double valuelength = 0;
- String Chinese = "[\u4e00-\u9fa5]";
- //Gets the length of the field value, if it contains Chinese characters, the length of each Chinese character is 2, otherwise 1
- For (int i = 0; i < s.length (); i++) {
- //Get a character
- String temp = s.substring (i, i + 1);
- //Determine if it is a Chinese character
- if (temp.matches (Chinese)) {
- //Chinese character length is 1
- Valuelength + = 1;
- } Else {
- //Other character length is 0.5
- Valuelength + = 0.5;
- }
- }
- // Carry rounding
- return Math.ceil (valuelength);
- }
- Reprint Source: http://blog.csdn.net/z69183787/article/details/18843925
Java determines the length of a string containing Chinese