`
linleizi
  • 浏览: 227561 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

中英文字符串截取

 
阅读更多
下面两个方法可能不是大家想要的,大家可以根据自己的需要进行修改,希望有所帮助……
/**
* @param test 要截取的字符串
            * @param m 要截取长度
* @function 返回截取的字符串
*/
public static String subString(String test, int m) {
if (test == "") {
return "";
}
byte[] strBytes = test.getBytes();
int strLen = strBytes.length;
if (m >= strLen || m < 1)  {
return test;
}
int count = 0;
for (int i = 1; i < m; i++) {
int value = (int) strBytes[i];
if (value < 0) {
count++;
}
}
if (count % 2 != 0) {
m = (m == 1) ? m + 1 : m - 1;
}
return test.substring(0, m);
}

/**
* @param test 要截取的字符串
            * @param m 要截取长度
* @function 返回截取的字符串
*/
public static String subStr(String test, int m) {
if (test == "") {
return "";
}
if (test.length() <= m) {
return test;
}
String regex = "[a-z][A-Z]";
Pattern p = Pattern.compile(regex);
char strCha = test.charAt(m - 1);
Matcher ma = p.matcher(strCha + "");
if (!ma.matches()) {
m = m - 1;
}
return test.substring(0,m);
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics