⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textutil.java

📁 j2me is based on j2mepolish, client & server for mobile application. menu sample
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//#condition polish.midp || polish.usePolishGui || polish.usePolishGui

/*
 * Created on 20-Apr-2004 at 01:30:49.
 *
 * Copyright (c) 2004-2005 Robert Virkus / Enough Software
 *
 * This file is part of J2ME Polish.
 *
 * J2ME Polish 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.
 * 
 * J2ME Polish 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.
 * 
 * You should have received a copy of the GNU General Public License
 * along with J2ME Polish; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Commercial licenses are also available, please
 * refer to the accompanying LICENSE.txt or visit
 * http://www.j2mepolish.org for details.
 */
package de.enough.polish.util;

import javax.microedition.lcdui.Font;


/**
 * <p>Provides some usefull String methods.</p>
 *
 * <p>Copyright Enough Software 2004, 2005</p>

 * <pre>
 * history
 *        20-Apr-2004 - rob creation
 * </pre>
 * @author Robert Virkus, robert@enough.de
 */
public final class TextUtil {

	private static final String UNRESERVED = "-_.!~*'()\"";
	
	/**
	 * Splits the given String around the matches defined by the given delimiter into an array.
	 * Example:
	 * <code>TextUtil.split("one;two;three", ';', 3)</code> results into the array
	 * <code>{"one", "two", "three"}</code>.<br />
	 *
	 * @param value the String which should be split into an array
	 * @param delimiter the delimiter which marks the boundaries of the array 
	 * @return an array, when the delimiter was not found, the array will only have a single element.
	 */
	public static String[] split(String value, char delimiter) {
		char[] valueChars = value.toCharArray();
		int lastIndex = 0;
		ArrayList strings = null;
		for (int i = 0; i < valueChars.length; i++) {
			char c = valueChars[i];
			if (c == delimiter) {
				if (strings == null) {
					strings = new ArrayList();
				}
				strings.add( new String( valueChars, lastIndex, i - lastIndex ) );
				lastIndex = i + 1;
			}
		}
		if (strings == null) {
			return new String[]{ value };
		}
		// add tail:
		strings.add( new String( valueChars, lastIndex, valueChars.length - lastIndex ) );
		return (String[]) strings.toArray( new String[ strings.size() ] );
	}
	
	/**
	 * Splits the given String around the matches defined by the given delimiter into an array.
	 * Example:
	 * <code>TextUtil.split("one;two;three", ';', 3)</code> results into the array
	 * <code>{"one", "two", "three"}</code>.<br />
	 * <code>TextUtil.split("one;two;three", ';', 4)</code> results into the array
	 * <code>{"one", "two", "three", null}</code>.<br />
	 * <code>TextUtil.split("one;two;three", ';', 2)</code> results into the array
	 * <code>{"one", "two"}</code>.<br />
	 * This method is less resource intensive compared to the other split method, since
	 * no temporary list needs to be created
	 *
	 * @param value the String which should be split into an array
	 * @param delimiter the delimiter which marks the boundaries of the array 
	 * @param numberOfChunks the number of expected matches
	 * @return an array with the length of numberOfChunks, when not enough elements are found, the array will contain null elements
	 */
	public static String[] split(String value, char delimiter, int numberOfChunks) {
		char[] valueChars = value.toCharArray();
		int lastIndex = 0;
		String[] chunks = new String[ numberOfChunks ];
		int chunkIndex = 0;
		for (int i = 0; i < valueChars.length; i++) {
			char c = valueChars[i];
			if (c == delimiter) {
				chunks[ chunkIndex ] = value.substring( lastIndex, i - lastIndex );
				lastIndex = i + 1;
				chunkIndex++;
				if (chunkIndex == numberOfChunks ) {
					break;
				}
			}
		}
		if (chunkIndex < numberOfChunks) {
			// add tail:
			chunks[chunkIndex] = value.substring( lastIndex, valueChars.length - lastIndex );
		}
		return chunks;
	}

	/**
	 * Wraps the given string so it fits on the specified lines.
	 * First of al it is splitted at the line-breaks ('\n'), subsequently the substrings
	 * are splitted when they do not fit on a single line.
	 *  
	 * @param value the string which should be splitted
	 * @param font the font which is used to display the font
	 * @param firstLineWidth the allowed width for the first line
	 * @param lineWidth the allowed width for all other lines, lineWidth >= firstLineWidth
	 * @return the array containing the substrings
	 * @deprecated please use wrap instead
	 * @see #wrap(String, Font, int, int)
	 */
	public static String[] split( String value, Font font, int firstLineWidth, int lineWidth ) {
		return wrap(value, font, firstLineWidth, lineWidth);
	}

