开发中我们有可能会遇到这种情况,就是将字符串中的某个字符去掉
php代码
@test
public void test() {
string d = trimpunctuation2("你,好oewefo,21.2!;:、1?dsf");
system.out.println(d);
}
// 将字符串中的标点符号过滤掉
public static string trimpunctuation2(string str) {
string punct[] = { ",", ".", "!", "?", ";", ":", ",", "。", "!", "?",
";", ":", "、" };
list<string> punctlist = arrays.aslist(punct); // 将string数组转list集合
stringbuilder result = new stringbuilder();
for (int i = 0; i < str.length(); i++) {
character c = str.charat(i);
if (punctlist.contains(c.tostring())) {
} else {
result.append(str.charat(i));
}
}
return result.tostring();
}
// 将字符串中的标点符号过滤掉
public static string trimpunctuation(string str) {
stringbuilder result = new stringbuilder();
for (int i = 0; i < str.length(); ++i) {
char punct[] = { ',', '.', '!', '?', ';', ':', ',', '。', '!', '?',
';', ':', '、' };
boolean need_filter = false;
for (int j = 0; j < punct.length; ++j) {
if (punct[j] == str.charat(i)) {
need_filter = true;
break;
}
}
if (!need_filter) {
result.append(str.charat(i));
}
}
return result.tostring();
}