Java 在android中过滤文本视图中的文本
有没有办法在android中从TextView中删除整数。例如,假设我们有这样的文本: 123Isuru456Ranasinghe 我希望这个文本在删除整数后是这样的 伊斯鲁拉那辛赫 如何在android中实现这一点?这将对您有所帮助Java 在android中过滤文本视图中的文本,java,android,Java,Android,有没有办法在android中从TextView中删除整数。例如,假设我们有这样的文本: 123Isuru456Ranasinghe 我希望这个文本在删除整数后是这样的 伊斯鲁拉那辛赫 如何在android中实现这一点?这将对您有所帮助 public static String removeDigits(String text) { int length = text.length(); StringBuffer buffer = new StringBuffe
public static String removeDigits(String text) {
int length = text.length();
StringBuffer buffer = new StringBuffer(length);
for(int i = 0; i < length; i++) {
char ch = text.charAt(i);
if (!Character.isDigit(ch)) {
buffer.append(ch);
}
}
return buffer.toString();
}
返回带删除数字的字符串。这只是纯java。与安卓无关。
StringBuilder ans = new StringBuilder();
char currentChar;
for (int i = 0; i < str.length(); ++i) {
currentChar = str.charAt(i);
if (Character.isLetter(currentChar)) {
ans.append(currentChar);
}
}
这是你想要做什么的代码
String str = "123Isuru456Ranasinghe";
String newStr = str.replaceAll("[0-9]", "");
经过一些测试,似乎最长的解决方案在性能方面是最好的
public static void main(String[] arg) throws IOException {
// Building a long string...
StringBuilder str = new StringBuilder();
for (int i = 0; i < 1000000; i++)
str.append("123Isuru456Ranasinghe");
removeNum1(str.toString());
removeNum2(str.toString());
}
// With a replaceAll => 1743 ms
private static void removeNum1(String _str) {
long start = System.currentTimeMillis();
String finalString = _str.replaceAll("[0-9]", "");
System.out.println(System.currentTimeMillis() - start);
}
// With StringBuilder and loop => 348 ms
private static void removeNum2(String _str) {
long start = System.currentTimeMillis();
StringBuilder finalString = new StringBuilder();
char currentChar;
for (int i = 0; i < _str.length(); ++i) {
currentChar = _str.charAt(i);
if (Character.isLetter(currentChar)) {
finalString.append(currentChar);
}
}
System.out.println(System.currentTimeMillis() - start);
}
使用循环要快得多。
但在你的情况下,这是一个有点无用的:p
现在你必须在慢写和短写以及非常快但有点复杂之间做出选择。这一切都取决于你需要什么。不应该是如果!Character.isDigitch?您还可以考虑重命名该方法以删除数字。该函数保留数字而不是字符,因此您应该输入一个!在Character.isDigitch之前获取字符。-编辑:我想我有点太慢了:如果您想在处理长字符串的过程中优化应用程序的性能,这个解决方案非常好。
public static void main(String[] arg) throws IOException {
// Building a long string...
StringBuilder str = new StringBuilder();
for (int i = 0; i < 1000000; i++)
str.append("123Isuru456Ranasinghe");
removeNum1(str.toString());
removeNum2(str.toString());
}
// With a replaceAll => 1743 ms
private static void removeNum1(String _str) {
long start = System.currentTimeMillis();
String finalString = _str.replaceAll("[0-9]", "");
System.out.println(System.currentTimeMillis() - start);
}
// With StringBuilder and loop => 348 ms
private static void removeNum2(String _str) {
long start = System.currentTimeMillis();
StringBuilder finalString = new StringBuilder();
char currentChar;
for (int i = 0; i < _str.length(); ++i) {
currentChar = _str.charAt(i);
if (Character.isLetter(currentChar)) {
finalString.append(currentChar);
}
}
System.out.println(System.currentTimeMillis() - start);
}