📄 stringutil.java
字号:
}
start[count] = s;
}
count++;
String[] result = new String[count];
for (int i = 0; i < count; i++) {
result[i] = src.substring(start[i], end[i]);
}
return result;
}
// ---------------------------------------------------------------- byte arrays
/**
* Converts string into byte array. Chars are truncated to byte size.
*
* @param s string to convert from
*
* @return byte array
*/
public static byte[] toByteArray(String s) {
if (s == null) {
return null;
}
return CharUtil.toByteArray(s.toCharArray());
}
// ---------------------------------------------------------------- ignore cases
/**
* Finds first index of a substring in the given source string with ignored case.
*
* @param src source string for examination
* @param subS substring to find
*
* @return index of founded substring or -1 if substring is not found
* @see #indexOfIgnoreCase(String, String, int)
*/
public static int indexOfIgnoreCase(String src, String subS) {
return indexOfIgnoreCase(src, subS, 0);
}
/**
* Finds first index of a substring in the given source string with ignored
* case. This seems to be the fastest way doing this, with common string
* length and content (of course, with no use of Boyer-Mayer type of
* algorithms). Other implementations are slower: getting char array frist,
* lowercasing the source string, using String.regionMatch etc.
*
* @param src source string for examination
* @param subS substring to find
* @param startIndex starting index from where search begins
*
* @return index of founded substring or -1 if substring is not found
*/
public static int indexOfIgnoreCase(String src, String subS, int startIndex) {
String sub = subS.toLowerCase();
int sublen = sub.length();
int total = src.length() - sublen + 1;
for (int i = startIndex; i < total; i++) {
int j = 0;
while (j < sublen) {
char source = Character.toLowerCase(src.charAt(i + j));
if (sub.charAt(j) != source) {
break;
}
j++;
}
if (j == sublen) {
return i;
}
}
return -1;
}
/**
* Finds last index of a substring in the given source string with ignored
* case.
*
* @param s
* @param subS substring to find
*
* @return last index of founded substring or -1 if substring is not found
* @see #indexOfIgnoreCase(String, String, int)
* @see #lastIndexOfIgnoreCase(String, String, int)
*/
public static int lastIndexOfIgnoreCase(String s, String subS) {
return lastIndexOfIgnoreCase(s, subS, 0);
}
/**
* Finds last index of a substring in the given source string with ignored
* case.
*
* @param src source string for examination
* @param subS substring to find
* @param startIndex starting index from where search begins
*
* @return last index of founded substring or -1 if substring is not found
* @see #indexOfIgnoreCase(String, String, int)
*/
public static int lastIndexOfIgnoreCase(String src, String subS, int startIndex) {
String sub = subS.toLowerCase();
int sublen = sub.length();
int total = src.length() - sublen;
for (int i = total; i >= startIndex; i--) {
int j = 0;
while (j < sublen) {
char source = Character.toLowerCase(src.charAt(i + j));
if (sub.charAt(j) != source) {
break;
}
j++;
}
if (j == sublen) {
return i;
}
}
return -1;
}
/**
* Tests if this string starts with the specified prefix with ignored case.
*
* @param src source string to test
* @param subS starting substring
*
* @return <code>true</code> if the character sequence represented by the argument is
* a prefix of the character sequence represented by this string;
* <code>false</code> otherwise.
*/
public static boolean startsWithIgnoreCase(String src, String subS) {
return startsWithIgnoreCase(src, subS, 0);
}
/**
* Tests if this string starts with the specified prefix with ignored case
* and with the specified prefix beginning a specified index.
*
* @param src source string to test
* @param subS starting substring
* @param startIndex index from where to test
*
* @return <code>true</code> if the character sequence represented by the argument is
* a prefix of the character sequence represented by this string;
* <code>false</code> otherwise.
*/
public static boolean startsWithIgnoreCase(String src, String subS, int startIndex) {
String sub = subS.toLowerCase();
int sublen = sub.length();
if (startIndex + sublen > src.length()) {
return false;
}
int j = 0;
int i = startIndex;
while (j < sublen) {
char source = Character.toLowerCase(src.charAt(i));
if (sub.charAt(j) != source) {
return false;
}
j++; i++;
}
return true;
}
/**
* Tests if this string ends with the specified suffix.
*
* @param src String to test
* @param subS suffix
*
* @return <code>true</code> if the character sequence represented by the argument is
* a suffix of the character sequence represented by this object;
* <code>false</code> otherwise.
*/
public static boolean endsWithIgnoreCase(String src, String subS) {
String sub = subS.toLowerCase();
int sublen = sub.length();
int j = 0;
int i = src.length() - sublen;
if (i < 0) {
return false;
}
while (j < sublen) {
char source = Character.toLowerCase(src.charAt(i));
if (sub.charAt(j) != source) {
return false;
}
j++; i++;
}
return true;
}
// ---------------------------------------------------------------- wildcard match
/**
* Checks whether a string matches a given wildcard pattern.
* Possible patterns allow to match single characters ('?') or any count of
* characters ('*'). Wildcard characters can be escaped (by an '\').
*
* <p>This method uses recursive matching, as in linux or windows. regexp works the same.
* This method is very fast, comparing to similar implementations.
*
* @param string input string
* @param pattern pattern to match
* @return <code>true</code> if string matches the pattern, otherwise <code>fasle</code>
*/
public static final boolean match(String string, String pattern) {
return match(string, pattern, 0, 0);
}
/**
* Internall matching recursive function.
*/
private static boolean match(String string, String pattern, int stringStartNdx, int patternStartNdx) {
int pNdx = patternStartNdx;
int sNdx = stringStartNdx;
int pLen = pattern.length();
int sLen = string.length();
boolean nextIsNotWildcard = false;
while (true) {
// check if end of string and/or pattern occured
if ((sNdx >= sLen) == true) { // end of string still may have pending '*' in pattern
while ((pNdx < pLen) && (pattern.charAt(pNdx) == '*')) {
pNdx++;
}
if (pNdx >= pLen) { // end of both string and pattern
return true;
}
return false;
}
if (pNdx >= pLen) { // end pf pattern, but not end of the string
return false;
}
char p = pattern.charAt(pNdx); // pattern char
// perform logic
if (nextIsNotWildcard == false) {
if (p == '\\') {
pNdx++;
nextIsNotWildcard = true;
continue;
}
if (p == '?') {
sNdx++; pNdx++;
continue;
}
if (p == '*') {
char pnext = 0; // next pattern char
if (pNdx + 1 < pLen) {
pnext = pattern.charAt(pNdx + 1);
}
if (pnext == '*') { // double '*' have the same effect as one '*'
pNdx++;
continue;
}
int i;
pNdx++;
// find recursively if there is any substring from the end of the
// line that matches the rest of the pattern !!!
for (i = string.length(); i >= sNdx; i--) {
if (match(string, pattern, i, pNdx) == true) {
return true;
}
}
return false;
}
} else {
nextIsNotWildcard = false;
}
// check if pattern char and string char are equals
if (p != string.charAt(sNdx)) {
return false;
}
// everything matches for now, continue
sNdx++; pNdx++;
}
}
// ---------------------------------------------------------------- count substrings
/**
* Count substring occurences in a source string.
*
* @param source source string
* @param sub substring to count
* @return number of substring occurences
*/
public final static int count(String source, String sub) {
int count = 0;
int i = 0, j = 0;
while (true) {
i = source.indexOf(sub, j);
if (i == -1) {
break;
}
count++;
j = i + sub.length();
}
return count;
}
/**
* Count substring occurences in a source string, ignoring case.
*
* @param source source string
* @param sub substring to count
* @return number of substring occurences
*/
public final static int countIgnoreCase(String source, String sub) {
int count = 0;
int i = 0, j = 0;
while (true) {
i = StringUtil.indexOfIgnoreCase(source, sub, j);
if (i == -1) {
break;
}
count++;
j = i + sub.length();
}
return count;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -