📄 library.java
字号:
return s1;
}
if (start + length > len1) {
length = len1 - start;
}
return s1.substring(0, start) + s2 + s1.substring(start + length);
}
/**
* Returns a copy of the given <code>String</code>, with all upper case
* characters converted to lower case. This uses the default Java String
* conversion.
* @param s the <code>String</code> from which to produce a lower case
* version
* @return a lower case version of <code>s</code>
*/
public static String lcase(String s) {
return (s == null) ? null
: s.toLowerCase();
}
/**
* Returns the leftmost <code>count</code> characters from the given
* <code>String</code>. <p>
*
* <b>Note:</b> boundry conditions are handled in the following order of
* precedence:
*
* <UL>
* <LI> if <code>s</code> is <code>null</code>, then <code>null</code>
* is returned
* <LI> if <code>count</code> is less than 1, then a zero-length
* <code>String</code> is returned
* <LI> if <code>count</code> is greater than the length of <code>s</code>,
* then a copy of <code>s</code> is returned
* </UL>
* @param s the <code>String</code> from which to retrieve the leftmost
* characters
* @param count the count of leftmost characters to retrieve
* @return the leftmost <code>count</code> characters of <code>s</code>
*/
public static String left(String s, int count) {
if (s == null) {
return null;
}
return s.substring(0, ((count < 0) ? 0
: (count < s.length()) ? count
: s.length()));
}
// fredt@users - 20020819 - patch 595854 by thomasm@users
/**
* Returns the number of characters in the given <code>String</code>.
* This includes trailing blanks.
*
* @param s the <code>String</code> for which to determine length
* @return the length of <code>s</code>, including trailing blanks
*/
public static Integer length(String s) {
return s == null ? null
: ValuePool.getInt(s.length());
}
/**
* Returns the number of bytes in the given <code>String</code>.
* This includes trailing blanks.
*
* @param s the <code>String</code> for which to determine the octet length
* @return the octet length of <code>s</code>, including trailing blanks
* @since 1.7.2
*/
public static Integer octetLength(String s) {
return s == null ? null
: ValuePool.getInt(s.length() * 2);
}
/**
* Returns the number of bits in the given <code>String</code>.
* This includes trailing blanks.
*
* @param s the <code>String</code> for which to determine the bit length
* @return the bit length of <code>s</code>, including trailing blanks
* @since 1.7.2
*/
public static Integer bitLength(String s) {
return s == null ? null
: ValuePool.getInt(s.length() * 16);
}
/**
* Returns the starting position of the first occurrence of
* the given <code>search</code> <code>String</code> object within
* the given <code>String</code> object, <code>s</code>.
*
* The search for the first occurrence of <code>search</code> begins with
* the first character position in <code>s</code>, unless the optional
* argument, <code>start</code>, is specified (non-null). If
* <code>start</code> is specified, the search begins with the character
* position indicated by the value of <code>start</code>, where the
* first character position in <code>s</code> is indicated by the value 1.
* If <code>search</code> is not found within <code>s</code>, the
* value 0 is returned.
* @param search the <code>String</code> occurence to find in <code>s</code>
* @param s the <code>String</code> within which to find the first
* occurence of <code>search</code>
* @param start the optional character position from which to start
* looking in <code>s</code>
* @return the one-based starting position of the first occurrence of
* <code>search</code> within <code>s</code>, or 0 if not found
*/
public static int locate(String search, String s, Integer start) {
if (s == null || search == null) {
return 0;
}
int i = (start == null) ? 0
: start.intValue() - 1;
return s.indexOf(search, (i < 0) ? 0
: i) + 1;
}
/**
* As locate but from start position l. <p>
*
* @param search the <code>String</code> occurence to find in <code>s</code>
* @param s the <code>String</code> within which to find the first
* occurence of <code>search</code>
* @return the one-based starting position of the first occurrence of
* <code>search</code> within <code>s</code>, or 0 if not found
*/
public static int position(String search, String s) {
return locate(search, s, null);
}
/**
* Returns the characters of the given <code>String</code>, with the
* leading spaces removed. Characters such as TAB are not removed.
*
* @param s the <code>String</code> from which to remove the leading blanks
* @return the characters of the given <code>String</code>, with the leading
* spaces removed
*/
public static String ltrim(String s) {
if (s == null) {
return s;
}
int len = s.length(),
i = 0;
while (i < len && s.charAt(i) <= ' ') {
i++;
}
return (i == 0) ? s
: s.substring(i);
}
/**
* Converts a raw binary value, as represented by the given
* <code>String</code>, to the equivalent <code>String</code>
* of hexidecimal digit characters. <p>
*
* This conversion has the effect of expanding the character count 1:4.
*
* @param s the raw binary value, as a <code>String</code>
* @return an equivalent <code>String</code> of hexidecimal digit characters
*/
public static String rawToHex(String s) {
if (s == null) {
return null;
}
char[] from = s.toCharArray();
String hex;
StringBuffer to = new StringBuffer(4 * s.length());
for (int i = 0; i < from.length; i++) {
hex = Integer.toHexString(from[i] & 0xffff);
for (int j = hex.length(); j < 4; j++) {
to.append('0');
}
to.append(hex);
}
return (to.toString());
}
/**
* Returns a <code>String</code> composed of the given <code>String</code>,
* repeated <code>count</code> times.
*
* @param s the <code>String</code> to repeat
* @param count the number of repetitions
* @return the given <code>String</code>, repeated <code>count</code> times
*/
public static String repeat(String s, Integer count) {
if (s == null || count == null || count.intValue() < 0) {
return null;
}
int i = count.intValue();
StringBuffer b = new StringBuffer(s.length() * i);
while (i-- > 0) {
b.append(s);
}
return b.toString();
}
// fredt@users - 20020903 - patch 1.7.1 - bug fix to allow multiple replaces
/**
* Replaces all occurrences of <code>replace</code> in <code>s</code>
* with the <code>String</code> object: <code>with</code>
* @param s the target for replacement
* @param replace the substring(s), if any, in <code>s</code> to replace
* @param with the value to substitute for <code>replace</code>
* @return <code>s</code>, with all occurences of <code>replace</code>
* replaced by <code>with</code>
*/
public static String replace(String s, String replace, String with) {
if (s == null || replace == null) {
return s;
}
if (with == null) {
with = "";
}
StringBuffer b = new StringBuffer();
int start = 0;
int lenreplace = replace.length();
while (true) {
int i = s.indexOf(replace, start);
if (i == -1) {
b.append(s.substring(start));
break;
}
b.append(s.substring(start, i));
b.append(with);
start = i + lenreplace;
}
return b.toString();
}
/**
* Returns the rightmost <code>count</code> characters of the given
* <code>String</code>, <code>s</code>. <p>
*
* <b>Note:</b> boundry conditions are handled in the following order of
* precedence: <p>
*
* <UL>
* <LI> if <code>s</code> is <code>null</code>, <code>null</code> is returned
* <LI> if <code>count</code> is less than one, a zero-length
* <code>String</code> is returned
* <LI> if <code>count</code> is greater than the length of <code>s</code>,
* a copy of <code>s</code> is returned
* </UL>
* @param s the <code>String</code> from which to retrieve the rightmost
* <code>count</code> characters
* @param count the number of rightmost characters to retrieve
* @return the rightmost <code>count</code> characters of <code>s</code>
*/
public static String right(String s, int count) {
if (s == null) {
return null;
}
count = s.length() - count;
return s.substring((count < 0) ? 0
: (count < s.length()) ? count
: s.length());
}
// fredt@users 20020530 - patch 1.7.0 fredt - trim only the space character
/**
* Returns the characters of the given <code>String</code>, with trailing
* spaces removed.
* @param s the <code>String</code> from which to remove the trailing blanks
* @return the characters of the given <code>String</code>, with the
* trailing spaces removed
*/
public static String rtrim(String s) {
if (s == null) {
return s;
}
int endindex = s.length() - 1;
int i = endindex;
for (; i >= 0 && s.charAt(i) == ' '; i--) {}
return i == endindex ? s
: s.substring(0, i + 1);
}
/**
* Returns the character sequence <code>s</code>, with the leading,
* trailing or both the leading and trailing occurences of the first
* character of the character sequence <code>trimstr</code> removed. <p>
*
* This method is in support of the standard SQL String function TRIM.
* Ordinarily, the functionality of this method is accessed from SQL using
* the following syntax: <p>
*
* <pre class="SqlCodeExample">
* <trim function> ::= TRIM <left paren> <trim operands> <right paren>
* <trim operands> ::= [ [ <trim specification> ] [ <trim character> ] FROM ] <trim source>
* <trim source> ::= <character value expression>
* <trim specification> ::= LEADING | TRAILING | BOTH
* <trim character> ::= <character value expression>
* </pre>
*
* @param s the string to trim
* @param trimstr the character whose occurences will be removed
* @param leading if true, remove leading occurences
* @param trailing if true, remove trailing occurences
* @return s, with the leading, trailing or both the leading and trailing
* occurences of the first character of <code>trimstr</code> removed
* @since 1.7.2
*/
public static String trim(String s, String trimstr, boolean leading,
boolean trailing) {
if (s == null) {
return s;
}
int trim = trimstr.charAt(0);
int endindex = s.length();
if (trailing) {
for (--endindex; endindex >= 0 && s.charAt(endindex) == trim;
endindex--) {}
endindex++;
}
if (endindex == 0) {
return "";
}
int startindex = 0;
if (leading) {
while (startindex < endindex && s.charAt(startindex) == trim) {
startindex++;
}
}
if (startindex == 0 && endindex == s.length()) {
return s;
} else {
return s.substring(startindex, endindex);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -