📄 stringutils.java
字号:
/* * Core - Library of useful classes that are used in many CyberDemia projects. * Copyright (C) 2004 CyberDemia Research and Services * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. * * See the COPYING file located in the top-level-directory of * the archive of this library for complete text of license. */package com.cyberdemia.util;/** * StringUtils is a utility class for manipulating Strings. * * @author Alexander Yap * @version $Revision: 1.2 $ at $Date: 2004/06/12 12:51:56 $ by $Author: alexycyap $ */public abstract class StringUtils{ private StringUtils() { // Not used } /** * Truncates the src String to a maximum of length characters. * If src's length is shorter than or equals to length, it is returned unchanged. * If src is longer than length, is it truncated to (length-3) characters and * then the suffix "..." is appended to the end to form the resultant String. * * @param src Original String * @param length Length to truncate to, must be at least 3. * @return Truncated String. * @throws IllegalArgumentException if length is less than 3. */ public static String truncate(String src, int length) { if (length<3) { throw new IllegalArgumentException("Argument length is too short, must be 3 or more."); } if (src.length()<=length) { return src; } return src.substring(0,length-3) + "..."; } /** * Escapes HTML sensitive characters within an input String and returns * a String suitable to be used in a HTML page. * @param inStr Input String * @param escapeSpace true to escape space characters as non-breaking-spaces (preventing word wrapping), false to keep them as spaces. * @return String suitable to be used in a HTML page. */ public static String escapeHTML(String inStr, boolean escapeSpace) { char[] inChars = inStr.toCharArray(); StringBuffer buf = new StringBuffer(inChars.length); for (int i=0; i<inChars.length; i++) { char c = inChars[i]; if ( (c==' ') || Character.isLetterOrDigit(c) ) { if ((c==' ') && escapeSpace) { buf.append(" "); } else { buf.append(c); } } else { switch (c) { case '<': buf.append("<"); break; case '>': buf.append(">"); break; case '\n': buf.append("<br />"); break; case '"': buf.append("""); break; case '&': buf.append("&"); break; default: // Unicode buf.append("&#"); buf.append(String.valueOf((int)c)); buf.append(';'); } } } return buf.toString(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -