Question is this:
for an HTML page content, wherein the parsed all key-value pairs, where such type = "text", type attribute, text value, both as a key-value pair
as follows: <input type="text" class="s_ipt" name="wd" id="kw" maxlength="100" a="b" autocomplete="off">
get this problem we first thought was how to traverse, from start to finish how to take out all the sub-strings, symbols and how to eliminate them. First, the method of traversing like a long time, the result is not good enough, the following code, seeking to improve:
public static void main(String[] args) {
String zifu = "<input type=\"text\" class=\"s_ipt\" name=\"wd\" id=\"kw\" maxlength=\"100\" a=\"b\" autocomplete=\"off\">";
String zfsz[] = new String[10];
int a = 0;
for (int i = 0; i < zifu.length(); i++) {
if (' '==zifu.charAt(i)) {
for (int j = i+3; j < zifu.length(); j++) {
if (' '==zifu.charAt(j)||'>'==zifu.charAt(j)) {
zfsz[a] = zifu.substring(i+1, j);
String temp = zfsz[a].replaceAll("\\W+"," ");
System.out.println(temp);
a++;
break;
}
}
}
}
}
Think very hard, also wrote to burn the brain, and finally search for a moment about regular expressions, find the String class split method is to use regular expressions to split them into an array, with specific reference to the relevant API, I can not say here.
Code is modified as follows:
public static void main(String[] args) {
String zifu = "<input type=\"text\" class=\"s_ipt\" name=\"wd\" id=\"kw\" maxlength=\"100\" a=\"b\" autocomplete=\"off\">";
String regex0 = " ";
//首先,我们以空格将字符串拆分成字符数组
String[] result0 = zifu.split(regex0);
//然后对所有字符数组中的非字母元素进行替换
for (int i = 1; i < result0.length; i++) {
regex0 = result0[i].replaceAll("\\W+", " ");
System.out.println(regex0);
}
}
Ah, I am not looking at it more simple? There are better ways, can comment below, seek advice. .