aspose file tools*
The moose likes Beginning Java and the fly likes Removing Characters from String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Removing Characters from String" Watch "Removing Characters from String" New topic
Author

Removing Characters from String

Conrad McLaughlin
Greenhorn

Joined: Jan 31, 2006
Posts: 27
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

Joined: Jan 31, 2006
Posts: 27
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

Joined: Jan 31, 2006
Posts: 27
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

Joined: Feb 07, 2005
Posts: 2367
Strings are immutable. You need to capture the output of the method.
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Hello,
Maybe this methods help you to understand the logic.

memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
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

Joined: Oct 02, 2003
Posts: 10908
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
jQuery in Action, 2nd edition
 
subject: Removing Characters from String
 
Similar Threads
Clipboard/Tokenizer -- copy and paste problem
How to read the complete contents of a file in a String?!
Math.Random() query
how to know the no of occurance in a string
splitting a string with multiple delimiters