• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Search for whitespace in a String

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just had to add - I've already used split() in two other methods....
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.....
 
Michael Cleary
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dohhh....Forgot about the immutability of Strings. Guess I'll make a few modifications.
Thanks,
Mike
 
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic