1

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.

  • you are not setting your string back to final_result. Need to do `final_result=final_result.replaceAll(",", "")` – SSK Aug 20 '20 at 14:08
  • What is the type of `result_stack` variable? If it is a type which supports `steram()` of elements you could simply call `String result = result_stack.stream().collect(Collectors.joining());` to get your result without `[` `]` and `,`. – Pshemo Aug 20 '20 at 14:32

5 Answers5

2

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 :)

Dan Rayson
  • 1,315
  • 1
  • 14
  • 37
  • Thank you! That's the simplest way!!! – CyberHunter Aug 20 '20 at 14:30
  • You're welcome :) I would consider looking into the other answers here as people have raised some valid points about how you ought to approach the problem. I merely did the simplest possible answer, they have gone into more detail and explained some more robust approaches. – Dan Rayson Aug 20 '20 at 14:44
1

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
Andreas
  • 154,647
  • 11
  • 152
  • 247
0

Use a regular expression to remove all symbols but letters

String final_result = String.valueOf(result_stack).replaceAll("[^a-zA-Z]", "");
grig
  • 848
  • 6
  • 15
0

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.

JustAnotherDeveloper
  • 2,061
  • 2
  • 10
  • 24
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
Flying Dutchman
  • 365
  • 6
  • 13