This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 18546

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
It is sorta covered in the JavaRanch Style Guide.
subject: clearing white space
Similar Threads
sorting contents of strings
Some tips to create these methods
charAt with longs?
regex Pattern class and spaces