Java examples for java.lang:String HTML
Returns the text specified, but line-wrapped and with <html> added to the beginning.
//package com.java2s; public class Main { public static void main(String[] argv) throws Exception { String text = "java2s.com"; System.out.println(htmlTipWrap(text)); }//from w w w. j av a 2 s. c o m /** * Returns the text specified, but line-wrapped and with <html> added * to the beginning. This is intended to be used with tool tips. * * @param text * The text to wrap * @return The wrapped text, with <html> at the beginning */ public static String htmlTipWrap(String text) { return "<html>" + lineWrap(text, "<br/>", 80); } /** * Returns a string which represents the initial string, but with the wrap * string (which would usually be a newline or, for html content, something * like "<br/>") inserted such that none of the text lines in the * resulting string is longer than <code>width</code>, unless the line is * made up of only one word and the word itself is longer than * <code>width</code>. Lines are only wrapped on space characters, and * the space character is retained (with the wrap string inserted directly * after the space). * * @param input * @param width * @return */ public static String lineWrap(String input, String wrapString, int width) { if (input.length() <= width) /* * The input is shorter than the specified width, so it should take * up only one line */ return input; if (input.indexOf(" ") == -1) /* * The input is longer than one line but contains no spaces */ return input; /* * The input is longer than the specified length and contains a space. * Now we need to find the last space within the width specified, and * wrap around it. If there isn't a space within the width specified, we * find the first space after the width specified and wrap on that one * instead. */ String firstWidth = input.substring(0, width); String remainder = input.substring(width); int lastSpaceIndex = firstWidth.lastIndexOf(" "); int firstSpaceIndex = remainder.indexOf(" "); assert firstSpaceIndex != -1 || lastSpaceIndex != -1; int index = (lastSpaceIndex == -1) ? (firstSpaceIndex + width) : lastSpaceIndex; /* * Now we need to position the pointer directly after the space */ index = index + 1; input = input.substring(0, index) + wrapString + lineWrap(input.substring(index), wrapString, width); return input; } }