String:不可变的序列,底层是byte[],引用数据类型,适合少量字符串操作
构造方法
1、 String():字节数组byte
byte[] b = {98,97,99};//一个字节数组
String str = new String(b);//字节数组转字符串
System.out.println(str);
2、 String():字符数组char
char[] c = {'a','b','c'};
String s = new String(c);
System.out.println(s);
比较
字符串比较:equals()
public class HelloWorld {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
char[] c = {'H','e','l','l','o'};
String str = new String(c);
System.out.println(str.equals(str1));
System.out.println(str.equals(str2));
}
}
查询
1、 字符串长度:length()
package com.llhc;
class Hello {
public static void main(String[] args) {
String str = "hello";
System.out.println("字符串长度:"+str.length());
}
}
2、字符串拼接:contcat()
public class HelloWorld {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
String str3 = str1.concat(str2);
System.out.println(str3);
}
}
3、 获取单个或全部字符:charAt()
package com.llhc;
class Hello {
public static void main(String[] args) {
String str = "hello";
for(int i=0;i<str.length();i++){
System.out.println(str.charAt(i));
}
}
}
4、 内容第一次出现:indexOf()
package exercise;
class exercise {
public static void main(String[] args) {
String str = "hello hello";
System.out.println(str.indexOf("ello"));
}
}
截取
自定义截取:substring
public class HelloWorld {
public static void main(String[] args) {
String str = "hello hello";
System.out.println(str.substring(2,6));
}
}
转换
1、String转'字符数组':toCharArray()
public class HelloWorld {
public static void main(String[] args) {
String str = "hello hello";
char[] c=str.toCharArray();
for(int i=0;i<c.length;i++){
System.out.println(c[i]);
}
}
}
2、String转'字节数组':getBytes()
public class HelloWorld {
public static void main(String[] args) {
String str = "hello hello";
byte[] c=str.getBytes();
for(int i=0;i<c.length;i++){
System.out.println(c[i]);
}
}
}
3、替换:replace()
public class HelloWorld {
public static void main(String[] args) {
String str = "hello hello";
String str1=str.replace("h","w");
System.out.println(str1);
}
}
分割
字符串分割:split()
public class HelloWorld {
public static void main(String[] args) {
String str = "hello,hello,world";
String[] str1 = str.split(",");
for (int i=0;i<str1.length;i++){
System.out.println(str1[i]);
}
}
}
其他方法
1、trim():去除字符串前后的空格
package exercise;
class exercise {
public static void main(String[] args) {
String str = " hello world ";
String str1 = str.trim();
System.out.println(str1);
}
}
2、toUpperCase():转大写
package exercise;
class exercise {
public static void main(String[] args) {
String str = " hello world ";
String str1 = str.toUpperCase();
System.out.println(str1);
}
}
3、toLowerCase():转小写
package exercise;
class exercise {
public static void main(String[] args) {
String str = "HELLD WORLD";
String str1 = str.toLowerCase();
System.out.println(str1);
}
}
StringBuffer+StringBuilder
1、空参构造
2、添加:append()
3、转换:toString()
public class HelloWorld {
public static void main(String[] args) {
//空参构造方法、默认16个字符
StringBuilder sb = new StringBuilder();
System.out.println(sb);
//添加
sb.append("1");
sb.append("2");
sb.append(3.14);
sb.append(true);
System.out.println(sb);
//转换(String----->StringBuilder)
String s = "中国";
StringBuilder sb1=new StringBuilder(s);
System.out.println("String转StringBuilder:"+sb1);
//(StringBuilder---->String)
String s2=sb1.toString();
System.out.println("StringBuilder转String:"+s2);
}
}
字符串总结:
1、什么情况下使用[String,StringBuilder,StringBuffer三者的区别](https://www.cnblogs.com/su-feng/p/6659064.html)
String:是一个不可变的字符序列,适用于少量字符串操作的情况。(String
对象一旦创建,值不可更改)
StringBuilder:是可变的字符序列,是线程不安全的,适用于单线程下大量
字符串操作的情况。(值可以更改)
StringBuffer:是可变的字符序列,是线程安全的,适用于多线程下大量字符
串操作的情况。(值可以更改)
2、现实开发中优先使用法则:
StringBuilder>StringBuffer>String