1. 程式人生 > >Java邏輯代碼判斷字數

Java邏輯代碼判斷字數

temp else 字數 nes ati true count 中文 com

package cn.com.f_young.erp.util;

public class WorldTrueCount {
/**
* 判斷中文和標點符號的邏輯代碼
* @param value
* @return
*/
public static int String_length_chinese(String value) {
int valueLength = 0;
String chinese = "[\u4e00-\u9fa5]";
String english = "[a-zA-Z]";
for (int i = 0; i < value.length(); i++) {
String temp = value.substring(i, i + 1);
if(temp.equals(" ")){
valueLength+=0;
}else if (temp.matches(chinese)) {
valueLength += 1;
}else if(temp.matches(english)) {
valueLength += 0;
}else{
valueLength += 1;
}
}
return valueLength;
}
/**
* 判斷英文的邏輯代碼
* @param value
* @return
*/
public static int String_length_english(String value) {
int valueLength = 1;
String chinese = "[\u4e00-\u9fa5]";
String english = "[a-zA-Z]";
int k=0;
for (int i = 0; i < value.length(); i++) {
String temp = value.substring(i, i + 1);
if(temp.equals(" ")){
if(k==1){
valueLength+=1;
k=0;
}
valueLength+=0;
}else if (temp.matches(chinese)) {
if(k==1){
valueLength+=1;
k=0;
}
valueLength += 0;
}else if(temp.matches(english)) {
k = 1;
valueLength += 0;
}else{
if(k==1){
valueLength+=1;
k=0;
}
valueLength += 0;//標點符號
}
}
return valueLength;
}


}

Java邏輯代碼判斷字數