英文:
Removing Commas and Square Brackets from Stack or String in Java Android Studio
问题
我正在处理一些Android Studio项目。我有一个问题。
我有一个名为result_stack的堆栈。-> result_stack = [K,T,E,J,O,S,O,G,O,J]
我尝试将此堆栈转换为字符串。-> String final_result = String.valueOf(result_stack);
现在,当我尝试打印final_result。-> System.out.println(final_result);
-> 它打印出[K,T,E,J,O,S,O,G,O,J]
但是我想摆脱所有的逗号,方括号和空格。我的意思是,我想要打印出像这样的内容:"KTEJOSOGOJ",然后我将其设置为一个TextView。
我尝试了这个,但什么都没有改变:
final_result.replaceAll(",", "");
System.out.println(final_result);
我仍然得到[K,T,E,J,O,S,O,G,O,J]作为输出。请帮我解决这个问题。
英文:
I'm working on some anroid studio projects. I have a problem.
I have a stack named result_stack. -> result_stack = [K, T, E, J, O, S, O, G, O, J]
I tried to convert this stack in to string -> String final_result = String.valueOf(result_stack);
Now when I try to print final_result -> System.out.println(final_result);
-> it prints [K, T, E, J, O, S, O, G, O, J]
But I want to get rid of all commas, square brackets and spaces. I mean, I want to print it like this "KTEJOSOGOJ" and then I'll set this as a textview.
I tried this but nothing has changed:
final_result.replaceAll(",", "");
System.out.println(final_result);
I still get [K, T, E, J, O, S, O, G, O, J] as output. Please help me with this.
答案1
得分: 2
因为 Strings
是不可变的(一旦创建,就不会改变),所以你仍然看到旧值。
解决方法是将你的 final_result
变量设置为 replaceAll
方法调用的结果。有点奇怪,我知道。
像这样: final_result = final_result.replaceAll("","");
附注:你还需要替换空格和括号,但你会解决的
英文:
The reason you're still seeing the old value is because Strings
are immutable (once they've been created, they don't change.)
The solution is to set your final_result
variable to the result of your replaceAll
method call. Strange, I know.
Like this: final_result = final_result.replaceAll(",", "");
p.s. You'll need to replace the spaces and the brackets too, but you'll figure that out
答案2
得分: 1
以下是翻译好的部分:
不要调用 `String.valueOf(result_stack)` 或 `result_stack.toString()`,尝试“修复”结果。正确构建字符串。
Stack<String> result_stack = new Stack<>();
result_stack.addAll(Arrays.asList("K", "T", "E", "J", "O", "S", "O", "G", "O", "J"));
// 错误的方式,不要这样做
String final_result1 = String.valueOf(result_stack);
System.out.println(final_result1);
// 正确的方式(Java 8+)
String final_result2 = result_stack.stream().collect(Collectors.joining());
System.out.println(final_result2);
// 正确的方式(Java 1.5+)
StringBuilder buf = new StringBuilder();
for (String value : result_stack)
buf.append(value);
String final_result3 = buf.toString();
System.out.println(final_result3);
输出
[K, T, E, J, O, S, O, G, O, J]
KTEJOSOGOJ
KTEJOSOGOJ
英文:
Don't call String.valueOf(result_stack)
or result_stack.toString()
, and try to "fix" the result. Build the string correctly instead.
Stack<String> result_stack = new Stack<>();
result_stack.addAll(Arrays.asList("K", "T", "E", "J", "O", "S", "O", "G", "O", "J"));
// The wrong way, don't do this
String final_result1 = String.valueOf(result_stack);
System.out.println(final_result1);
// The correct way (Java 8+)
String final_result2 = result_stack.stream().collect(Collectors.joining());
System.out.println(final_result2);
// The correct way (Java 1.5+)
StringBuilder buf = new StringBuilder();
for (String value : result_stack)
buf.append(value);
String final_result3 = buf.toString();
System.out.println(final_result3);
Output
[K, T, E, J, O, S, O, G, O, J]
KTEJOSOGOJ
KTEJOSOGOJ
答案3
得分: 0
使用正则表达式去除所有符号,只保留字母。
String final_result = String.valueOf(result_stack).replaceAll("[^a-zA-Z]", "");
英文:
Use a regular expression to remove all symbols but letters
String final_result = String.valueOf(result_stack).replaceAll("[^a-zA-Z]", "");
答案4
得分: 0
我用一个数组而不是堆栈来完成了它,但是如果在将堆栈转换为字符串时得到了方括号和逗号,同样的思路适用,你只需要一些对 replace()
函数的调用:
import java.util.*;
public class MyClass {
public static void main(String args[]) {
String[] array = {"K", "T", "E", "J", "O", "S", "O", "G", "O", "J"};
System.out.println(Arrays.toString(array).replace(",", "").replace("[", "").replace("]", "").replace(" ", ""));
}
}
这将打印出:KTEJOSOGOJ
不过,你也可以通过调用 substring()
函数来实现相同的效果,而不是这么多次地调用 replace()
。请记住,在Java中,字符串是不可变的,因此除非将调用的结果赋值给类型为String的对象,否则您需要将它们链接为另一个方法的输入,就像我所做的那样。
英文:
I did it with an array instead of a stack, but if you get the square brackets and commas when you convert the stack to a String, the same idea applies, you just need some calls to replace()
:
import java.util.*;
public class MyClass {
public static void main(String args[]) {
String[] array = {"K", "T", "E", "J", "O", "S", "O", "G", "O", "J"};
System.out.println(Arrays.toString(array).replace(",","").replace("[","").replace("]","").replace(" ",""));
}
}
This prints the following: KTEJOSOGOJ
Then again, you can achieve the same thing with a call to substring()
instead of so many calls to replace()
. Remember that strings are immutable in Java so you need to assign the results of methods you call on an object of type String unless you chain them as input for another method like I did.
答案5
得分: 0
String HomeName = "[K, T, E, J, O, S, O, G, O, J]";
String Replace = HomeName.replaceAll("[^a-zA-Z0-9\\\\s+]", "");
UserName.setText(Replace);
Output
KTEJOSOGOJ
英文:
String HomeName = "[K, T, E, J, O, S, O, G, O, J]";
String Replace = HomeName.replaceAll("[^a-zA-Z0-9\\\\s+]","");
UserName.setText(Replace);
Output
KTEJOSOGOJ
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论