?? stringutils.java
字號:
package edu.stanford.nlp.util;import java.util.*;import java.util.regex.*;import java.io.*;import java.net.*;/** * Stringtils is a class for random String things. * * @author Dan Klein * @author Christopher Manning * @version 2003/02/03 */public class StringUtils { /** * Don't let anyone instantiate this class. */ private StringUtils() {} /** Say whether this regular expression can be found inside * this String. This method provides one of the two "missing" * convenience methods for regular expressions in the String class * in JDK1.4. This is the one you'll want to use all the time if * you're used to Perl. What were they smoking? * @param str String to search for match in * @param regex String to compile as the regular expression * @return Whether the regex can be found in str */ public static boolean find(String str, String regex) { return Pattern.compile(regex).matcher(str).find(); } /** Say whether this regular expression can be found at the beginning of * this String. This method provides one of the two "missing" * convenience methods for regular expressions in the String class * in JDK1.4. * @param str String to search for match at start of * @param regex String to compile as the regular expression * @return Whether the regex can be found at the start of str */ public static boolean lookingAt(String str, String regex) { return Pattern.compile(regex).matcher(str).lookingAt(); } /** Say whether this regular expression matches * this String. This method is the same as the String.matches() method, * and is included just to give a call that is parallel to the other * static regex methods in this class. * @param str String to search for match at start of * @param regex String to compile as the regular expression * @return Whether the regex matches the whole of this str */ public static boolean matches(String str, String regex) { return Pattern.compile(regex).matcher(str).matches(); } public static String slurpFile(File file) throws IOException { String lineSeparator = System.getProperty("line.separator"); BufferedReader br = new BufferedReader(new FileReader(file)); String temp; StringBuffer buff = new StringBuffer(16000); // make biggish while ((temp = br.readLine()) != null) { buff.append(temp); buff.append(lineSeparator); } br.close(); return buff.toString(); } public static String slurpURL(URL u) throws IOException { String lineSeparator = System.getProperty("line.separator"); URLConnection uc = u.openConnection(); InputStream is = uc.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String temp; StringBuffer buff = new StringBuffer(16000); // make biggish while ((temp = br.readLine()) != null) { buff.append(temp); buff.append(lineSeparator); } br.close(); return buff.toString(); } public static String join(List l, String glue) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < l.size(); i++) { if (i > 0) sb.append(glue); sb.append(l.get(i).toString()); } return sb.toString(); } public static String join(Object[] elements,String glue) { return(join(Arrays.asList(elements),glue)); } public static String join(List l) { return join(l, " "); } public static String join(Object[] elements) { return(join(elements," ")); } public static List split(String s) { return(Arrays.asList(s.split("\\s+"))); } /** Return a String of length a minimum of totalChars characters by * padding the input String str with spaces. If str is already longer * than totalChars, it is returned unchanged. */ public static String pad(String str, int totalChars) { StringBuffer sb = new StringBuffer(str); for (int i = 0; i < totalChars-str.length(); i++) { sb.append(" "); } return sb.toString(); } public static String pad(Object obj, int totalChars) { return pad(obj.toString(), totalChars); } public static String leftPad(String str, int totalChars) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < totalChars-str.length(); i++) { sb.append(" "); } sb.append(str); return sb.toString(); } public static String leftPad(Object obj, int totalChars) { return leftPad(obj.toString(), totalChars); } public static String leftPad(int i, int totalChars) { return leftPad(new Integer(i), totalChars); } public static String leftPad(double d, int totalChars) { return leftPad(new Double(d), totalChars); } /** Returns s if it's at most maxWidth chars, otherwise chops right side to fit. */ public static String trim(String s, int maxWidth) { if(s.length()<=maxWidth) return(s); return(s.substring(0,maxWidth)); } public static String trim(Object obj, int maxWidth) { return trim(obj.toString(), maxWidth); } public static String fileNameClean(String s) { char[] chars = s.toCharArray(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < chars.length; i++) { char c = chars[i]; if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c == '_')) sb.append(c); else { if (c == ' ' || c == '-') sb.append('_'); else sb.append("x"+(int)c+"x"); } } return sb.toString(); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -