This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to cut string into equal length peaces? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to cut string into equal length peaces?" Watch "How to cut string into equal length peaces?" New topic

How to cut string into equal length peaces?


Joined: Nov 11, 2001
Posts: 2
If I have a string "__this__is_not__easy_"
How do I get the length of the first word
and cut the rest of the string into equal
length peaces like this.
(_ = space)
result should be:
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
I'm unclear on the rules that apply to breaking up your input string, "~~this~~is~not~~easy~", where "~" stands for a space. So I'll proceed on the assumptions I've gleaned from answers to these questions:
1) Do the leading spaces (here, the two before "this") count in the length of the first word?
2) Does the first word end with the "s" in "this" or with the last space before "is"?
3) If there are multiple spaces between words, should they be included in the pieces of the string as well?
It would seem from the problem as you've described it that the answers to these questions would be "No", "the 's' in 'this'", and "No". If these assumptions are correct, then you need to:
A. Remove all duplicate spaces from your input string, so that "~~this~~is~not~~easy~" would become "this~is~not~easy".
B. Get the length of the first string.
C. Use the substring() method in a loop to return pieces of the string that are as long as the value in B, making sure that the first character of the substring is not a space, and indexing accordingly.
Hope this helps, MacTosh.
I agree. Here's the link:
subject: How to cut string into equal length peaces?
It's not a secret anymore!