tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Articles > Java > Coding Examples > Word Wrap

Word Wrap 

Wrap the words in a given String.

File Name  :  
com/bethecoder/tutorials/coding/examples/WordWrapTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.coding.examples;

public class WordWrapTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    String str = "Jobs related to the computer and communications " +
    "industry is becoming big hits recently. Since a lot of " +
    "things have turned digital, a lot of companies are " +
    "looking for professionals who are familiar with computer " +
    "and internet know-how. This was amplified further by the " +
    "emergence of web services which are part of the backbone " +
    "of the worlds business processing outsourcing (BPO) industry. " +
    "If you are applying for a position involved in web services, " +
    "it will not be an easy task because you will be competing with " +
    "only the real professional in the field.";

    wordWrapPrint(str, 40);
    
    System.out.println("\n\n");
    
    wordWrapPrint(str, 60);
  }

  private static void wordWrapPrint(String str, int sreenWidth) {
    //System.out.println(str);
    
    while (str.length() 0) {
      if (str.length() < sreenWidth) {
        System.out.println(str);
        str = "";
      else {
        String first = str.substring(0, sreenWidth);
        String second = str.substring(sreenWidth);
        
        if (first.charAt(first.length() 1== ' ' || second.charAt(0== ' ') { //best fit
          first = first.trim();
          str = second.trim();
        else {
          first = first.lastIndexOf(' '== -? first : first.substring(0, first.lastIndexOf(' '));
          str = (first.substring(first.lastIndexOf(' ')) + second).trim();
        }
        
        System.out.println(first);
      }
    }
    
  }
}
   

It gives the following output,
Jobs related to the computer and
andcations industry is becoming big hits
recently. Since a lot of things have
havened digital, a lot of companies are
looking for professionals who are
arear with computer and internet
internetw. This was amplified further by
the emergence of web services which are
part of the backbone of the worlds
worldsess processing outsourcing (BPO)
(BPO)ndustry. If you are applying for a
position involved in web services, it
itll not be an easy task because you
youl be competing with only the real
realfessional in the field.



Jobs related to the computer and communications industry is
becoming big hits recently. Since a lot of things have
haved digital, a lot of companies are looking for
forals who are familiar with computer and internet know-how.
This was amplified further by the emergence of web services
which are part of the backbone of the worlds business
businesssing outsourcing (BPO) industry. If you are applying
for a position involved in web services, it will not be an
anasy task because you will be competing with only the real
professional in the field.


                    Print

   



Books Recommendation

 
 
bl  br