Solved

Removing Spaces, Punctuation in Java Code

Posted on 2004-09-24
18
3,103 Views
Last Modified: 2011-09-20
Greetings!

Does anyone know how to implement the functionality to remove the spaces and punctuation from a String retrieved from a database?

For example, I am trying to remove all the spaces from the following fieldname, certificateName:

"Annual's - Disclosure Form: 2004"

and have it save instead as

"AnnualsDisclosureForms2004"

Does anyone know a quick and convenient way to do this?  

I'm looking for something similar to this:

 replaceAll("^a-zA-Z]", "")

but for punctuation and spaces.

Any help or advice would be great!!

Thanks!
jamie

0
Comment
Question by:jmc430
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
  • 3
  • +1
18 Comments
 
LVL 19

Expert Comment

by:ramazanyich
ID: 12147077
in java 1.4 you can do it:
String aa="Annual's - Disclosure Form: 2004";
aa=aa.replaceAll("\s","");
0
 

Author Comment

by:jmc430
ID: 12147182
is it the backward slash?  i get a compilation error with that - "\s"

thanks for your help ..
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12147203
String str="Annual's - Disclosure Form: 2004".replaceAll("\\s","").replaceAll(":","");
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12147215
rather

"Annual's - Disclosure Form: 2004".replaceAll("\\s","").replaceAll(":","").replaceApll("-","");
0
 
LVL 19

Expert Comment

by:ramazanyich
ID: 12147218
aa=aa.replaceAll("\\s","");
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12147221
rather
"Annual's - Disclosure Form: 2004".replaceAll("\\s","").replaceAll(":","").replaceAll("-","")
0
 
LVL 19

Accepted Solution

by:
ramazanyich earned 70 total points
ID: 12147241
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12147280
String str="Annual's - Disclosure Form: 2004";
str=str.replaceAll("[^a-zA-Z]", "");
0
 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12152516
You missed the digits.  Try this:

String str="Annual's - Disclosure Form: 2004";
str=str.replaceAll("[^a-zA-Z0-9]", "");
0
 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12152558
jmc430, just for your info, sudhakar_koundinya's solution (with the digits, as I added) is elegant because is replaces everything that isn't a letter or digit with the empty String.

Incidentally, you could also use the alphanumeric symbol (\w) or the non-alphanumeric symbol (\W).  The only gotcha there is that they match [a-zA-Z0-9_] and [^a-zA-Z0-9_] respectively.  So they're only good if you want to match or not match underscores.
0
 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12152567
So, this will remove everything everything except letters, numbers and the underscore:

String str="Annual's - Disclosure Form: 2004";
str=str.replaceAll("\W", "");
0
 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12152657
Oops, always forget to escape my slashes.  Use "\\W".

String str="Annual's - Disclosure Form: 2004";
str=str.replaceAll("\\W", "");
0
 
LVL 4

Assisted Solution

by:Rick_Townsend
Rick_Townsend earned 90 total points
ID: 12152694
FYI, don't forget to escape backslahes (ie: "\" becomes "\\")in your _original string_, prior to it being handled by replaceAll().  If you have any un-escaped slashes, the regex handler will try to do back-substitution.  Post a question if you need more info on this.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12153322
>> You missed the digits.  Try this:

He/She should require this

"Annual's - Disclosure Form: 2004"
and have it save instead as
"AnnualsDisclosureForms2004"
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12153333

>>str=str.replaceAll("\\W", "");

that is also quite good expression
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12153342
but ignoring digits
0
 
LVL 14

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 90 total points
ID: 12153351
oops sorry, Ignore  my previous comments

 str=str.replaceAll("\\W", "");
            or
str=Hello Annual's - Disclosure Form: 2004".replaceAll("[^a-zA-Z0-9]", "");

0
 

Author Comment

by:jmc430
ID: 12160887
thanks for all of your help!  i really appreciate it!  it works ... yay!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Html Table Looping (part 2) 5 35
junit example issue 2 25
Java Inheritance super keyword use 8 35
Strange router problem - can't access hotmail.com 14 53
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question