Solved

Removing Spaces, Punctuation in Java Code

Posted on 2004-09-24
18
3,079 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now