1/** cut the Chinese string to the length occupied by the current DB2 (encoding GBK) */2 Public String cutstringfordb2 (string SRC, integer size) {3 int Len = SRC. length (); 4 int res_len = 0; 5 stringbuilder res_ B = new stringbuilder (); 6 for (INT I = 0; I <Len; I ++) {7 if (ischinese (SRC. charat (I) {8 res_len + = 3; 9} else {10 res_len + = 1; 11} 12 if (res_len> size) {13 break; 14} else {15 res_ B .append (SRC. charat (I); 16} 17} 18 return res_ B .tostri Ng (); 19} 20 21 public Boolean ischinese (char c) {22 character. unicodeblock UB = character. unicodeblock. of (c); 23 if (UB = character. unicodeblock. cjk_uniied_ideographs | UB = character. unicodeblock. cjk_compatibility_ideographs24 | UB = character. unicodeblock. cjk_unified_ideographs_extension_a | UB = character. unicodeblock. cjk_unified_ideographs_extension_b25 | UB = character. unicodeblock. c Jk_symbols_and_punctuation | UB = character. unicodeblock. halfwidth_and_fullwidth_forms26 | UB = character. unicodeblock. general_punctuation) {27 return true; 28} 29 return false; 30} 31 32 public double cutdouble (string SRC, integer intvalue, integer pointvalue) {33 If (SRC. length ()> intvalue) {34 if (SRC. matches ("[0-9] * \. [0-9] * ") {35 string [] STRs = SRC. split ("\\. "); 36 stringbuilder res = new S Tringbuilder (). append (STRs [0]. Length ()> intvalue? 37 STRs [0]. substring (0, intvalue): STRs [0]); 38 res. append (STRs [1]. Length ()> pointvalue? STRs [1]. substring (0, pointvalue): STRs [1]); 39 return double. parsedouble (res. tostring (); 40} else if (SRC. matches ("[0-9] *") {41 return double. parsedouble (SRC. substring (0, intvalue); 42} else {43 return 0.0d; 44} 45} else {46 return double. parsedouble (SRC); 47} 48}
[1] specify the length of a string based on three Chinese characters. [2] truncation of the double type. Specify the length = integer + decimal place)