public class SplitString {
/**
* @param args
*/
public static void main(String[] args) {
String str = "中Zell中国5000年,Congruations!";
int bytes = 8;
str = new SplitString().getSubString(str, bytes);
System.out.println(str);
}
public String getSubString(String str,int bytes){
int temp = bytes;
byte [] AllBytes = str.getBytes();
int AllStrByteLen = AllBytes.length;
if(bytes > AllStrByteLen | bytes <= 0){
return str;
}
int ChinaWordsNum = 0;
for(int i=0;i<temp;i++){
int IntOfByte = (int)AllBytes[i];
if(IntOfByte < 0 ){
ChinaWordsNum+=1;
}
}
if(ChinaWordsNum%2 == 0){
return new String(AllBytes,0,temp);
}else{
if(bytes == 1){
return null;
}
}
return new String(AllBytes,0,temp-1);
}
public String getSubString(String SubjectString, int start, int length){
String ResultString = null;
try {
Pattern regex = Pattern.compile("^.{" + start + "}(.{" + length + "}).*$");
Matcher regexMatcher = regex.matcher(SubjectString);
System.out.println(regexMatcher.matches());
try {
ResultString = regexMatcher.replaceAll("$1");
} catch (IllegalArgumentException ex){
} catch (IndexOutOfBoundsException ex){
}
}catch(PatternSyntaxException ex){
}
return ResultString;
}
}
本文介绍了一个Java程序,用于从给定字符串中提取包含特定数量中文字符的子串。通过计算字符串中中文字符的数量,并根据指定的字节长度调整子串的输出,实现了对字符串的有效切割。
236

被折叠的 条评论
为什么被折叠?



