File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "clearing white space" Watch "clearing white space" New topic
Author

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.

thanks
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
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

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



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
Consider Paul's rocket mass heater.
 
subject: clearing white space