📄 parserutils.java
字号:
StringBuffer output = new StringBuffer(); boolean charFound=false; for (int index=0; index<input.length(); index++) { charFound=false; for (int charsCount=0; charsCount<charsToBeRemoved.length(); charsCount++) if (charsToBeRemoved.charAt(charsCount)==input.charAt(index)) charFound=true; if (!((Character.isWhitespace(input.charAt(index))) || (Character.isSpaceChar(input.charAt(index))) || (charFound))) output.append(input.charAt(index)); } return output.toString(); } /** * Remove from the beginning and the end of the input string all the spaces and tabs like chars. * Remove also the chars specified in the input variable charsToBeRemoved. * <BR>The removal process removes only chars at the beginning and at the end of the string. * <BR>For example if you call trimSpacesBeginEnd("<DIV> +12.5 </DIV>", "<>DIV/"), * <BR>you obtain a string "+12.5" as output (space chars and <,>,D,I,V,/ are chars that must be removed). * <BR>For example if you call trimSpacesBeginEnd("<DIV> Trim all spaces but not the ones inside the string </DIV>", "<>DIV/"), * <BR>you obtain a string "Trim all spaces but not the ones inside the string" as output (all the spaces inside the string are preserved). * @param input The string in input. * @param charsToBeRemoved The chars to be removed. * @return The string as output. */ public static String trimSpacesBeginEnd (String input, String charsToBeRemoved) { String output = new String(); int begin=0; int end=input.length()-1; boolean charFound=false; boolean ok=true; for (int index=begin; (index<input.length()) && ok; index++) { charFound=false; for (int charsCount=0; charsCount<charsToBeRemoved.length(); charsCount++) if (charsToBeRemoved.charAt(charsCount)==input.charAt(index)) charFound=true; if (!( (Character.isWhitespace(input.charAt(index))) || (Character.isSpaceChar(input.charAt(index))) || (charFound) )) { begin=index; ok=false; } } ok=true; for (int index=end; (index>=0) && ok; index--) { charFound=false; for (int charsCount=0; charsCount<charsToBeRemoved.length(); charsCount++) if (charsToBeRemoved.charAt(charsCount)==input.charAt(index)) charFound=true; if (!( (Character.isWhitespace(input.charAt(index))) || (Character.isSpaceChar(input.charAt(index))) || (charFound) )) { end=index; ok=false; } } output=input.substring(begin,end+1); return output; } /** * Split the input string considering as string separator * all the characters * with the only exception of the characters specified in charsDoNotBeRemoved param. * <BR>For example if you call splitButChars("<DIV> +12.5, +3.4 </DIV>", "+.1234567890"), * <BR>you obtain an array of strings {"+12.5", "+3.4"} as output (+,.,1,2,3,4,5,6,7,8,9,0 are chars that do not be removed). * @param input The string in input. * @param charsDoNotBeRemoved The chars that do not be removed. * @return The array of strings as output. */ public static String[] splitButChars (String input, String charsDoNotBeRemoved) { ArrayList output = new ArrayList(); int minCapacity = 0; StringBuffer str = new StringBuffer(); boolean charFound = false; boolean toBeAdd = false; for (int index=0; index<input.length(); index++) { charFound=false; for (int charsCount=0; charsCount<charsDoNotBeRemoved.length(); charsCount++) if (charsDoNotBeRemoved.charAt(charsCount)==input.charAt(index)) charFound=true; if (charFound) { str.append(input.charAt(index)); toBeAdd=false; } else if (!toBeAdd) toBeAdd=true; // finished to parse one string if (toBeAdd && (str.length()!=0)) { minCapacity++; output.ensureCapacity(minCapacity); if (output.add(str.toString())) str = new StringBuffer(); else minCapacity--; } } // add the last string if (str.length()!=0) { minCapacity++; output.ensureCapacity(minCapacity); if (output.add(str.toString())) str = new StringBuffer(); else minCapacity--; } output.trimToSize(); Object[] outputObj = output.toArray(); String[] outputStr = new String[output.size()]; for (int i=0; i<output.size(); i++) outputStr[i] = new String((String) outputObj[i]); return outputStr; } /** * Remove from the input string all the characters * with the only exception of the characters specified in charsDoNotBeRemoved param. * <BR>For example if you call trimButChars("<DIV> +12.5 </DIV>", "+.1234567890"), * <BR>you obtain a string "+12.5" as output (+,.,1,2,3,4,5,6,7,8,9,0 are chars that do not be removed). * <BR>For example if you call trimButChars("<DIV> +1 2 . 5 </DIV>", "+.1234567890"), * <BR>you obtain a string "+12.5" as output (the spaces between 1 and 2, 2 and ., . and 5 are removed). * @param input The string in input. * @param charsDoNotBeRemoved The chars that do not be removed. * @return The string as output. */ public static String trimButChars (String input, String charsDoNotBeRemoved) { StringBuffer output = new StringBuffer(); boolean charFound=false; for (int index=0; index<input.length(); index++) { charFound=false; for (int charsCount=0; charsCount<charsDoNotBeRemoved.length(); charsCount++) if (charsDoNotBeRemoved.charAt(charsCount)==input.charAt(index)) charFound=true; if (charFound) output.append(input.charAt(index)); } return output.toString(); } /** * Remove from the beginning and the end of the input string all the characters * with the only exception of the characters specified in charsDoNotBeRemoved param. * <BR>The removal process removes only chars at the beginning and at the end of the string. * <BR>For example if you call trimButCharsBeginEnd("<DIV> +12.5 </DIV>", "+.1234567890"), * <BR>you obtain a string "+12.5" as output (+,.,1,2,3,4,5,6,7,8,9,0 are chars that do not be removed). * <BR>For example if you call trimButCharsBeginEnd("<DIV> +1 2 . 5 </DIV>", "+.1234567890"), * <BR>you obtain a string "+1 2 . 5" as output (the spaces inside the string are not removed). * @param input The string in input. * @param charsDoNotBeRemoved The chars that do not be removed. * @return The string as output. */ public static String trimButCharsBeginEnd (String input, String charsDoNotBeRemoved) { String output = new String(); int begin=0; int end=input.length()-1; boolean charFound=false; boolean ok=true; for (int index=begin; (index<input.length()) && ok; index++) { charFound=false; for (int charsCount=0; charsCount<charsDoNotBeRemoved.length(); charsCount++) if (charsDoNotBeRemoved.charAt(charsCount)==input.charAt(index)) charFound=true; if (charFound) { begin=index; ok=false; } } ok=true; for (int index=end; (index>=0) && ok; index--) { charFound=false; for (int charsCount=0; charsCount<charsDoNotBeRemoved.length(); charsCount++) if (charsDoNotBeRemoved.charAt(charsCount)==input.charAt(index)) charFound=true; if (charFound) { end=index; ok=false; } } output=input.substring(begin,end+1); return output; } /** * Split the input string considering as string separator * the chars specified in the input variable charsToBeRemoved. * <BR>For example if you call splitChars("<DIV> +12.5, +3.4 </DIV>", " <>DIV/,"), * <BR>you obtain an array of strings {"+12.5", "+3.4"} as output (space chars and <,>,D,I,V,/ and the comma are chars that must be removed). * @param input The string in input. * @param charsToBeRemoved The chars to be removed. * @return The array of strings as output. */ public static String[] splitChars (String input, String charsToBeRemoved) { ArrayList output = new ArrayList(); int minCapacity = 0; StringBuffer str = new StringBuffer(); boolean charFound = false; boolean toBeAdd = false; for (int index=0; index<input.length(); index++) { charFound=false; for (int charsCount=0; charsCount<charsToBeRemoved.length(); charsCount++) if (charsToBeRemoved.charAt(charsCount)==input.charAt(index)) charFound=true; if (!(charFound)) { str.append(input.charAt(index)); toBeAdd=false; } else if (!toBeAdd) toBeAdd=true; // finished to parse one string if (toBeAdd && (str.length()!=0)) { minCapacity++; output.ensureCapacity(minCapacity); if (output.add(str.toString())) str = new StringBuffer(); else minCapacity--; } } // add the last string if (str.length()!=0) { minCapacity++; output.ensureCapacity(minCapacity); if (output.add(str.toString())) str = new StringBuffer(); else minCapacity--; } output.trimToSize(); Object[] outputObj = output.toArray(); String[] outputStr = new String[output.size()]; for (int i=0; i<output.size(); i++) outputStr[i] = new String((String) outputObj[i]); return outputStr; } /** * Remove from the input string all the chars specified in the input variable charsToBeRemoved. * <BR>For example if you call trimChars("<DIV> +12.5 </DIV>", "<>DIV/ "), * <BR>you obtain a string "+12.5" as output (<,>,D,I,V,/ and space char are chars that must be removed). * <BR>For example if you call trimChars("<DIV> Trim All Chars Also The Ones Inside The String </DIV>", "<>DIV/ "), * <BR>you obtain a string "TrimAllCharsAlsoTheOnesInsideTheString" as output (all the spaces inside the string are removed). * @param input The string in input. * @param charsToBeRemoved The chars to be removed. * @return The string as output. */ public static String trimChars (String input, String charsToBeRemoved) { StringBuffer output = new StringBuffer(); boolean charFound=false; for (int index=0; index<input.length(); index++) { charFound=false; for (int charsCount=0; charsCount<charsToBeRemoved.length(); charsCount++) if (charsToBeRemoved.charAt(charsCount)==input.charAt(index)) charFound=true; if (!(charFound)) output.append(input.charAt(index)); } return output.toString(); } /** * Remove from the beginning and the end of the input string all the chars specified in the input variable charsToBeRemoved. * <BR>The removal process removes only chars at the beginning and at the end of the string. * <BR>For example if you call trimCharsBeginEnd("<DIV> +12.5 </DIV>", "<>DIV/ "), * <BR>you obtain a string "+12.5" as output (' ' is a space char and <,>,D,I,V,/ are chars that must be removed). * <BR>For example if you call trimCharsBeginEnd("<DIV> Trim all spaces but not the ones inside the string </DIV>", "<>DIV/ "),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -