0.介绍
拼音搜索常用与电话本或音视频查找,如果输入“zs” “zhangsan"期望能够出现”张三“ ”zhang San”。
1.提取关键词
将”张三“ ”zhang San”提取成”张“”三“ ”zhang“ ”San”。
代码如下
private static final String KONG = "";
private static final char SPACE = ' ';
public static String getStringKeyWord(String str) {
String[] strArrary = new String[str.length()];
// int
int index = converToArray(str, strArrary);
for (int i = 0; i < index; i++) {
System.out.print("getStringKeyWord i:" + i + " strArray:" + strArrary[i] + "\n");
}
return null;
}
private static int converToArray(String str, String[] strArray) {
int index = 0;
String strTemp = KONG;
for (int i = 0; i < str.length(); i++) {
char word = str.charAt(i);
if (isEnglishOrDigit(word)) {
strTemp += Character.toString(word);
} else if (isSpace(word)) {
if (!KONG.equals(strTemp)) {
strArray[index++] = strTemp;
strTemp = KONG;
}
} else {
if (!KONG.equals(strTemp)) {
strArray[index++] = strTemp;
strTemp = KONG;
}
strTemp += Character.toString(word);
if (!KONG.equals(strTemp)) {
strArray[index++] = strTemp;
strTemp = KONG;
}
}
if (i == str.length() - 1) {
if (!KONG.equals(strTemp)) {
strArray[index++] = strTemp;
strTemp = KONG;
}
}
}
return index;
}
private static boolean isEnglishOrDigit(char word) {
if (('0' <= word && word <= '9') || ('a' <= word && word <= 'z') || ('A' <= word && word <= 'Z')) {
return true;
}
return false;
}
private static boolean isSpace(char word) {
if (SPACE == word) {
return true;
}
return false;
}
2.先记录下,后续在整理上传