String类的常用方法
上一次大致说了以下String类的定义,以及创建对象在JVM的演变过程,这次我们来说一些String常用的方法!
字符串分割
String[ ] split(String regex); 使用正则表达式拆分字符串,返回字符串数组
String[] strs = "2020-4-4".split("-");
System.out.println(Arrays.toString(strs)); // [2020, 4, 4]
String[] strs1 = "name=lee&age=25&gender=male".split("&");
System.out.println(Arrays.toString(strs1)); // [name=lee, age=25, gender=male]
判断字符串是否相等
boolean equals(String a); 判断两个字符串是否相等,返回布尔类型结果
boolean equalsIgnoreCase(String a); 判断两个字符串是否相等并且忽略大小写
System.out.println("abcdef".equals("abcdef")); // true
System.out.println("abcdef".equals("Abcdef")); // false
System.out.println("abcdef".equalsIgnoreCase("Abcdef")); // true
String a = "abc";
String b = "abc";
System.out.println(a.equals(b)); // true
System.out.println(a==b); // true
String s1 = new String("abc");
String s2 = new String("abc");
System.out.println(s1.equals(s2)); // true
System.out.println(s1 == s2); // false, == 比较的是内存地址
System.out.println(a==s1); // false
System.out.println(a.equals(s1)); // true
截取字符串
String substring(int beginIndex); 根据指定开始索引截取字符串;
String substring(int beginIndex, int endIndex); 根据指定起始索引截取字符串;
注意:开始索引可以取到,结束索引取不到(也就是前闭后开)
System.out.println("http://www.baidu.com".substring(7)); // www.baidu.com
System.out.println("http://www.baidu.com".substring(11,15)); // baid
System.out.println("http://www.baidu.com".substring(11,16)); // baidu
去除字符串前后空白
String trim(); 去除字符串前后空白,仅前后,字符串中间带有空格,不能去除
System.out.println(" Hello ,java!".trim()); //Hello ,java!
System.out.println(" Hello ,java! ".trim()); //Hello ,java!
获取字符串长度
int length(); 返回int类型的字符串长度
注意:在判断数组长度和字符床长度是不一样的
数组长度是length属性,而字符串长度是length()方法!!
System.out.println("abcd".length()); // 4
将非字符串转换成字符串
String valueOf(xxx);
注意: 如果valueOf()的参数是一个对象到时候,会自动调用对象的toString()方法
例如:
String s1 = String.valueOf(new Customer());
System.out.println(s1); // 如果Customer类没有重写toString()方法,则输出的是对象的内存地址
使用valueOf:
System.out.println(String.valueOf(3.14)); // "3.14"
System.out.println(String.valueOf(true)); // "true"
System.out.println(String.valueOf(1000)); // "1000"
字符串替换
String replace(CharSequence target, CharSequence replacement);
使用replacement替换字符串中的target得到新字符串
System.out.println("http://www.baidu.com".replace("http:","https:")); // https://www.baidu.com
System.out.println("name=lee&age=25&gender=male".replace("=",":")); // name:lee&age:25&gender:male
将字符串转换成char数组
char[ ] toCharArray();
char[] chars = "我爱学Java".toCharArray();
System.out.println(Arrays.toString(chars)); // [我, 爱, 学, J, a, v, a]
转换大小写
String toLowerCase(); 将字符串全部转换为小写
String toUpperCase(); 将字符串全部转换为大写
System.out.println("fasVD132$#fd_=safwEFW".toUpperCase()); //FASVD132$#FD_=SAFWEFW
System.out.println("FwGEfeEWR3 4r3FDfsf".toLowerCase()); //fwgefeewr3 4r3fdfsf
是否包含某个子字符串
boolean contains(CharSequence s); 检查字符串中是否包含子字符串,返回布尔类型结果
System.out.print("HelloWorld,Java".contains("World")) // true
System.out.print("HelloWorld,Java".contains("world")) // false,注意大小写
是否以XXX开口或结尾
boolean startsWith(String a); 判断当前字符串是否以某个字符串开头,返回布尔类型结果
boolean endsWith(String a); 判断当前字符串是否以某个字符串结尾,返回布尔类型结果
System.out.println("helloWorld.java".startsWith("hello")); // true
System.out.println("helloWorld.java".startsWith("ello")); // false
System.out.println("helloWorld.java".startsWith("World",5)); //true
// 参数中可以加入int类型的索引值,即从第几个开始时World开始的
System.out.println("helloWorld.java".endsWith(".java")); //true
System.out.println("Hello,Kitty!".endsWith("Kit")); // false
字符串转成字节数组
byte[ ] getBytes( );
byte[] b = "abcd".getBytes(); // [ 97, 98, 99, 100]
int indexOf(String str);
判断某个字符串在当前字符串中第一次出现的位置索引,如果找不到则返回-1
System.out.println("javapythonc++gophp".indexOf("python")); // 4
System.out.println("javapythonc++gophp".indexOf("java")); // 0
System.out.println("javapythonc++gophp".indexOf("pthon")); // -1
int lastIndexOf(String str);
判断某个字符串在当前字符串中最后一次出现的位置索引,如果找不到则返回-1
System.out.println("javac++php".lastIndexOf("p")); // 9
System.out.println("javac++php".lastIndexOf("ph")); // 7
System.out.println("javac++php".lastIndexOf("c++")); // 4
是否为空
boolean isEmpty( ); 判断某个字符串是否为空
注意:为空不是为null,如果为null调用了isEmpty()方法,会报空指针异常
实质上底层时调用字符串的length方法,根据长度判断是否为空
public boolean isEmpty() {
return value.length == 0;
}
举例:
String a = "";
System.out.println(a.isEmpty()); // true
String b = "java";
System.out.println(b.isEmpty()); // false
System.out.println(" ".isEmpty()); // false,注意空格也不是空
System.out.println("hello".isEmpty()); // false
String c = null;
System.out.println(c.isEmpty()); // Exception in thread "main" java.lang.NullPointerException