Solved

Removing Spaces, Punctuation in Java Code

Posted on 2004-09-24
18
3,074 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
Comment Utility
in java 1.4 you can do it:
String aa="Annual's - Disclosure Form: 2004";
aa=aa.replaceAll("\s","");
0
 

Author Comment

by:jmc430
Comment Utility
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
Comment Utility
String str="Annual's - Disclosure Form: 2004".replaceAll("\\s","").replaceAll(":","");
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
rather

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

Expert Comment

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

Expert Comment

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

Accepted Solution

by:
ramazanyich earned 70 total points
Comment Utility
0
 
LVL 14

Expert Comment

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

Expert Comment

by:Rick_Townsend
Comment Utility
You missed the digits.  Try this:

String str="Annual's - Disclosure Form: 2004";
str=str.replaceAll("[^a-zA-Z0-9]", "");
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Expert Comment

by:Rick_Townsend
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Comment Utility

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

that is also quite good expression
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
but ignoring digits
0
 
LVL 14

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 90 total points
Comment Utility
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
Comment Utility
thanks for all of your help!  i really appreciate it!  it works ... yay!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

762 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

10 Experts available now in Live!

Get 1:1 Help Now