aspose file tools*
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

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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?
Similar Threads
Problems using/making method!
Reading every line from a file to string
cutting string into peaces
I Need help with file handling
Reading txt file where tab is used?