Results 1 to 4 of 4
  1. #1
    JackJ is offline Member
    Join Date
    Dec 2007
    Posts
    2
    Rep Power
    0

    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

  2. #2
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    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));

  3. #3
    JackJ is offline Member
    Join Date
    Dec 2007
    Posts
    2
    Rep Power
    0

    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.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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

  1. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 09:27 PM
  2. problem with split method
    By abhiN in forum New To Java
    Replies: 7
    Last Post: 02-10-2009, 02:54 PM
  3. how to split a file
    By nagaraaju in forum New To Java
    Replies: 0
    Last Post: 03-14-2008, 09:45 AM
  4. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 10:32 PM
  5. Getting all characters in a String
    By Alayna in forum New To Java
    Replies: 2
    Last Post: 05-20-2007, 12:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •