File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Search for whitespace in a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Search for whitespace in a String" Watch "Search for whitespace in a String" New topic
Author

Search for whitespace in a String

Michael Cleary
Ranch Hand

Joined: Jul 29, 2003
Posts: 93
I'm having trouble searching for whitespace in a String. I've tried iterating through the string with a loop using <string>.charAt, but I don't know how to compare each iteration to a whitespace (" "). I can't use " == ", and I haven't had any luck using <string>.equals() either. I basically need something like: if(character is a whitespace) {
do something; }
I also tried assigning the string to a character array, and then looping throught the char array, but I still have trouble looking for a white space for some reason. It seems like it should be so simple...
Any thoughts would be greatly appreciated,
TIA,
Mike


Mike<br />SCJP 1.4<br />----------------------------<br />mdcleary@earthlink.net<br />----------------------------<br />There are 10 types of people<br />in the world. Those that <br />understand binary, and those<br />that don't.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8895
    
    8

Help me out here. What kind of output are you expecting? Are you trying to identify the whitespace or the words within a string? Is this an assignment where you have to show your work iterating through characters or can you use an API call which will do all the work for you? If the latter is the case and you want to obtain the words in a string, look closely at java.lang.String and java.util.StringTokenizer
[ October 07, 2003: Message edited by: Joe Ess ]

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
maneesh subherwal
Ranch Hand

Joined: Aug 26, 2002
Posts: 42
use the same charAt iteration and compare the charAt to ' ' i.e. in single quotes.
String s = "AB 123";
char spaceChar = s.charAt(2); // the value of spaceChar should be ' '
hope this helps
Thanks,
Maneesh
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Here's a little bit of code that might give you some ideas. There are many more ways to perform similar tasks that you might come across though.

Output will be:
this_is_a_string
this.is.a.string
this-is-a-string
[ October 07, 2003: Message edited by: Jason Menard ]
Michael Cleary
Ranch Hand

Joined: Jul 29, 2003
Posts: 93
Thanks for all the quick replies! Basically, the input will be a String, which is the concatenation or 4 separate Strings entered by a user (I add a space between each String entered by the user during the concatenation). What we're actually looking for is the last letter of each word in the concatenated String. We then create a new String consisting of the last letters for output.
So what I was trying to do was search the String for the whitespace between words, and then take the previous character. At the end, I add the last character from the String (since there may not be any white space at the end of the String). We have only looked at String right now, so I should probably not use StringBuffer or StringTokenizer.
I tried using something similar to the following (from Jason's post):
for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
if (c == ' ') {
c = '.';
}
}
Instead of c = '.', I had str += s.charAT(i -1);
//str was a previously declared String
I kept getting an outOfBounds exception - this confused me since even though I know this would happen if i = 0, there was never a white space at the beginning of the String, so the condition would not have been true (and therefore str = s.charAT(i - 1) should not have been evaluated). If I change the loop initializer to int i = 1, I avoided the exception. But my final string (str) ended up including most of the original string.
Anyway, I will play w/ it some more later.
Thanks again,
Mike
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
So what I was trying to do was search the String for the whitespace between words, and then take the previous character.
Ok, this leads us to some different approaches.
If you are using JSDK 1.4, you could take a look at the String.split() method. If you're not using JSDK 1.4, or even if you are, another thing you might look at is StringTokenizer.
String.split() is a little bit advanced because it uses regular expressions. A little easier to use, and maybe even more appropriate for this task is StringTokenizer, which might be worth looking into.
Michael Cleary
Ranch Hand

Joined: Jul 29, 2003
Posts: 93
Thanks - it's interesting to know about split(). That may come in very handy. It is somewhat similar to split in Perl, which I use all the time!
I actually got my method to work as follows:
public String lastLetter() {
s.trim();//trim any leading or trailing whitespace
String lst_ltr = "";

for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == ' ') {
lst_ltr += s.charAt(i -1);
}
}//for

lst_ltr += s.charAt(s.length() -1); //get last character of string
return lst_ltr;

}//end lastLetter
Thanks for all the help!
Mike
Michael Cleary
Ranch Hand

Joined: Jul 29, 2003
Posts: 93
Just had to add - I've already used split() in two other methods....
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Glad to hear you got it working. Just a quick style note... In Java, and unlike Perl, Strings are immutable. In other words, when you have a line like:

...unlike other languages, this isn't technically String concatenation in the sense you may be used to. Strings being immutable, what you are really doing is creating new String objects each iteration and just re-using the reference. To avoid this, a good habit to get into is to use a StringBuffer for this kind of iterative String concatenation. Take a look back at my code for an example.
I was a Perl hacker once as well. I had done nothing but Perl/CGI for a couple of years, and had very little Java knowledge/experience when I was dropped into a Servlet project. I tried to do things just like I did them in Perl (even wrote my own flock() and split() methods), and it was awhile before I caught on to some of the more subtle differences between the languages.
Thillai Sakthi
Ranch Hand

Joined: Jun 17, 2000
Posts: 102
Originally posted by Jason Menard:
If you are using JSDK 1.4, you could take a look at the String.split() method. If you're not using JSDK 1.4, or even if you are, another thing you might look at is StringTokenizer.

StringTokenizer would not be of much use here if the two words contain more than one space ''. Lets consider this:

For this, the output is still 'this','is','a','string', irrespective of number of white spaces in this string....Ofcourse, for this purpose of getting the last letter from each word, this is not important....just a point to ponder.....


Regards,
Shakthi
Michael Cleary
Ranch Hand

Joined: Jul 29, 2003
Posts: 93
Dohhh....Forgot about the immutability of Strings. Guess I'll make a few modifications.
Thanks,
Mike
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Search for whitespace in a String