• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Removing Characters from String

 
Conrad McLaughlin
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the best way to remove unwanted characters from a string?

* So if the user enters String A, String B is created with unwanted characters removed from A.


String A = "Hello. How Are You?";
THEN
String B = "Hello How Are You";


I have tried something like this:



But I get an lang string array error for some reason.
 
Conrad McLaughlin
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I went on a website and looked at replaceAll method. But when I compile this why is String A is exactly the same?

 
Conrad McLaughlin
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I went on a website and looked at replaceAll method. But when I compile this why is String A is exactly the same?

 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings are immutable. You need to capture the output of the method.
 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Maybe this methods help you to understand the logic.

 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by memati bas:
Hello,
Maybe this methods help you to understand the logic.




Oh soory for the wrong code. The developed codes is at the below.

 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To state it a little more explicitly, once a string is created, it CANNOT be changed. So what good are all those "replaceAll()" type methods???

simple. they RETURN a NEW string. so when you call

A.replaceAll(".","");

string A is not changed,, but a brand new string is created where the "." have been replaced with "". you need some new variable, say "String B", and then write this:

String B = A.replaceAll(".","");

A will still contain the original string, and B will be the string you want.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic