This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Determine the space occurrence within a String

 
Shay Gaghe
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • 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).]
 
Matt Senecal
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • 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).]
 
Nirvana_417
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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);
}
}
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • 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).]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic