package me.ag2s.epublib.util; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Various String utility functions. *
* Most of the functions herein are re-implementations of the ones in apache
* commons StringUtils. The reason for re-implementing this is that the
* functions are fairly simple and using my own implementation saves the
* inclusion of a 200Kb jar file.
*
* @author paul.siegmann
*/
public class StringUtil {
/**
* Changes a path containing '..', '.' and empty dirs into a path that
* doesn't. X/foo/../Y is changed into 'X/Y', etc. Does not handle invalid
* paths like "../".
*
* @param path path
* @return the normalized path
*/
public static String collapsePathDots(String path) {
String[] stringParts = path.split("/");
List
* If the text does not contain the given separator then the given text is
* returned.
*
* @param text d
* @param separator d
* @return the substring of the given text before the given separator.
*/
public static String substringBefore(String text, char separator) {
if (isEmpty(text)) {
return text;
}
int sepPos = text.indexOf(separator);
if (sepPos < 0) {
return text;
}
return text.substring(0, sepPos);
}
/**
* Gives the substring of the given text before the last occurrence of the
* given separator.
*
* If the text does not contain the given separator then the given text is
* returned.
*
* @param text d
* @param separator d
* @return the substring of the given text before the last occurrence of the given separator.
*/
public static String substringBeforeLast(String text, char separator) {
if (isEmpty(text)) {
return text;
}
int cPos = text.lastIndexOf(separator);
if (cPos < 0) {
return text;
}
return text.substring(0, cPos);
}
/**
* Gives the substring of the given text after the last occurrence of the
* given separator.
*
* If the text does not contain the given separator then "" is returned.
*
* @param text d
* @param separator d
* @return the substring of the given text after the last occurrence of the given separator.
*/
public static String substringAfterLast(String text, char separator) {
if (isEmpty(text)) {
return text;
}
int cPos = text.lastIndexOf(separator);
if (cPos < 0) {
return "";
}
return text.substring(cPos + 1);
}
/**
* Gives the substring of the given text after the given separator.
*
* If the text does not contain the given separator then "" is returned.
*
* @param text the input text
* @param c the separator char
* @return the substring of the given text after the given separator.
*/
public static String substringAfter(String text, char c) {
if (isEmpty(text)) {
return text;
}
int cPos = text.indexOf(c);
if (cPos < 0) {
return "";
}
return text.substring(cPos + 1);
}
public static String formatHtml(String text) {
StringBuilder body = new StringBuilder();
for (String s : text.split("\\r?\\n")) {
s = s.replaceAll("^\\s+|\\s+$", "");
if (s.length() > 0) {
//段落为一张图片才认定为图片章节/漫画并启用多看单图优化,否则认定为普通文字夹杂着的图片文字。
if (s.matches("(?i)^ ").append(s).append("]+)/?>$")) {
body.append(s.replaceAll("(?i)^
]+)/?>$",
"