1.String就是字符串类型
它的常用方法
1.1 字符串构造
public class Test { public static void main(String[] args) { String str1 = "Hello"; String str2 = new String("abc"); char[] chars = {'g','a','o'};//字符数组 String str3 = new String(chars); System.out.println(str1); System.out.println(str2); System.out.println(str3); } }
String是引用类型,内部并不储存字符串本身
ctrl点开String的方法,可以看到它的方法中有这两条
private final byte[] value; private int hash;
这说明他实际上字符串是保存在char类型的数组中的
public static void main(String[] args) { String s1 = new String("hello"); String s2 = new String("world"); String s3 = s1; System.out.println(s3); }
1.2 String对象的比较
public class Test { public static void main(String[] args) { String s1 = new String("hello"); String s2 = new String("hello"); String s3 = new String("world"); String s4 = s1; System.out.println(s1 == s2); System.out.println(s1 == s3); System.out.println(s2 == s3); System.out.println(s4 == s1); System.out.println(s1.equals(s2)); }
false
false
false
true
true
1.比较是否是同一个对象
注意:对于内置类型,==比较的是变量中的值,对于引用类型==比较的引用中的地址
2.boolean equals(Object anObject)方法:按照字典序比较
字典序:字符大小的顺序
3.比较两个字符串的大小
public static void main(String[] args) { String s1 = new String("hello"); String s2 = new String("aello"); int ret = s1.compareTo(s2); if(ret > 0){ System.out.println("s1 > s2"); } else if (ret == 0) { System.out.println("s1 == s2"); }else{ System.out.println("s1 < s2"); } }
4.忽略大小写
比较两个字符串的大小
int ret = s1.compareToIgnoreCase(s2);
比较两个字符串的相等
System.out.println(s1.equalsIgnoreCase(s2));
1.3 查找字符串对象
方法1:
public static void main(String[] args) { String str = "abcdef"; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); System.out.println(ch); } }
方法二
public static void main(String[] args) { String str = "ababcabcd"; /* for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); System.out.println(ch); }*/ //从头开始找遇到第一个就结束 System.out.println(str.indexOf('c')); //从3开始找遇到第一个就结束 System.out.println(str.indexOf('c',3)); System.out.println(str.indexOf("abc")); System.out.println(str.indexOf("abc",4)); System.out.println("----------------------------------------"); //从后往前找,碰见第一个,返回它的下标 System.out.println(str.lastIndexOf('c')); System.out.println(str.lastIndexOf('c',3)); System.out.println(str.lastIndexOf("abc")); System.out.println(str.lastIndexOf("abc",3)); }
1.3字符串的转换
1.3.1 数值和字符串转化
String str = String.valueOf();
他有许多重载方法
class Student{ String name = "fjq"; int age = 21; public Student(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } } public class Test { public static void main(String[] args) { String str = String.valueOf(123); System.out.println(str); String str2 = String.valueOf(true); System.out.println(str2); String str3 = String.valueOf(12.56); System.out.println(str3); String str4 = String.valueOf(new Student("zxl",21)); System.out.println(str4);
System.out.println("-------------------------------------------------"); int val1 = Integer.parseInt("123"); System.out.println(val1 + 1); double val2 = Double.parseDouble("12.25"); System.out.println(val2 + 0.38);
}
1.3.2 大 小写的转换
public static void main(String[] args) { String str = "HelloTgjhvbs"; String ret = str.toUpperCase(); System.out.println(ret); String str1 = "HedsafHUNDFHBNDbs"; String ret1 = str1.toLowerCase(); System.out.println(ret1); }
1.3.3 字符串转数组
String str1 = "ABCDEFG"; System.out.println("-------------------"); char[] chars = str1.toCharArray(); System.out.println(Arrays.toString(chars));
//数组转字符串
String s2 = new String(chars); System.out.println(s2);