Solved

Removing Spaces, Punctuation in Java Code

Posted on 2004-09-24
18
3,085 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

786 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