File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Determine the space occurrence within a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Determine the space occurrence within a String " Watch "Determine the space occurrence within a String " New topic
Author

Determine the space occurrence within a String

Shay Gaghe
Ranch Hand

Joined: Sep 03, 2001
Posts: 102
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

Joined: Dec 01, 2000
Posts: 255
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).]


There's nothing more dangerous than a resourceful idiot. ---Dilbert
Nirvana_417
Greenhorn

Joined: Oct 15, 2001
Posts: 4
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

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Jan 30, 2000
Posts: 18671
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).]


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Determine the space occurrence within a String