String是字符串类型,C语言里面没有这个类型。
String提供的构造方法特别多常用的有下面三种:
String对象的比较
对于内置类型,==比较的是变量中的值;对于引用类型==比较的是引用中的地址用equals来比较。
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 10;
// 对于基本类型变量,==比较两个变量中存储的值是否相同
System.out.println(a == b); // false
System.out.println(a == c); // true
// 对于引用类型变量,==比较两个引用变量引用的是否为同一个对象
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = new String("world");
String s4 = s1;
System.out.println(s1 == s2); // false
System.out.println(s2 == s3); // false
System.out.println(s1 == s4); // true
}
equals比较的是储存的值,与==比较地址是否相同不一样:
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 10;
// 对于基本类型变量,==比较两个变量中存储的值是否相同
System.out.println(a == b); // false
System.out.println(a == c); // true
// 对于引用类型变量,==比较两个引用变量引用的是否为同一个对象
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = new String("world");
String s4 = s1;
System.out.println(s1.equals(s2)); // true
System.out.println(s2.equals(s3)); // true
System.out.println(s1.equals(s4)); // false
}
比较字符串的大小要用compareTo:
public static void main(String[] args) {
String s1 = new String("abc");
String s2 = new String("ac");
String s3 = new String("abc");
String s4 = new String("abcdef");
System.out.println(s1.compareTo(s2)); // 不同输出字符差值-1
System.out.println(s1.compareTo(s3)); // 相同输出 0
System.out.println(s1.compareTo(s4)); // 前k个字符完全相同,输出长度差值 -3
}
s1和s2比,s1比s2要大那么会返回正数,小的话返回负数,相等返回0 。
与equals不同的是,equals返回的是boolean类型,而compareTo返回的是int类型。具体比较方式:
1. 先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值。
2. 如果前k个字符相等(k为两个字符长度最小值),返回值两个字符串长度差值。
int compareToIgnoreCase(String str) 方法:与compareTo方式相同,但是忽略大小写比较:
public static void main(String[] args) {
String s1 = new String("abc");
String s2 = new String("ac");
String s3 = new String("ABc");
String s4 = new String("abcdef");
System.out.println(s1.compareToIgnoreCase(s2)); // 不同输出字符差值-1
System.out.println(s1.compareToIgnoreCase(s3)); // 相同输出 0
System.out.println(s1.compareToIgnoreCase(s4)); // 前k个字符完全相同,输出长度差值 -3
}
字符串查找
int.charAt(int index):按照index位置上的字符来输出。要注意是否越界。
int.indexOf(ch):默认从0下标开始找,返回ch第一次出现的位置,没有返回-1。
int indexOf(int ch, int fromIndex):从fromIndex位置开始找ch第一次出现的位置,没有返回-1。
int lastIndexOf(int ch):从后往前找,返回ch第一次出现的位置,没有返回-1。
int lastIndexOf(int ch, int fromIndex):从fromIndex位置开始往前找ch第一次出现的位置,没有返 回-1。
public static void main(String[] args) {
String s = "aaabbbcccaaabbbccc";
System.out.println(s.charAt(3)); // 'b'
System.out.println(s.indexOf('c')); // 6
System.out.println(s.indexOf('c', 10)); // 从10位置开始找,输出结果:15
System.out.println(s.indexOf("bbb")); // 3
System.out.println(s.indexOf("bbb", 10)); // 12
System.out.println(s.lastIndexOf('c')); // 17
System.out.println(s.lastIndexOf('c', 10)); // 8
System.out.println(s.lastIndexOf("bbb")); // 12
System.out.println(s.lastIndexOf("bbb", 10)); // 3
public static void main(String[] args) {
字符串转化
涉及到String类型的转换并不是在原来的字符串本身上进行改变,而是产生一个新的对象。
1.字符串与数字转换:
下面是将数字转化为字符串和将字符串转化为数字,需要用到String.valueOf()和Integer.parseInt():
public static void main(String[] args) {
class Student{
publice name;
pubilce int age;
publce Student(String name,int age){
this.name = name;
this.age = age;
}
}
// 数字转字符串
String s1 = String.valueOf(1234);
String s2 = String.valueOf(12.34);
String s3 = String.valueOf(true);
String s4 = String.valueOf(new Student("YanFeng", 18));
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.println("=================================");
// 字符串转数字
int data1 = Integer.parseInt("1234");
double data2 = Double.parseDouble("12.34");
System.out.println(data1);
System.out.println(data1 + 1);//证明已经转换成数字
System.out.println(data2);
}
上面数子转换对象中,s4的过程被称为序列化,把对象变成字符串,后面还有反序列化,把字符串变成对象。
分割线 下面对象转换字符时,不能有非数字字符。
2.大小写转换
toUpperCase()可以转换大小写。
public static void main(String[] args) {
String s1 = "hello";
String s2 = "HELLO";
// 小写转大写
System.out.println(s1.toUpperCase());
// 大写转小写
System.out.println(s2.toLowerCase());
}
toUpperCase()将字符全部转换为大写,toLowerCase()将全部转换为小写。
3.字符串转数组
.toCharArray()可以将字符串转化为数组:
public static void main(String[] args) {
String s = "hello";
// 字符串转数组
char[] ch = s.toCharArray();
for (int i = 0; i < ch.length; i++) {
System.out.print(ch[i]);
}
System.out.println();
// 数组转字符串
String s2 = new String(ch);
System.out.println(s2);
}
4.格式化
将括号里面的以“”里面的格式储存:
public static void main(String[] args) {
String s = String.format("%d-%d-%d", 2019, 9,14);
System.out.println(s);
}
字符串替换
replace(' ',' '),将前面字符全部替换成后面字符。
replace(" "," "),将前面字符串全部替换成后面字符串。
public static void main(String[] args) {
String s = "ababcabcd";
String str = s.replace('a','k');
System.out.println (str);
str = s.replace("a","ks");
System.out.println (str);
}
String replaceFirst(String regex, String replacement) 替换首个出现的内容:
public static void main(String[] args) {
String s = "ababcabcd";
String str = s.replaceFirst( "a","k");
System.out.println (str);
}
String replaceAll(String regex, String replacement) 替换所有的指定内容:
public static void main(String[] args) {
String s = "ababcabcd";
String str = s. replaceAll( "a","k");
System.out.println (str);
}
注意事项: 由于字符串是不可变对象,替换不修改当前字符串,而是产生一个新的字符串。
字符串拆分
String[] split(String regex) 将字符串全部拆分:
System.out.println();
String str = "hello world hello bit" ;
String[] result = str.split(" ") ; // 按照空格拆分
for(i = 0; i < strings.length; i++) {
System.out.println(s);
}
输出结果是:name zhangsan age 10
下面代码可以验证拆分以后他的返回值是一个数值:
System.out.println();
String str = "hello world hello bit" ;
String[] result = str.split(" ") ; // 按照空格拆分
for(int i = 0; i < result.length; i++) {
System.out.print(result[i]+" 0 ");
}
输出结果是:hello 0 world 0 hello 0 bit 0
特殊:拆分是特别常用的操作. 一定要重点掌握. 另外有些特殊字符作为分割符可能无法正确切分, 需要加上转义"\\":
String str = "192.168.1.1" ;
String[] result = str.split("\\.") ;
for(String s: result) {
System.out.println(s);
}
注意事项:
1. 字符"|","*","+"都得加上转义字符,前面加上 "\\" 。如同上面"\\."
2. 而如果是 "\" ,那么就得写成 "\\\\" 。
public static void main(String[] args) {
String s = "a\\bc\\d";\\ \\打印出来是\
//返回值 是数组
String[] strings = s.split("\\\\");
for (int i = 0; i < strings.length; i++) {
System.out.print(strings[i] + " ");
}
}
3. 如果一个字符串中有多个分隔符,可以用"|"作为连字符,就不用多次定义。
String s = "name=zhangsan&age=10";
//返回值 是数组
String[] strings = s.split("&|=");
for (int i = 0; i < strings.length; i++) {
System.out.print(strings[i] + " ");
}
字符串的截取
是从一个完整的字符串之中截取出部分内容,
1.String substring(int beginIndex),从指定位置截取到结尾。
2.String substring(int beginIndex, int endIndex),截取指定位置。
public static void main(String[] args) {
String str = "helloworld" ;
System.out.println(str.substring(5));//结果:hello
System.out.println(str.substring(0, 5));//结果:world
}
ps:取值范围一般都是左闭右开。
其他方法
String trim():去掉字符串中的左右空格,保留中间空格。
public static void main(String[] args) {
String str = " hello world " ;
System.out.println("["+str+"]");
System.out.println("["+str.trim()+"]");
}
//结果:[ hello world ]
// [hello world]
String toUpperCase():字符串转大写;
String toLowerCase():字符串转小写:
public static void main(String[] args) {
String str = " hello%$$%@#$%world 哈哈哈 " ;
System.out.println(str.toUpperCase());
System.out.println(str.toLowerCase());
}
//HELLO%$$%@#$%WORLD 哈哈哈
//hello%$$%@#$%world 哈哈哈
ps:只会转化字母大小写。
字符串的不可变性
遇到直接通过String调用的都是静态方法;
1. String类被final修饰,表明该类不能被继承
2. value被修饰被final修饰,表明value自身的值不能改变,即不能引用其它字符数组,但是其引用空间中 的内容可以修改。
ps:final修饰类表明该类不想被继承,final修饰引用类型表明该引用变量不能引用其他对象,但是其引用对象中的内 容是可以修改的,所有涉及到可能修改字符串内容的操作都是创建一个新对象,改变的是新对象。
字符串修改
提醒:尽量避免直接对String类型对象进行修改,因为String类是不能修改的,所有的修改都会创建新对象,效率 非常低下。
public static void main(String[] args) {
String s = "hello";
s += " world";
System.out.println(s); // 输出:hello world
}
并不推荐这种方法,中间用的临时变量很多。
尽量避免对String的直接需要,如果要修改建议尽量 使用StringBuilder。String和StringBuilder最大的区别在于String的内容无法修改,而StringBuilder的内容可 以修改。频繁修改字符串的情况考虑使用StringBuilder。:
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("hello");
stringBuilder.append(" world");
String ret = stringBuilder.toString();
System.out.println(ret);
}
注意:String和StringBuilder类不能直接转换。如果要想互相转换,可以采用如下原则:
1.String变为StringBuilder: 利用StringBuilder的构造方法或append()方法
2.StringBuilder变为String: 调用toString()方法。