java常用类
String类
hashCode()
返回字符串的哈希码(相当于一个地址,用户指定字符串存放的地址,在hashset中大量使用。
indexOf()
public class Demo03 {
public static void main(String[] args) {
String str = "123";
int index = str.indexOf("1");
System.out.println(index);
}
}
public class Demo03 {
public static void main(String[] args) {
String str = "1233333";
int index = str.indexOf('c',2); // ch;fromindex
System.out.println(index); // 1
}
}
public class Demo03 {
public static void main(String[] args) {
String str = "1233333";
int index = str.indexOf('3',2);
int index1 = str.lastIndexOf('3');
System.out.println(index); // 1
System.out.println(index1); // 6
}
}
replace()
// replace()
String s1 = "hi, good morning";
String s2 = s1.replace('o','e');
System.out.println(s2); // hi, geed merning
String s1 = "hi, good morning, good boy";
String s3 = s1.replace("good","bad");
System.out.println(s3); // hi, bad morning, bad boy
replaceFirst() 只替换第一个匹配的元素
split()
返回值是数组
// split()
String s4 = "123,343,213";
String[] s5 = s4.split(",");
for (int i = 0; i < s5.length; i++) {
System.out.println(s5[i]);
}
// 123
// 343
// 213
// 限制使用次数
String[] s5 = s4.split(",",2);
for (int i = 0; i < s5.length; i++) {
System.out.println(s5[i]);
// 123
// 343,213
}
startsWith()
// s4 从 索引位置4 开始 是否以"34"开头
System.out.println(s4.startsWith("34",4)); // true
substring
public class Demo04 {
public static void main(String[] args) {
String str = "123,231,314,563";
System.out.println(str.substring(3)); // ,231,314,563
System.out.println(str.substring(3,5)); // not include 5
// output: ,2
}
}
toLowerCase()
String str = "123,231,A314,563";
System.out.println(str.toLowerCase()); // 123,231,a314,563
String s1 = "adf";
System.out.println(str.toLowerCase()); // 123,231,a314,563
System.out.println(s1.toUpperCase()); // ADF
trim()
忽略前导空白和尾部空白
源码分析
string是一旦定义就不可改变的
private final char value[]
字符串比较用 equals()
public class Demo05 {
public static void main(String[] args) {
String s1 = "hello" + " java";
String s2 = "hello java";
System.out.println(s1 == s2); // true
System.out.println(s1.equals(s2)); // true
String s3 = "hello";
String s4 = " java";
String s5 = s3 + s4;
System.out.println(s2 == s5); // false
System.out.println(s2.equals(s5)); //true
}
}
stringBuilder & stringBuffer
StringBuilder:
char[] value
没有final修饰,可变的字符序列
两者的区别:StringBuilder线程不安全,效率高;StringBuffer线程安全,效率低
一般使用StringBuilder
public class Demo06 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("dsfaafgar");
System.out.println(Integer.toHexString(sb.hashCode()));
System.out.println(sb);
sb.setCharAt(2,'M');
System.out.println(Integer.toHexString(sb.hashCode()));
System.out.println(sb);
// output: 地址没有改变,说明是同一个对象
// 61bbe9ba
// dsfaafgar
// 61bbe9ba
// dsMaafgar
用法
public class Demo07 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 26; i++){
sb.append((char)('a'+i));
}
System.out.println(sb); // abcdefghijklmnopqrstuvwxyz
sb.reverse();
System.out.println(sb); // zyxwvutsrqponmlkjihgfedcba
sb.setCharAt(3,'M');
System.out.println(sb); // zyxMvutsrqponmlkjihgfedcba
sb.insert(0,'7');
System.out.println(sb); // 7zyxMvutsrqponmlkjihgfedcba
// insert 支持链式调用,因为这个方法return this,将自己返回了
sb.insert(0,'2').insert(5,'9');
System.out.println(sb); // 27zyx9Mvutsrqponmlkjihgfedcba
sb.delete(20,23); // 也支持链式调用
System.out.println(sb); // 27zyx9Mvutsrqponmlkjfedcba
}
}
测试可变/不可变字符序列的使用
// 测试可变字符序列和不可变字符序列使用的陷阱
public class Demo08 {
public static void main(String[] args) {
// 使用string进行字符串拼接
String str = "";
long num1 = Runtime.getRuntime().freeMemory();
long time1 = System.currentTimeMillis();
for(int i = 0;i < 5000;i++){
str = str+i; // 相当于产生了10000个对象
// i也会作为一个对象进行字符串的拼接
// 耗费时间和空间 尽量避免
}
long num2 = Runtime.getRuntime().freeMemory();
long time2 = System.currentTimeMillis();
System.out.println("string take memory: "+ (num1-num2));
System.out.println("string take time: "+ (time2-time1));
// 使用stringbuilder进行字符串拼接(推荐使用)
StringBuilder sb1 = new StringBuilder("");
long num3 = Runtime.getRuntime().freeMemory();
long time3 = System.currentTimeMillis();
for(int i=0;i<5000;i++){
sb1.append(i); // 不会频繁产生额外的对象 节省时间和空间
}
long num4 = Runtime.getRuntime().freeMemory();
long time4 = System.currentTimeMillis();
System.out.println("string take memory: "+ (num3-num4));
System.out.println("string take time: "+ (time4-time3));
}
}
output:
string take memory: 33158248
string take time: 79
stringbuilder take memory: 0
stringbuilder take time: 0