	/**
	 * Wraps the given string so it fits on the specified lines.
	 * First of al it is splitted at the line-breaks ('\n'), subsequently the substrings
	 * are splitted when they do not fit on a single line.
	 *  
	 * @param value the string which should be splitted
	 * @param font the font which is used to display the font
	 * @param firstLineWidth the allowed width for the first line
	 * @param lineWidth the allowed width for all other lines, lineWidth >= firstLineWidth
	 * @return the array containing the substrings
	 */
	public static String[] wrap( String value, Font font, int firstLineWidth, int lineWidth ) {
		if (firstLineWidth <= 0 || lineWidth <= 0) {
			//#debug error
			//# System.out.println("INVALID LINE WIDTH FOR SPLITTING " + firstLineWidth + " / " + lineWidth + " ( for string " + value + ")");
			return new String[]{ value };
		}
		boolean hasLineBreaks = (value.indexOf('\n') != -1);
		int completeWidth = font.stringWidth(value);
		if ( (completeWidth <= firstLineWidth && !hasLineBreaks) ) { // || (value.length() <= 1) ) {
			// the given string fits on the first line:
			//if (hasLineBreaks) {
			//	return split( "complete/linebreaks:" + completeWidth + "> " + value, '\n');
			//} else {
				return new String[]{  value };
			//}
		}
		// the given string does not fit on the first line:
		ArrayList lines = new ArrayList();
		if (!hasLineBreaks) {
			wrap( value, font, completeWidth, firstLineWidth, lineWidth, lines );
		} else {
			// now the string will be splitted at the line-breaks and
			// then each line is processed:
			char[] valueChars = value.toCharArray();
			int lastIndex = 0;
			char c =' ';
			for (int i = 0; i < valueChars.length; i++) {
				c = valueChars[i];
				if (c == '\n' || i == valueChars.length -1 ) {
					String line = null;
					if (i == valueChars.length -1) {
						line = new String( valueChars, lastIndex, (i + 1) - lastIndex );
						//System.out.println("wrap: adding last line " + line );
					} else {
						line = new String( valueChars, lastIndex, i - lastIndex );
						//System.out.println("wrap: adding " + line );
					}
					completeWidth = font.stringWidth(line);
					if (completeWidth <= firstLineWidth ) {
						lines.add( line );						
					} else {
						wrap(line, font, completeWidth, firstLineWidth, lineWidth, lines);
					}
					lastIndex = i + 1;
					// after the first line all line widths are the same:
					firstLineWidth = lineWidth;
				} // for each line
			} // for all chars
			// special case for lines that end with \n: add a further line
			if (c == '\n') {
				lines.add("");
			}
		}
		//#debug
		//# System.out.println("Wrapped [" + value + "] into " + lines.size() + " rows.");
		return (String[]) lines.toArray( new String[ lines.size() ]);
	}
	
	/**
	 * Wraps the given string so that the substrings fit into the the given line-widths.
	 * It is expected that the specified lineWidth >= firstLineWidth.
	 * The resulting substrings will be added to the given ArrayList.
	 * When the complete string fits into the first line, it will be added
	 * to the list.
	 * When the string needs to be splited to fit on the lines, it is tried to
	 * split the string at a gab between words. When this is not possible, the
	 * given string will be splitted in the middle of the corresponding word. 
	 * 
	 * 
	 * @param value the string which should be splitted
	 * @param font the font which is used to display the font
	 * @param completeWidth the complete width of the given string for the specified font.
	 * @param firstLineWidth the allowed width for the first line
	 * @param lineWidth the allowed width for all other lines, lineWidth >= firstLineWidth
	 * @param list the list to which the substrings will be added.
	 * @deprecated please use wrap instead
	 * @see #wrap(String, Font, int, int, int, ArrayList)
	 */
	public static void split( String value, Font font, 
			int completeWidth, int firstLineWidth, int lineWidth, 
			ArrayList list ) 
	{
		wrap(value, font, completeWidth, firstLineWidth, lineWidth, list);
	}
	
	/**
	 * Wraps the given string so that the substrings fit into the the given line-widths.
	 * It is expected that the specified lineWidth >= firstLineWidth.
	 * The resulting substrings will be added to the given ArrayList.
	 * When the complete string fits into the first line, it will be added
	 * to the list.
	 * When the string needs to be splited to fit on the lines, it is tried to
	 * split the string at a gab between words. When this is not possible, the
	 * given string will be splitted in the middle of the corresponding word. 
	 * 
	 * 
	 * @param value the string which should be splitted
	 * @param font the font which is used to display the font
	 * @param completeWidth the complete width of the given string for the specified font.
	 * @param firstLineWidth the allowed width for the first line
	 * @param lineWidth the allowed width for all other lines, lineWidth >= firstLineWidth
	 * @param list the list to which the substrings will be added.
	 */
	public static void wrap( String value, Font font, 
			int completeWidth, int firstLineWidth, int lineWidth, 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -