Results 1 to 4 of 4
- 12-15-2007, 01:13 AM #1
Member
- Join Date
- Dec 2007
- Posts
- 2
- Rep Power
- 0
How to split a string into multiple lines of x characters each
Hi
I want to split a string into multiple lines of 'x' characters without breaking a word. That is, for example, if x = 13, if the 13th character falls in the middle of a word, this word should not be considered for the first line, but must go into the next line.
Example:
If the string is "The quick fox jumped over a lazy dog", and I want to break this into multiple line of 13 characters, it should be like this:
The quick fox
jumped over a
lazy dog
How can I achieve this in java?
Thanks
J
- 12-15-2007, 02:38 AM #2
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
Java Code:String s = "The quick fox jumped over a lazy dog"; char[] sAr = s.toCharArray(); int start = 0; // start with for (int i = 13; i < sAr.length; i++) { if (sAr[i] == ' ') { System.out.println(s.substring(start, i)); start = i+1; i += 13; } } System.out.println(s.substring(start));
- 12-15-2007, 06:40 AM #3
Member
- Join Date
- Dec 2007
- Posts
- 2
- Rep Power
- 0
Hi staykovmarin
Thanks for your help.
Though this works well, the problem in this program is that if the 13th character is not a space, but a word, the word is included in the line which makes the total number of characters in the line greater than 13. My requirement is that it has to be exactly or less than 13 without splitting the word.
Thank you.
- 12-17-2007, 02:35 AM #4
Java Code:String s = //"The quick fox jumped over a lazy dog"; "The Java language provides special support for the string " + "concatenation operator ( + ), and for conversion of other " + "objects to strings. String concatenation is implemented " + "through the StringBuilder(or StringBuffer) class and its " + "append method. String conversions are implemented through " + "the method toString, defined by Object and inherited by " + "all classes in Java."; int charLimit = 13; System.out.println(" 111"); System.out.println("0123456789012"); System.out.println("-------------"); char[] chars = s.toCharArray(); boolean endOfString = false; int start = 0; int end = start; while(start < chars.length-1) { int charCount = 0; int lastSpace = 0; while(charCount < charLimit) { if(chars[charCount+start] == ' ') { lastSpace = charCount; } charCount++; if(charCount+start == s.length()) { endOfString = true; break; } } end = endOfString ? s.length() : (lastSpace > 0) ? lastSpace+start : charCount+start; System.out.println(s.substring(start, end)); start = end+1; }
Similar Threads
-
How to split a String using split function
By Java Tip in forum java.langReplies: 4Last Post: 04-17-2009, 08:27 PM -
problem with split method
By abhiN in forum New To JavaReplies: 7Last Post: 02-10-2009, 01:54 PM -
how to split a file
By nagaraaju in forum New To JavaReplies: 0Last Post: 03-14-2008, 08:45 AM -
How to split a String using split function
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:32 PM -
Getting all characters in a String
By Alayna in forum New To JavaReplies: 2Last Post: 05-20-2007, 11:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks