📄 stringutil.java
字号:
{
if (from == null)
return null;
else
return copyFrom(from, start, from.length - 1);
}
public String[] copyFrom(String from[], int start, int end)
{
int stop = end;
if (from == null)
return null;
if (stop > from.length - 1)
stop = from.length - 1;
int count = (stop - start) + 1;
if (count < 1)
{
return new String[0];
} else
{
String result[] = new String[count];
System.arraycopy(from, start, result, 0, count);
return result;
}
}
public String cutTail(String text, String separator)
{
if (text == null || separator == null)
return text;
int index = text.lastIndexOf(separator);
if (index < 0)
return text;
else
return text.substring(0, index);
}
public String cutHead(String text, String separator)
{
if (text == null || separator == null)
return text;
int index = text.lastIndexOf(separator);
if (index < 0)
return text;
else
return text.substring(index + 1);
}
public String[] splitNameValue(String str, String separator)
{
String result[] = {
"", ""
};
if (str != null)
{
int index = str.indexOf(separator);
if (index > 0)
{
result[0] = str.substring(0, index);
result[1] = str.substring(index + separator.length());
} else
{
result[0] = str;
}
}
return result;
}
public String prefix(String str, String separator)
{
return prefix(str, separator, true);
}
public String suffix(String str, String separator)
{
return suffix(str, separator, true);
}
public String upTo(String str, String separator)
{
return prefix(str, separator, false);
}
public String startingFrom(String str, String separator)
{
return suffix(str, separator, false);
}
public String reverse(String str)
{
if (str == null)
return null;
char newStr[] = new char[str.length()];
StringCharacterIterator iterator = new StringCharacterIterator(str);
int i = 0;
for (char ch = iterator.last(); ch != '\uFFFF'; ch = iterator.previous())
{
newStr[i] = ch;
i++;
}
return new String(newStr);
}
public Map toMap(String str, String elementSeparator, String keyValueSeparator, Map map)
{
if (str == null)
return map;
Map result = ((Map) (map != null ? map : ((Map) (new Hashtable()))));
String elemSep = elementSeparator != null ? elementSeparator : ",";
String kvSep = keyValueSeparator != null ? keyValueSeparator : "=";
String assignments[] = parts(str, elemSep);
for (int i = 0; i < assignments.length; i++)
{
String nameValue[] = splitNameValue(assignments[i], kvSep);
nameValue[0] = nameValue[0].trim();
nameValue[1] = nameValue[1].trim();
if (nameValue[0].length() > 0)
result.put(nameValue[0], nameValue[1]);
}
return result;
}
public Map asMap(String str)
{
return toMap(str, null, null, null);
}
public Map asMap(String str, String elementSeparator)
{
return toMap(str, elementSeparator, null, null);
}
public Map asMap(String str, String elementSeparator, String keyValueSeparator)
{
return toMap(str, elementSeparator, keyValueSeparator, null);
}
public Map toMap(String str, String elementSeparator, Map map)
{
return toMap(str, elementSeparator, null, map);
}
public Map toMap(String str, Map map)
{
return toMap(str, null, null, map);
}
public Properties asProperties(String str)
{
return toProperties(str, null);
}
public Properties toProperties(String str, Properties properties)
{
Properties props = properties != null ? properties : new Properties();
return (Properties)toMap(str, null, null, props);
}
protected String trimSeparator(String text, String separator)
{
int sepLen = separator.length();
for (; text.startsWith(separator); text = text.substring(separator.length()));
for (; text.endsWith(separator); text = text.substring(0, text.length() - sepLen));
return text;
}
protected String[] parts(String text, String delimiters, boolean all)
{
ArrayList result = null;
StringTokenizer tokenizer = null;
if (text == null)
return null;
if (delimiters == null || delimiters.length() == 0)
{
String resultArray[] = {
text
};
return resultArray;
}
if (text.length() == 0)
return new String[0];
result = new ArrayList();
tokenizer = new StringTokenizer(text, delimiters, all);
if (all)
collectParts(result, tokenizer, delimiters);
else
collectParts(result, tokenizer);
return (String[])(String[])result.toArray(new String[0]);
}
protected void collectParts(List list, StringTokenizer tokenizer)
{
for (; tokenizer.hasMoreTokens(); list.add(tokenizer.nextToken()));
}
protected void collectParts(List list, StringTokenizer tokenizer, String delimiter)
{
boolean lastWasDelimiter = false;
while (tokenizer.hasMoreTokens())
{
String token = tokenizer.nextToken();
if (delimiter.indexOf(token) >= 0)
{
if (lastWasDelimiter)
list.add("");
lastWasDelimiter = true;
} else
{
list.add(token);
lastWasDelimiter = false;
}
}
}
protected String[] substrings(String text, String separator, boolean all)
{
int index = 0;
int start = 0;
int sepLen = 0;
int strLen = 0;
String str = text;
ArrayList strings = new ArrayList();
if (text == null)
return new String[0];
if (separator == null || separator.length() == 0)
if (text.length() == 0)
{
return new String[0];
} else
{
String resultArray[] = {
text
};
return resultArray;
}
if (!all)
str = trimSeparator(text, separator);
strLen = str.length();
if (strLen > 0)
{
sepLen = separator.length();
for (index = str.indexOf(separator, start); index >= 0; index = str.indexOf(separator, start))
{
if (all)
{
if (index > 0)
strings.add(str.substring(start, index));
} else
if (index > start + sepLen)
strings.add(str.substring(start, index));
start = index + sepLen;
}
if (start < strLen)
strings.add(str.substring(start));
}
return (String[])(String[])strings.toArray(new String[0]);
}
protected String padCh(String str, int len, char ch, boolean left)
{
StringBuffer buffer = null;
int missing = len - str.length();
if (missing <= 0)
return str;
buffer = new StringBuffer(len);
if (!left)
buffer.append(str);
for (int i = 1; i <= missing; i++)
buffer.append(ch);
if (left)
buffer.append(str);
return buffer.toString();
}
protected int indexOfString(String strArray[], String searchStr, boolean ignoreCase)
{
if (strArray == null || strArray.length == 0)
return -1;
boolean found = false;
for (int i = 0; i < strArray.length; i++)
{
if (strArray[i] == null)
{
if (searchStr == null)
found = true;
} else
if (ignoreCase)
found = strArray[i].equalsIgnoreCase(searchStr);
else
found = strArray[i].equals(searchStr);
if (found)
return i;
}
return -1;
}
protected String prefix(String str, String separator, boolean returnNull)
{
if (str == null)
return null;
if (separator == null)
return returnNull ? null : str;
int index = str.indexOf(separator);
if (index >= 0)
return str.substring(0, index);
else
return returnNull ? null : str;
}
protected String suffix(String str, String separator, boolean returnNull)
{
if (str == null)
return null;
if (separator == null)
return returnNull ? null : str;
int index = str.indexOf(separator);
if (index >= 0)
return str.substring(index + separator.length());
else
return returnNull ? null : str;
}
protected String[] removeFromStringArray(String strings[], String removeStrings[])
{
List list = new ArrayList(strings.length);
for (int i = 0; i < strings.length; i++)
{
boolean remains;
if (removeStrings == null)
remains = strings[i] != null;
else
remains = !contains(removeStrings, strings[i]);
if (remains)
list.add(strings[i]);
}
return (String[])(String[])list.toArray(new String[list.size()]);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -