File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes clearing white space Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "clearing white space" Watch "clearing white space" New topic

clearing white space

Chris Dancy
Ranch Hand

Joined: Feb 14, 2006
Posts: 136
got a quick question. Im trying to clear all the white space from a string. All other characters are ok, just the white space has to go. I've looked online and found nothing useful, I keep getting char cannot be dereferenced errors. here is the code i tried
public String clearWhiteSpace(String si){
char [] temp = si.toCharArray();
String s = "";
for(int i = 0;i < temp.length;i++){
if(!temp[i].equals(' '))
s += temp[i];
return s;

Im running out of ideas, if anyone can help, let me know.

Chris Dancy

("Anger is not an emotion, its a symptom of fear.")
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Primitives dont have methods. To compare two primitives you use ==.

if(!temp[i] == ' ')

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Chris Dancy
Ranch Hand

Joined: Feb 14, 2006
Posts: 136
ahahaha. Stupid mistake on my part. I should have realized. thanks
Henry Wong

Joined: Sep 28, 2004
Posts: 20516

You can also remove spaces from the string, like this...


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: clearing white space
It's not a secret anymore!