• 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

Determine the space occurrence within a String

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
In general I use this syntax to determine the amount of character occurrence within a String:

But what about whitecharacter? How can I determine how many time a space(between the words) occur in within the string?
{code plase);
thanks in advance
Shay Gaghe

[This message has been edited by Shay Gaghe (edited October 15, 2001).]
 
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will this work?

To eliminate counting of space characters at the beginning and end of the string, you just need to check which character you're comparing:

If you want to check for all whitespace characters, you can use Character.isWhitespace(char). to check each character.
[This message has been edited by Matt Senecal (edited October 15, 2001).]
[This message has been edited by Matt Senecal (edited October 15, 2001).]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
simple...just use ' '
public class myChar {
public static void main(String []argh){
String s = "I have Son my Son has Son ";
int records=0;
System.out.println("length=>"+ s.length());
for(int i=0;i < s.length();i++){ <br /> if(s.charAt(i)== ' ') <br /> records++; <br /> } <br /> <br /> System.out.println("the amount of S are=>"+ records);
}
}
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even simpler... just use
Spaces at the start and the end of the string are ignored. To get the number of whitespace chunks, simply subtract one from the number of words (assuming you are interested in the number of white spaces rather than the number of space characters). Have I won a prize now?
- Peter

[This message has been edited by Peter den Haan (edited October 16, 2001).]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter's solution also assumes that you are interested in the number of whitespace "chunks" rather than the number of discrete whitespace characters. If you have two consecutive spaces, they will be counted as one. Same for a "\n\r" or " \n\r" at the end of a line. This may be exactly what you want, or not - it's important to be aware of the issues. A few more alternatives to be aware of: you can modify the list of delimiters used by the StringTokenizer.
<code><pre>
public int countWords(String s) {
return new StringTokenizer(s, " ").countTokens();
}
</pre></code>
This StringTokenizer ignores \n, \r, \t, \f, and only pays attention to ' ' as a delimiter. But it still counts consecutive spaces as one.
If you don't like counting multiple characters as one, but do want to count other forms of whitespace as well, the simple solution is
<code><pre>
public int countWhiteSpace(String input) {
int count = 0;
for (int i = 0; len = input.length(); i < len; i++)
if (Character.isWhitespace(input.charAt(i)))
count++;
return count;
}
</pre></code>

[This message has been edited by Jim Yingst (edited October 16, 2001).]
reply
    Bookmark Topic Watch Topic
  • New Topic