StringBuffer buf = new StringBuffer("Java");
// Append
buf.append(" Almanac v1/"); // Java Almanac v1/
buf.append(3); // Java Almanac v1/3
// Set
int index = 15;
buf.setCharAt(index, '.'); // Java Almanac v1.3
// Insert
index = 5;
buf.insert(index, "Developers ");// Java Developers Almanac v1.3
// Replace
int start = 27;
int end = 28;
buf.replace(start, end, "4"); // Java Developers Almanac v1.4
// Delete
start = 24;
end = 25;
buf.delete(start, end); // Java Developers Almanac 1.4
// Convert to string
String s = buf.toString();
比较字符串
String s1 = "a";
String s2 = "A";
String s3 = "B";
// Check if identical 监测是否一致
boolean b = s1.equals(s2); // false
// Check if identical ignoring case
b = s1.equalsIgnoreCase(s2); // true
// 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 equals 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 equals s3
}
// A string can also be compared with a StringBuffer;
StringBuffer sbuf = new StringBuffer("a");
b = s1.contentEquals(sbuf); // true
判断一个字符串中是否包含另外一个字符串
String string = "Madam, I am Adam";
// Starts with
boolean b = string.startsWith("Mad"); // true
// Ends with
b = string.endsWith("dam"); // true
// Anywhere
b = string.indexOf("I am") > 0; // true
// 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 substr = "aString".substring(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
// Substrings
// 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;
StringBuffer result = new StringBuffer();
while ((e = str.indexOf(pattern, s)) >= 0) {
result.append(str.substring(s, e));
result.append(replace);
s = e+pattern.length();
}
result.append(str.substring(s));
return result.toString();
}
转变字符串的大小写
// Convert to upper case
String upper = string.toUpperCase();
// Convert to lower case
String lower = string.toLowerCase();
Converting a Primitive Type Value to a String
// Use String.valueOf()
String s = String.valueOf(true); // true
s = String.valueOf((byte)0x12); // 18
s = String.valueOf((byte)0xFF); // -1
s = String.valueOf('a'); // a
s = String.valueOf((short)123); // 123
s = String.valueOf(123); // 123
s = String.valueOf(123L); // 123
s = String.valueOf(1.23F); // 1.23
s = String.valueOf(1.23D); // 1.23
// Use +
s = ""+true; // true
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
Converting Between Unicode and UTF-8
try {
// Convert from Unicode to UTF-8
String string = "abc/u5639/u563b";
byte[] utf8 = string.getBytes("UTF-8");
// Convert from UTF-8 to Unicode
string = new String(utf8, "UTF-8");
} catch (UnsupportedEncodingException e) {
}
Determining a Character's Unicode Block
char ch = '/u5639';
Character.UnicodeBlock block = Character.UnicodeBlock.of(ch);
Determining If a String Is a Legal Java Identifier
// Returns true if s is a legal Java identifier.
public static boolean isJavaIdentifier(String s) {
if (s.length() == 0 || !Character.isJavaIdentifierStart(s.charAt(0))) {
return false;
}
for (int i=1; i<s.length(); i++) {
if (!Character.isJavaIdentifierPart(s.charAt(i))) {
return false;
}
}
return true;
}
// Some examples
boolean b = isJavaIdentifier("my_var"); // true
b = isJavaIdentifier("my_var.1"); // false
b = isJavaIdentifier("$my_var"); // true
b = isJavaIdentifier("/u0391var"); // true
b = isJavaIdentifier("_"); // true
b = isJavaIdentifier("___FCKpd___175quot;); // true
b = isJavaIdentifier("1$my_var"); // false
Numbers
精确计算类实例
package hxj;
import java.math.BigDecimal;
/**
*
由于
Java
的简单类型不能够精确的对浮点数进行运算,这个工具类提供精
*
确的浮点数运算,包括加减乘除和四舍五入。
*/
public class mymatch {
//
默认除法运算精度
private static final int DEF_DIV_SCALE = 10;
//
这个类不能实例化
private Arith() {}
/**
*
提供精确的加法运算。
* @param v1
被加数
* @param v2
加数
* @return
两个参数的和
*/
public static double add(double v1, double v2) {
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.add(b2).doubleValue();
}
/**
*
提供精确的减法运算。
* @param v1
被减数
* @param v2
减数
* @return
两个参数的差
*/
public static double sub(double v1, double v2) {
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.subtract(b2).doubleValue();
}
/**
*
提供精确的乘法运算。
* @param v1
被乘数
* @param v2
乘数
* @return
两个参数的
*/
public static double mul(double v1, double v2) {
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.multiply(b2).doubleValue();
}
/**
*
提供(相对)精确的除法运算,当发生除不尽的情况时,精确到
*
小数点以后
10
位,以后的数字四舍五入。
* @param v1
被除数
* @param v2
除数
* @return
两个参数的商
*/
public static double div(double v1, double v2) {
return div(v1, v2, DEF_DIV_SCALE);
}
/**
*
提供(相对)精确的除法运算。当发生除不尽的情况时,由
scale
参数指
*
定精度,以后的数字四舍五入。
* @param v1
被除数
* @param v2
除数
* @param scale
表示表示需要精确到小数点以后几位。
* @return
两个参数的商
*/
public static double div(double v1, double v2, int scale) {
if (scale < 0) {
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
/**
*
提供精确的小数位四舍五入处理。
* @param v
需要四舍五入的数字
* @param scale
小数点后保留几位
* @return
四舍五入后的结果
*/
public static double round(double v, int scale) {
if (scale < 0) {
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
};
转变字符串为数字格式
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");
double d = Double.parseDouble("123.4e10");
Parsing and Formatting a Number into Binary, Octal, and Hexadecimal
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