![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Programmer
Join Date: Jun 2005
Location: Amittyville
Posts: 60
Rep Power: 4
![]() |
finding the number of spaces in a String
Is there anyway to find the number of spaces in a String? Thanx!
__________________
I steal hippos... |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Lets say you have String a:
String [] temp = a.split(" ");
System.out.println(temp.length);That would be an easy way to do it. You could look through and count if you wanted, as well with a loop.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower Last edited by Mjordan2nd; Jun 27th, 2005 at 5:38 PM. |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Jun 2005
Location: Amittyville
Posts: 60
Rep Power: 4
![]() |
perfect!
thanx!
__________________
I steal hippos... |
|
|
|
|
|
#4 | |
|
Newbie
Join Date: Jun 2005
Posts: 8
Rep Power: 0
![]() |
Quote:
If string a is "hello world" then temp's length will be 2 since it will split the string at the space. Obviously there is only one space. It should be System.out.println(temp.length - 1); |
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4
![]() |
those wont capture spaces at the end of a string.
String a = "hello world "; // 3 spaces count = a.split(" ").length-1; // count == 1for (int i=0; i<a.length(); i++) {
if(a.charAt(i)==' ')
count++;
} // count == 3
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand." - B. Russell http://web.bryant.edu/~srk2 |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 328
Rep Power: 4
![]() |
...heh heh heh...or:
while (i < a.length()) count += a.charAt(i++) == ' ' ? 1 : 0; Sorry, the evil laugh was because that's just a less readable version. I can never resist the opportunity to use the post-increment or the compact conditional to reduce the clarity of perfectly good C or C-like code. ![]() |
|
|
|
|
|
#7 | |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Yea, temp.length-1. I didn't think of that.
Quote:
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|