StringB?r b = new StringB?r(\"Java\");
// Append
b.append(\" Almanac v1/\"); // Java Almanac v1/
b.append(3); // Java Almanac v1/3
// Set
int index = 15;
b.setCharAt(index, \'.\'); // Java Almanac v1.3
// Insert
index = 5;
b.insert(index, \"Developers \");// Java Developers Almanac v1.3
// Replace
int start = 27;
int end = 28;
b.replace(start, end, \"4\"); // Java Developers Almanac v1.4
// Delete
start = 24;
end = 25;
b.delete(start, end); // Java Developers Almanac 1.4
// Convert to string
String s = b.toString();
String s1 = \"a\";
String s2 = \"A\";
String s3 = \"B\";
// Check if identical 监测是否一致
boolean b = s1.eq ls(s2); // false
// Check if identical ignoring case
b = s1.eq lsIgnoreCase(s2); // tr
// Check order of two strings 监测两个字符串的顺序
int i = s1.compareTo(s2); // 32; lowercase follows uppercase
if (i < 0) {
// s1 precedes s2
} else if (i > 0) {
// s1 follows s2
} else {
// s1 eq ls s2
}
// Check order of two strings ignoring case
i = s1.compareToIgnoreCase(s3); // -1
if (i < 0) {
// s1 precedes s3
} else if (i > 0) {
// s1 follows s3
} else {
// s1 eq ls s3
}
// A string can also be compared with a StringB?r;
StringB?r sb = new StringB?r(\"a\");
b = s1.contentEq ls(sb); // tr
String string = \"Madam, I am Adam\";
// Starts with
boolean b = string.startsWith(\"Mad\"); // tr
// Ends with
b = string.endsWith(\"dam\"); // tr
// Anywhere
b = string.indexOf(\"I am\") > 0; // tr
// To ignore case, regular expressions must be used
// Starts with
b = string.matches(\"(?i)mad.*\");
// Ends with
b = string.matches(\"(?i).*adam\");
// Anywhere
b = string.matches(\"(?i).*i am.*\");
int start = 1;
int end = 4;
String s str = \"aString\".s string(start, end); // Str
String string = \"madam, i am Adam\";
// Characters
// First occurrence of a c
int index = string.indexOf(\'a\'); // 1
// Last occurrence
index = string.lastIndexOf(\'a\'); // 14
// Not found
index = string.lastIndexOf(\'z\'); // -1
// S strings
// First occurrence
index = string.indexOf(\"dam\"); // 1
// Last occurrence
index = string.lastIndexOf(\"dam\"); // 13
// Not found
index = string.lastIndexOf(\"z\"); // -1
// Replace all occurrences of \'a\' with \'o\'
String newString = string.replace(\'a\', \'o\');
static String replace(String str, String pattern, String replace) {
int s = 0;
int e = 0;
StringB?r result = new StringB?r();
while ((e = str.indexOf(pattern, s)) >= 0) {
result.append(str.s string(s, e));
result.append(replace);
s = e+pattern.length();
}
result.append(str.s string(s));
return result.toString();
}
// Convert to upper case
String upper = string.toUpperCase();
// Convert to lower case
String lower = string.toLowerCase();
// Use String.valOf()
String s = String.valOf(tr); // tr
s = String.valOf((byte)0x12); // 18
s = String.valOf((byte)0xFF); // -1
s = String.valOf(\'a\'); // a
s = String.valOf((short)123); // 123
s = String.valOf(123); // 123
s = String.valOf(123L); // 123
s = String.valOf(1.23F); // 1.23
s = String.valOf(1.23D); // 1.23
// Use +
s = \"\"+tr; // tr
s = \"\"+((byte)0x12); // 18
s = \"\"+((byte)0xFF); // -1
s = \"\"+\'a\'; // a
s = \"\"+((short)123); // 123
s = \"\"+123; // 123
s = \"\"+123L; // 123
s = \"\"+1.23F; // 1.23
s = \"\"+1.23D; // 1.23
try {
// Convert from Unicode to UTF-8
String string = \"abc\\嘹\\嘻\";
byte[] utf8 = string.getBytes(\"UTF-8\");
// Convert from UTF-8 to Unicode
string = new String(utf8, \"UTF-8\");
} catch (UnsupportedEncodingException e) {
}
char ch = \'\\嘹\';
Character.UnicodeBlock block = Character.UnicodeBlock.of(ch);
// Returns tr if s is a legal Java identifier.
p lic static boolean isJavaIdentifier(String s) {
if (s.length() == 0 || !Character.isJavaIdentifierStart(s.charAt(0))) {
return false;
}
for (int i=1; i
if (!Character.isJavaIdentifierPart(s.charAt(i))) {
return false;
}
}
return tr;
}
// Some examples
boolean b = isJavaIdentifier(\"my_var\"); // tr
b = isJavaIdentifier(\"my_var.1\"); // false
b = isJavaIdentifier(\"$my_var\"); // tr
b = isJavaIdentifier(\"\\Αvar\"); // tr
b = isJavaIdentifier(\"_\"); // tr
b = isJavaIdentifier(\"___FCKpd___175quot;); // tr
b = isJavaIdentifier(\"1$my_var\"); // false
package hxj;
import java.math.BigDecimal;
/**
* 由于Java的简单类型不能够精确的对浮点数进行运算,这个工具类提供精
* 确的浮点数运算,包括加减乘除和四舍五入。
*/
p lic class mymatch {
//默认除法运算精度
private static final int DEF_DIV_SCALE = 10;
//这个类不能实例化
private Arith() {}
/**
* 提供精确的加法运算。
* @param v1 被加数
* @param v2 加数
* @return 两个参数的和
*/
p lic static do le add(do le v1, do le v2) {
BigDecimal b1 = new BigDecimal(Do le.toString(v1));
BigDecimal b2 = new BigDecimal(Do le.toString(v2));
return b1.add(b2).do leVal();
}
/**
* 提供精确的减法运算。
* @param v1 被减数
* @param v2 减数
* @return 两个参数的差
*/
p lic static do le s (do le v1, do le v2) {
BigDecimal b1 = new BigDecimal(Do le.toString(v1));
BigDecimal b2 = new BigDecimal(Do le.toString(v2));
return b1.s tract(b2).do leVal();
}
/**
* 提供精确的乘法运算。
* @param v1 被乘数
* @param v2 乘数
* @return 两个参数的
*/
p lic static do le mul(do le v1, do le v2) {
BigDecimal b1 = new BigDecimal(Do le.toString(v1));
BigDecimal b2 = new BigDecimal(Do le.toString(v2));
return b1.multiply(b2).do leVal();
}
/**
* 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到
* 小数点以后10位,以后的数字四舍五入。
* @param v1 被除数
* @param v2 除数
* @return 两个参数的商
*/
p lic static do le div(do le v1, do le v2) {
return div(v1, v2, DEF_DIV_SCALE);
}
/**
* 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指
* 定精度,以后的数字四舍五入。
* @param v1 被除数
* @param v2 除数
* @param scale 表示表示需要精确到小数点以后几位。
* @return 两个参数的商
*/
p lic static do le div(do le v1, do le v2, int scale) {
if (scale < 0) {
throw new IllegalArgumentException(\"The scale must be a positive integer or zero\");
}
BigDecimal b1 = new BigDecimal(Do le.toString(v1));
BigDecimal b2 = new BigDecimal(Do le.toString(v2));
return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).do leVal();
}
/**
* 提供精确的小数位四舍五入处理。
* @param v 需要四舍五入的数字
* @param scale 小数点后保留几位
* @return 四舍五入后的结果
*/
p lic static do le round(do le v, int scale) {
if (scale < 0) {
throw new IllegalArgumentException(\"The scale must be a positive integer or zero\");
}
BigDecimal b = new BigDecimal(Do le.toString(v));
BigDecimal one = new BigDecimal(\"1\");
return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).do leVal();
}
};
byte b = Byte.parseByte(\"123\");
short s = Short.parseShort(\"123\");
int i = Integer.parseInt(\"123\");
long l = Long.parseLong(\"123\");
float f = Float.parseFloat(\"123.4\");
do le d = Do le.parseDo le(\"123.4e10\");
int i = 1023;
// Parse and format to binary
i = Integer.parseInt(\"1111111111\", 2); // 1023
String s = Integer.toString(i, 2); // 1111111111
// Parse and format to octal
i = Integer.parseInt(\"1777\", 8); // 1023
s = Integer.toString(i, 8); // 1777
// Parse and format to decimal
i = Integer.parseInt(\"1023\"); // 1023
s = Integer.toString(i); // 1023
// Parse and format to hexadecimal
i = Integer.parseInt(\"3ff\", 16); // 1023
s = Integer.toString(i, 16); // 3ff
// Parse and format to arbitrary radix <= Character.MAX_RADIX
int radix = 32;
i = Integer.parseInt(\"vv\", radix); // 1023
s = Integer.toString(i, radix); // vv