Forums Register Login

Search for whitespace in a String

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Just had to add - I've already used split() in two other methods....
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.....
+Pie Number of slices to send: Send
Dohhh....Forgot about the immutability of Strings. Guess I'll make a few modifications.
Thanks,
Mike
If you have a bad day in October, have a slice of banana cream pie. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 19285 times.
Similar Threads
Comparing chars at positions in an array
Represent @ in xml file
Re: using javascript in jsp's
Using collection attribute of logic:iterate
Creating XML with DOM
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 21:33:24.