?? cutstringutil.java
字號(hào):
package test;
public class CutStringUtil {
/**
* 判斷一個(gè)字符是Ascill字符還是其它字符(如漢,日,韓文字符)
*
* @param char
* c, 需要判斷的字符
* @return boolean, 返回true,Ascill字符
*/
public static boolean isLetter(char c) {
int k = 0x80;
return c / k == 0 ? true : false;
}
/**
* 得到一個(gè)字符串的長(zhǎng)度,顯示的長(zhǎng)度,一個(gè)漢字或日韓文長(zhǎng)度為2,英文字符長(zhǎng)度為1
*
* @param String
* s ,需要得到長(zhǎng)度的字符串
* @return int, 得到的字符串長(zhǎng)度
*/
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;
}
/**
* 截取一段字符的長(zhǎng)度,不區(qū)分中英文,如果數(shù)字不正好,則少取一個(gè)字符位
*
* @author patriotlml
* @param String
* origin, 原始字符串
* @param int
* len, 截取長(zhǎng)度(一個(gè)漢字長(zhǎng)度按2算的)
* @return String, 返回的字符串
*/
public static String substring(String origin, int len) {
if (origin == null || origin.equals("")||len<1)
return "";
byte[] strByte = new byte[len];
if (len > length(origin)){
return origin;}
System.arraycopy(origin.getBytes(), 0, strByte, 0, len);
int count = 0;
for (int i = 0; i < len; i++) {
int value = (int) strByte[i];
if (value < 0) {
count++;
}
}
if (count % 2 != 0) {
len = (len == 1) ? ++len : --len;
}
return new String(strByte, 0, len);
}
/**
* @param args
*/
public static void main(String[] args) {
String str=CutStringUtil.substring("ing中i華en人|en民ing",11);
System.out.println(str);
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -