Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-15-2007, 02:13 AM
Member
 
Join Date: Dec 2007
Posts: 2
Rep Power: 0
JackJ is on a distinguished road
Default 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
  #2 (permalink)  
Old 12-15-2007, 03:38 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
Rep Power: 0
staykovmarin is on a distinguished road
Default
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
Rep Power: 0
JackJ is on a distinguished road
Default
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
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
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
Reply

Bookmarks

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with split method abhiN New To Java 7 02-10-2009 02:54 PM
How to split a String using split function Java Tip java.lang 1 08-26-2008 08:51 AM
how to split a file nagaraaju New To Java 0 03-14-2008 09:45 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 +2. The time now is 08:29 PM.



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