Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 27th, 2005, 5:24 PM   #1
squishiful
Programmer
 
Join Date: Jun 2005
Location: Amittyville
Posts: 60
Rep Power: 4 squishiful is on a distinguished road
finding the number of spaces in a String

Is there anyway to find the number of spaces in a String? Thanx!
__________________
I steal hippos...
squishiful is offline   Reply With Quote
Old Jun 27th, 2005, 5:28 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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.
Mjordan2nd is offline   Reply With Quote
Old Jun 27th, 2005, 5:30 PM   #3
squishiful
Programmer
 
Join Date: Jun 2005
Location: Amittyville
Posts: 60
Rep Power: 4 squishiful is on a distinguished road
perfect!
thanx!
__________________
I steal hippos...
squishiful is offline   Reply With Quote
Old Jul 6th, 2005, 1:14 PM   #4
VigilanteP@comcast.net
Newbie
 
Join Date: Jun 2005
Posts: 8
Rep Power: 0 VigilanteP@comcast.net is on a distinguished road
Quote:
Originally Posted by Mjordan2nd
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.
That won't work.

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);
VigilanteP@comcast.net is offline   Reply With Quote
Old Jul 6th, 2005, 2:09 PM   #5
skuinders
Hobbyist Programmer
 
skuinders's Avatar
 
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4 skuinders is on a distinguished road
those wont capture spaces at the end of a string.

String a = "hello world  "; // 3 spaces
count = a.split(" ").length-1; // count == 1
I think the classic approach is probably the way to go:
for (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
skuinders is offline   Reply With Quote
Old Jul 6th, 2005, 5:06 PM   #6
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 335
Rep Power: 4 mackenga is on a distinguished road
...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.
mackenga is offline   Reply With Quote
Old Jul 6th, 2005, 6:43 PM   #7
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Yea, temp.length-1. I didn't think of that.

Quote:
Originally Posted by skuinders
those wont capture spaces at the end of a string.
And yea, didn't quite think of that either. I guess the loop would be the way to go then.
__________________
&quot;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.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:48 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC