This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
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).]
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
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); } }
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).]
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Determine the space occurrence within a String