Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-15-2007, 02:13 AM
Member
 
Join Date: Dec 2007
Posts: 2
JackJ is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-15-2007, 03:38 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
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));
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-15-2007, 07:40 AM
Member
 
Join Date: Dec 2007
Posts: 2
JackJ is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-17-2007, 03:35 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
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; }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to split a String using split function Java Tip java.lang 0 04-04-2008 03:39 PM
how to split a file nagaraaju New To Java 0 03-14-2008 09:45 AM
problem with split method abhiN New To Java 3 01-17-2008 08:31 AM
How to split a String using split function JavaBean Java Tips 0 10-04-2007 10:32 PM
Getting all characters in a String Alayna New To Java 2 05-20-2007 12:49 PM


All times are GMT +3. The time now is 02:43 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org