Java String Format formatStringTo80Columns(String str)

Here you can find the source of formatStringTo80Columns(String str)

Description

Takes a string and formats it so that words are not cut off at the 80 column mark.

License

Open Source License

Parameter

Parameter Description
str The string to format.

Return

A formatted string.

Declaration

public static String formatStringTo80Columns(String str) 

Method Source Code

//package com.java2s;
/****************************************************************************
 * Ether Code base, version 1.0                                             *
 *==========================================================================*
 * Copyright (C) 2011 by Ron Kinney                                         *
 * All rights reserved.                                                     *
 *                                                                          *
 * This program is free software; you can redistribute it and/or modify     *
 * it under the terms of the GNU General Public License as published        *
 * by the Free Software Foundation; either version 2 of the License, or     *
 * (at your option) any later version.                                      *
 *                                                                          *
 * This program is distributed in the hope that it will be useful,          *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 * GNU General Public License for more details.                             *
 *                                                                          *
 * Redistribution and use in source and binary forms, with or without       *
 * modification, are permitted provided that the following conditions are   *
 * met:                                                                     *
 *                                                                          *
 * * Redistributions of source code must retain this copyright notice,      *
 *   this list of conditions and the following disclaimer.                  *
 * * Redistributions in binary form must reproduce this copyright notice    *
 *   this list of conditions and the following disclaimer in the            *
 *   documentation and/or other materials provided with the distribution.   *
 *                                                                          *
 *==========================================================================*
 * Ron Kinney (ronkinney@gmail.com)                                         *
 * Ether Homepage:   http://tdod.org/ether                                  *
 ****************************************************************************/

import java.util.ArrayList;

public class Main {
    /**/*from  w w w . j  a  v  a2 s  . com*/
     * Takes a string and formats it so that words are not cut off at the 80 column mark.
     * If a word cuts into the 80th column, then it is wrapped to the next line instead.
     * @param str The string to format.
     * @return A formatted string.
     */
    public static String formatStringTo80Columns(String str) {
        ArrayList<String> list = new ArrayList<String>();
        String[] split = str.split(" ");
        StringBuffer buffer = new StringBuffer();
        for (int count = 0; count < split.length; count++) {
            String word = split[count];
            if ((buffer.length() + word.length() + 1) > 80) {
                list.add(buffer.toString());
                buffer = new StringBuffer(word);
            } else {
                if (buffer.length() == 0) {
                    buffer.append(word);
                } else {
                    buffer.append(" " + word);
                }
            }
        }
        list.add(buffer.toString());

        StringBuffer finalBuffer = new StringBuffer();
        for (String str1 : list) {
            finalBuffer.append(str1 + "\n");
        }

        return finalBuffer.toString();
    }
}

Related

  1. formatMessage(String message)
  2. formatNameToMimeType(final String formatName)
  3. formatParam(String param)
  4. formatProtein(String seq)
  5. formatString(String stringFormat, String... args)
  6. formatTooltip(String tooltip)
  7. formatTreeLines(String tree)
  8. formatVarname(StringBuilder buffer, Object varname)
  9. formatWords(String words, String delimiter, boolean isHeaderUpperCase)