?
Solved

Removing Spaces, Punctuation in Java Code

Posted on 2004-09-24
18
Medium Priority
?
3,157 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
  • 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 280 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 360 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 360 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

621 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