public class Demo03 {
public static void main(String[] args) {
String name = "hello";//"hello",常量存贮在字符串池当中。
System.out.println(name.hashCode());
name = "张三";//"张三",赋值给name变量,给字符串赋值时,并没有修改数据,而是重新开辟一个空间
System.out.println(name.hashCode());
String name2 = "张三";
System.out.println(name2.hashCode());
System.out.println("====================字符串的另一种创建方式,new String();=================");
//字符串的另一种创建方式,new String();
String str1 = new String("java");
String str2 = new String("java");
System.out.println(str1==str2);
}
}
常用方法
public int length();返回字符串的长度。 public char charAt(int index);根据下标获取字符。 public boolean contains(String str);判断当前字符串中是否包含str。
//字符串方法的使用
//1、length();返回字符串的长度
//2、charAt(int index);返回某个位置的字符
//3、contains(String str);判断是否包含某个子字符串
String s = "Welcome to China";
System.out.println(s.length());
System.out.println(s.charAt(s.length()-1));
System.out.println(s.contains("China"));
public char[] toCharArray();将字符串转换成数组。 public int indexOf(String str);查找str首次出现的下标,存在,则返回该下标;不存在,则返回-1。 public int lastIndexOf(String str);查找字符串在当前字符串中最后一次出现的下标索引。
//字符串方法的使用
//toCharArray();将字符串转换成数组。
//indexOf();查找str首次出现的下标,存在,则返回该下标;不存在,则返回-1。
//lastIndexOf();查找字符串在当前字符串中最后一次出现的下标索引。
String content = " Welcome to China ";
System.out.println(Arrays.toString(content.toCharArray()));
System.out.println(content.indexOf("o"));
System.out.println(content.indexOf("o",9));
System.out.println(content.lastIndexOf("o"));
System.out.println(content.lastIndexOf("o",9));
public String trim();去掉字符串前后的空格。 public String toUpperCase();将小写转成大写。 public boolean endWith(String str);判断字符串是否以str结尾。 public String replace(char oldChar, char newChar);将川字符串替换成新字符串 public StringDsplit(String str);根据str做拆分。
System.out.println("=====================字符串方法的使用3=====================");
//7trim();去掉字符串前后的空格。
//8toUpperCase();将小写转成大写。toLowerCase();将大写转换为小写。
//9endWith(String str);判断字符串是否以str结尾。,startwith(String str);判断是否以str开头。
System.out.println(content.trim());
System.out.println(content.toUpperCase());
System.out.println(content.toLowerCase());
System.out.println(content.endsWith(" "));
System.out.println(content.startsWith(" "));
System.out.println("=====================字符串方法的使用4=====================");
//replace();将旧的字符或字符串替换成新的字符或字符串
//split(String str);根据str做拆分。
System.out.println(content.replace(" ",""));
System.out.println(content.replace(' ','-'));
String[] split = content.split("[ ]+");
for (String i : split){
System.out.print(i+" ");
}
System.out.println();
System.out.println("=====================补充方法=====================");
//equals();、compare();
String s1 = "hello";
String s2 = "HELLO";
System.out.println(s1.equals(s2));
System.out.println(s1.equalsIgnoreCase(s2));
System.out.println(s2.equals(s1));
String s3 = "abc";
String s4 = "ABCd";
System.out.println(s3.compareToIgnoreCase(s4));
System.out.println(s4.compareTo(s3));
可变字符串
StringBuffer:可变长字符串,JDK1.0提供,运行效率慢、线程安全。 StringBuilder:可变长字符串,JDK5.0提供,运行效率快、线程不安全。
public class Demo05 {
public static void main(String[] args) {
StringBuffer SB = new StringBuffer();
//1、append();追加
SB.append("落日与晚风\n");
System.out.println(SB.toString());
SB.append("深情地相拥\n");
System.out.println(SB.toString());
SB.append("曾相遇的路口\n");
System.out.println(SB.toString());
//2、insert();添加
SB.insert(0,"记忆盘旋了很久\n");
System.out.println(SB.toString());
//3、replace();替换
SB.replace(0,7,"退后的接口");
System.out.println(SB.toString());
//4、delete();删除
SB.delete(0,6);
System.out.println(SB.toString());
//5、reverse();反转
SB.reverse();
System.out.println(SB.toString());
//清空
SB.delete(0,SB.length());
System.out.println("\""+ SB.toString()+"\"");
System.out.println(SB.length());
}
}
验证执行效率
public class Demo06 {
public static void main(String[] args) {
long start = System.currentTimeMillis();
String str = "";
for (int i = 0; i < 29999; i++) {
str+=i;
}
long end = System.currentTimeMillis();
System.out.println("用时:"+(end-start));
start = System.currentTimeMillis();
StringBuffer strB = new StringBuffer("");
for (int i = 0; i < 99999; i++) {
strB.append(i);
}
end = System.currentTimeMillis();
System.out.println("用时:"+(end-start));
start = System.currentTimeMillis();
StringBuilder strc = new StringBuilder("");
for (int i = 0; i < 29999; i++) {
strc.append(i);
}
end = System.currentTimeMillis();
System.out.println("用时:"+(end-start));
}