package com.its.util; import java.util.*; import java.text.SimpleDateFormat; import java.text.ParsePosition; import java.text.DecimalFormat; /** This is a brief extract from the source of the Stringer class * The class Stringer includes text manipulation utilities * @author Jeff.Zhuk@JavaSchool.com - http://javaschool.com - ITS, Inc. * Copyrights: THIS SOURCE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. Jeff Zhuk and ITS, Inc. grants to you nonexclusive, nontransferable rights to use and modify the source with the condition to keep the copyright notice above. */ public class Stringer { public static boolean debug = false; public static String lineSeparator = System.getProperty("line.separator"); public static String argSeparator = "|"; public static String graphExtensions = "jpg,jpeg,png,bmp,gif,tif"; /** * The rindexOf() method is similar to the PERL rindex method that looks for the last occurance of the pattern * @param source * @param pattern * @return index of the last occurrence or -1 */ public static int rindexOf(String source, String pattern) { if(source == null || pattern == null || source.length() < pattern.length()) return -1; int i = source.indexOf(pattern); if(i < 0) return i; for(int j=i; j >= 0; i=j) { j = source.indexOf(pattern, i+1); if(j<0) return i; } return i; } /** * The getPath() method returns a path without a filename * @param filename with a full path * @return path without a filename */ public static String getPath(String filename) { if(filename == null || filename.indexOf("/") < 0) return null; int slashIndex = rindexOf(filename, "/"); String path = filename.substring(0, slashIndex); return path; } /** * getExtension() method returns file extension or "" * @param iName file name * @return file extension */ public static String getExtension(String iName) { int extIndex = iName.lastIndexOf("."); if(extIndex > 0) return iName.toLowerCase().substring(extIndex); return ""; } /** * getFullName() method cuts extension and returns a file name with the path * @param iName file name * @return file name without extension */ public static String getFullName(String iName) { int extIndex = iName.lastIndexOf("."); if(extIndex > 0) return iName.substring(0, extIndex); else return iName; } /** * The indexOfPattern() method allows to pass several patterns separated by the "|" character * @param source * @param patterns as a single string with "|" separated patterns, for example, "\r|\n| |\'|\t" * @return indexOfPattern first found */ public static int indexOfPattern(String source, String patterns) { String[] pats = Stringer.split("|",patterns); return indexOfPattern(source, pats); } /** * The indexOfPattern() method allows to pass array of patterns and return the index of the first found pattern * @param source * @param patterns as a single string with "|" separated patterns, for example, "\r|\n| |\'|\t" * @return indexOfPattern first found */ public static int indexOfPattern(String source, String[] patterns) { if(source == null || patterns == null || patterns.length == 0) { return -1; } for(int k=0; k < patterns.length; k++) { String pattern = patterns[k].replaceAll("%20"," "); if(source.indexOf(pattern) >= 0) { return k; } } return -1; } /** * The split() method works like in PERL. * Useful for cases with special characters and in sub-sets of Java like J2ME or browser jvm) * Example: * String source="mama papa and you"; * String[] output = Stringer.split(" ",source); * // output={"mama","papa","and","you"}; * @param iSeparator * @param iSource * @return result strings produced from a source * **/ public static String[] split(String iSeparator, String iSource) { if(iSeparator == null || iSource == null) return null; Vector dest = new Vector(); String substr=null; for(int k=0, i=0; k < iSource.length(); k = i + iSeparator.length() ) { i = iSource.indexOf(iSeparator,k); if(i == k) { dest.addElement(""); // null string } else if(i > k) { substr=iSource.substring(k,i); if(substr.equals(iSeparator) == false) { dest.addElement(new String(substr)); } } else if(k < iSource.length() ) { // no more separators dest.addElement(new String(iSource.substring(k,iSource.length()))); i = iSource.length(); // end of for loop } } String[] result = toArray(dest); return result; } public static void main(String[] args) throws Exception { int indexOfFirstPattern("Hello! \r\n How are you? \n\r", "?,!,\n"); System.out.println("indexOfFirstPattern="+indexOfFirstPattern); } } // end of the extract from the Stringer class