Here you can find the source of formatStringTo80Columns(String str)
Parameter | Description |
---|---|
str | The string to format. |
public static String formatStringTo80Columns(String str)
//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(); } }