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

📄 strings.java

📁 Wicket一个开发Java Web应用程序框架。它使得开发web应用程序变得容易而轻松。 Wicket利用一个POJO data beans组件使得它可以与任何持久层技术相结合。
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.wicket.util.string;import java.security.AccessController;import java.security.PrivilegedAction;import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;import org.apache.wicket.Component;import org.apache.wicket.WicketRuntimeException;/** * A variety of static String utility methods. * <p> * The escapeMarkup() and toMultilineMarkup() methods are useful for turning normal Java Strings * into HTML strings. * <p> * The lastPathComponent(), firstPathComponent(), afterFirstPathComponent() and * beforeLastPathComponent() methods can chop up a String into path components using a separator * character. If the separator cannot be found the original String is returned. * <p> * Similarly, the beforeLast(), beforeFirst(), afterFirst() and afterLast() methods return sections * before and after a separator character. But if the separator cannot be found, an empty string is * returned. * <p> * Some other miscellaneous methods will strip a given ending off a String if it can be found * (stripEnding()), replace all occurrences of one String with another (replaceAll), do type * conversions (toBoolean(), toChar(), toString()), check a String for emptiness (isEmpty()), * convert a Throwable to a String (toString(Throwable)) or capitalize a String (capitalize()). *  * @author Jonathan Locke */public final class Strings{	/**	 * The line separator for the current platform.	 */	public static final String LINE_SEPARATOR;	/** A table of hex digits */	private static final char[] hexDigit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',			'B', 'C', 'D', 'E', 'F' };	private static final Pattern htmlNumber = Pattern.compile("\\&\\#\\d+\\;");	static	{		LINE_SEPARATOR = (String)AccessController.doPrivileged(new PrivilegedAction()		{			public Object run()			{				return System.getProperty("line.separator");			}		});	}	/**	 * Returns everything after the first occurrence of the given character in s.	 * 	 * @param s	 *            The string	 * @param c	 *            The character	 * @return Everything after the first occurrence of the given character in s. If the character	 *         cannot be found, an empty string is returned.	 */	public static String afterFirst(final String s, final char c)	{		if (s == null)		{			return null;		}		final int index = s.indexOf(c);		if (index == -1)		{			return "";		}		return s.substring(index + 1);	}	/**	 * Gets everything after the first path component of a path using a given separator. If the	 * separator cannot be found, an empty String is returned.	 * <p>	 * For example, afterFirstPathComponent("foo.bar.baz", '.') would return "bar.baz" and	 * afterFirstPathComponent("foo", '.') would return "".	 * 	 * @param path	 *            The path to parse	 * @param separator	 *            The path separator character	 * @return Everything after the first component in the path	 */	public static String afterFirstPathComponent(final String path, final char separator)	{		return afterFirst(path, separator);	}	/**	 * Returns everything after the last occurrence of the given character in s.	 * 	 * @param s	 *            The string	 * @param c	 *            The character	 * @return Everything after the last occurrence of the given character in s. If the character	 *         cannot be found, an empty string is returned.	 */	public static String afterLast(final String s, final char c)	{		if (s == null)		{			return null;		}		final int index = s.lastIndexOf(c);		if (index == -1)		{			return "";		}		return s.substring(index + 1);	}	/**	 * Returns everything before the first occurrence of the given character in s.	 * 	 * @param s	 *            The string	 * @param c	 *            The character	 * @return Everything before the first occurrence of the given character in s. If the character	 *         cannot be found, an empty string is returned.	 */	public static String beforeFirst(final String s, final char c)	{		if (s == null)		{			return null;		}		final int index = s.indexOf(c);		if (index == -1)		{			return "";		}		return s.substring(0, index);	}	/**	 * Returns everything before the last occurrence of the given character in s.	 * 	 * @param s	 *            The string	 * @param c	 *            The character	 * @return Everything before the last occurrence of the given character in s. If the character	 *         cannot be found, an empty string is returned.	 */	public static String beforeLast(final String s, final char c)	{		if (s == null)		{			return null;		}		final int index = s.lastIndexOf(c);		if (index == -1)		{			return "";		}		return s.substring(0, index);	}	/**	 * Gets everything before the last path component of a path using a given separator. If the	 * separator cannot be found, the path itself is returned.	 * <p>	 * For example, beforeLastPathComponent("foo.bar.baz", '.') would return "foo.bar" and	 * beforeLastPathComponent("foo", '.') would return "".	 * 	 * @param path	 *            The path to parse	 * @param separator	 *            The path separator character	 * @return Everything before the last component in the path	 */	public static String beforeLastPathComponent(final String path, final char separator)	{		return beforeLast(path, separator);	}	/**	 * Capitalizes a string.	 * 	 * @param s	 *            The string	 * @return The capitalized string	 */	public static String capitalize(final String s)	{		if (s == null)		{			return null;		}		final char[] chars = s.toCharArray();		if (chars.length > 0)		{			chars[0] = Character.toUpperCase(chars[0]);		}		return new String(chars);	}	/**	 * Converts a Java String to an HTML markup string, but does not convert normal spaces to	 * non-breaking space entities (&lt;nbsp&gt;).	 * 	 * @param s	 *            The string to escape	 * @see Strings#escapeMarkup(String, boolean)	 * @return The escaped string	 */	public static CharSequence escapeMarkup(final String s)	{		return escapeMarkup(s, false);	}	/**	 * Converts a Java String to an HTML markup String by replacing illegal characters with HTML	 * entities where appropriate. Spaces are converted to non-breaking spaces (&lt;nbsp&gt;) if	 * escapeSpaces is true, tabs are converted to four non-breaking spaces, less than signs are	 * converted to &amp;lt; entities and greater than signs to &amp;gt; entities.	 * 	 * @param s	 *            The string to escape	 * @param escapeSpaces	 *            True to replace ' ' with nonbreaking space	 * @return The escaped string	 */	public static CharSequence escapeMarkup(final String s, final boolean escapeSpaces)	{		return escapeMarkup(s, escapeSpaces, false);	}	/**	 * Converts a Java String to an HTML markup String by replacing illegal characters with HTML	 * entities where appropriate. Spaces are converted to non-breaking spaces (&lt;nbsp&gt;) if	 * escapeSpaces is true, tabs are converted to four non-breaking spaces, less than signs are	 * converted to &amp;lt; entities and greater than signs to &amp;gt; entities.	 * 	 * @param s	 *            The string to escape	 * @param escapeSpaces	 *            True to replace ' ' with nonbreaking space	 * @param convertToHtmlUnicodeEscapes	 *            True to convert non-7 bit characters to unicode HTML (&#...)	 * @return The escaped string	 */	public static CharSequence escapeMarkup(final String s, final boolean escapeSpaces,			final boolean convertToHtmlUnicodeEscapes)	{		if (s == null)		{			return null;		}		else		{			int len = s.length();			final AppendingStringBuffer buffer = new AppendingStringBuffer((int)(len * 1.1));			for (int i = 0; i < len; i++)			{				final char c = s.charAt(i);				switch (c)				{					case '\t' :						if (escapeSpaces)						{							// Assumption is four space tabs (sorry, but that's							// just how it is!)							buffer.append("&nbsp;&nbsp;&nbsp;&nbsp;");						}						else						{							buffer.append(c);						}						break;					case ' ' :						if (escapeSpaces)						{							buffer.append("&nbsp;");						}						else						{							buffer.append(c);						}						break;					case '<' :						buffer.append("&lt;");						break;					case '>' :						buffer.append("&gt;");						break;					case '&' :						// if this is an entity (&#), then do not convert						if ((i < len - 1) && (s.charAt(i + 1) == '#'))						{							buffer.append(c);						}						else						{							// it is not an entity, so convert it to &amp;							buffer.append("&amp;");						}						break;					case '"' :						buffer.append("&quot;");						break;					case '\'' :						buffer.append("&#039;");						break;					default :						if (convertToHtmlUnicodeEscapes)						{							int ci = 0xffff & c;							if (ci < 160)							{								// nothing special only 7 Bit								buffer.append(c);							}							else							{								// Not 7 Bit use the unicode system								buffer.append("&#");								buffer.append(new Integer(ci).toString());								buffer.append(';');							}						}						else						{							buffer.append(c);						}						break;				}			}			return buffer;		}	}	/**	 * Gets the first path component of a path using a given separator. If the separator cannot be	 * found, the path itself is returned.	 * <p>	 * For example, firstPathComponent("foo.bar", '.') would return "foo" and	 * firstPathComponent("foo", '.') would return "foo".	 * 	 * @param path	 *            The path to parse	 * @param separator	 *            The path separator character	 * @return The first component in the path or path itself if no separator characters exist.	 */	public static String firstPathComponent(final String path, final char separator)	{		if (path == null)		{			return null;		}		final int index = path.indexOf(separator);		if (index == -1)		{			return path;		}		return path.substring(0, index);	}	/**	 * Converts encoded &#92;uxxxx to unicode chars and changes special saved chars to their	 * original forms.	 * 	 * @param escapedUnicodeString	 *            escaped unicode string, like '\u4F60\u597D'.	 * 	 * @return The actual unicode. Can be used for instance with message bundles	 */	public static String fromEscapedUnicode(String escapedUnicodeString)	{		int off = 0;		char[] in = escapedUnicodeString.toCharArray();		int len = in.length;		char[] convtBuf = new char[len];		if (convtBuf.length < len)		{			int newLen = len * 2;			if (newLen < 0)			{				newLen = Integer.MAX_VALUE;			}			convtBuf = new char[newLen];		}		char aChar;		char[] out = convtBuf;		int outLen = 0;		int end = off + len;		while (off < end)		{			aChar = in[off++];			if (aChar == '\\')			{				aChar = in[off++];				if (aChar == 'u')				{

⌨️ 快捷键说明

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