String类
1. String类
构造方法:
import java.util.Arrays;
public class StringDemo01 {
public static void main(String[] args) throws UnsupportedEncodingException {
String str = "";
//String() 初始化新创建的String对象,使其表示空字符序列。
String str1 = new String();
System.out.println(str == str1); //false
System.out.println(str.equals(str1)); //true
//String(byte[] bytes) 通过使用平台的默认字符集解码指定的字节数组构造新的String 。
// String(byte[] bytes, String charsetName) 构造一个新的String由指定用指定的字节的数组解码charset 。
String s = "中国";
byte[] arr = s.getBytes("GBK");
System.out.println(arr.length);
System.out.println(Arrays.toString(arr));
//String str2 = new String(arr);
String str2 = new String(arr,"gbk");
System.out.println(str2);
//String(byte[] bytes, int offset, int length)
//String(byte[] bytes, int offset, int length, String charsetName)
String str3 = new String(arr,2,2,"gbk");
System.out.println(str3);
//String(char[] value) 分配新的 String ,使其表示当前包含在字符数组参数中的字符序列。
//String(char[] value, int offset, int count)
char[] arr2 = {'a','b','c','d','e'};
//String str4 = new String(arr2);
String str4 = new String(arr2,1,3);
System.out.println(str4);
//String(String original)
String str5 = new String("abc"); //创建了2个对象 一个new在堆中 一个"abc"在字符串常量池中
System.out.println(str5);
}
}
常用方法:
import java.util.Arrays;
public class StringDemo02 {
public static void main(String[] args) {
String str = "wodejiejiezhenbang";
String str1 = "Wodejiejiezhenbang";
//char charAt(int index) 返回指定索引处的 char值。
System.out.println("charAt:"+ str.charAt(5));
//int codePointAt(int index) 返回指定索引处的字符(Unicode代码点)。
//int codePointBefore(int index) 返回指定索引之前的字符(Unicode代码点)。
System.out.println(str.indexOf("a"));
System.out.println("codePointAt:"+str.codePointAt(15));
System.out.println("codePointAt:"+str.codePointBefore(15));
//int compareTo(String anotherString) 按字典顺序比较两个字符串。
System.out.println("compareTo:"+str.compareTo(str1));
//int compareToIgnoreCase(String str) 按字典顺序比较两个字符串,忽略大小写差异。
System.out.println("compareToIgnoreCase:"+str.compareToIgnoreCase(str1));
//String concat(String str) 将指定的字符串连接到此字符串的末尾。
System.out.println("concat:"+str.concat(str1));
System.out.println(str);
//boolean contains(CharSequence s) 当且仅当此字符串包含指定的char值序列时,才返回true。
System.out.println("contains:"+str.contains("jiejie"));
//boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结尾。
//boolean startsWith(String prefix) 测试此字符串是否以指定的前缀开头。
System.out.println("startsWith:"+str.startsWith("wo"));
System.out.println("endsWith:"+str.endsWith("wo"));
//boolean equals(Object anObject) 将此字符串与指定的对象进行比较。
//boolean equalsIgnoreCase(String anotherString) 将此 String与另一个 String比较,忽略了大小写。
//byte[] getBytes()
//byte[] getBytes(String charsetName) 使用命名的字符集将此 String编码为字节序列,将结果存储到新的字节数组中。
//void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 将此字符串中的字符复制到目标字符数组中。
char[] arr = new char[10];
str.getChars(10,18,arr,1);
System.out.println(Arrays.toString(arr));
//int indexOf(String str) 返回指定子字符串第一次出现的字符串中的索引。
//int indexOf(String str, int fromIndex) 从指定的索引处开始,返回指定子字符串第一次出现的字符串中的索引。
System.out.println("indexOf:"+str.indexOf("e"));
System.out.println("indexOf:"+str.indexOf("e",4));
System.out.println("indexOf:"+str.lastIndexOf("e"));
//boolean isBlank() 如果字符串为空或仅包含 white space代码点,则返回 true ,否则 false 。
//boolean isEmpty() 返回 true ,当且仅当, length()是 0 。
System.out.println(" ".isEmpty());
System.out.println(" ".isBlank());
//int length() 返回此字符串的长度。
System.out.println("length:"+str.length());
//String repeat(int count) 返回一个字符串,其值为此字符串的串联重复 count次。
System.out.println("repeat:"+str.repeat(3));
//String replace(char oldChar, char newChar) 返回从替换所有出现的导致一个字符串 oldChar在此字符串 newChar 。
//String replace(CharSequence target, CharSequence replacement)
System.out.println("replace:"+str.replace('e','E'));
System.out.println(str);
//String[] split(String regex) 将此字符串拆分为给定 regular expression的匹配 项 。
System.out.println(Arrays.toString(str.split("e")));
//String trim() 去除前后空格 去除半角空格
System.out.println(" hahaha ".trim());
System.out.println(" hahaha ".trim());
System.out.println(" hahaha ".strip());
System.out.println(" hahaha ".strip());
//11新增String类中的方法
//String strip() 返回一个字符串,其值为此字符串,并删除了所有前导和尾随 white space 。
//String stripLeading() 返回一个字符串,其值为此字符串,并删除了所有前导 white space 。
//String stripTrailing() 返回一个字符串,其值为此字符串,并删除所有尾随 white space 。
//String substring(int beginIndex) 返回一个字符串,该字符串是此字符串的子字符串。
//String substring(int beginIndex, int endIndex) 返回一个字符串,该字符串是此字符串的子字符串。g
System.out.println("substring:"+str.substring(2));
System.out.println("substring:"+str.substring(2,5));
//char[] toCharArray() 将此字符串转换为新的字符数组。
System.out.println(Arrays.toString(str.toCharArray()));
//String toLowerCase() 转小写
//String toUpperCase() 转大写
System.out.println(str.toUpperCase());
//static String valueOf(boolean b) 系列静态方法,参数内容转为字符串
boolean flag = false;
System.out.println(String.valueOf(flag));
}
}
2. StringBuilder与StringBuffer类
StringBuilder,StringBuffer与String类的区别:
- String类是不可变的字符序列,而StringBuilder/StringBuffer类是可变的字符序列
- StringBuilder类使用效率高但线程不同步,线程不安全;StringBuffer类使用效率低但线程同步,线程安全
执行效率:StringBuilder>StringBuffer>String
构造方法:
public class StringDemo03 {
public static void main(String[] args) {
String s = "";
//StringBuilder() 构造一个字符串构建器,其中不包含任何字符,初始容量为16个字符。
StringBuilder sb = new StringBuilder();
System.out.println(sb);
System.out.println(sb.length()); //字符中真实字符的个数
System.out.println(sb.capacity()); //缓冲区容量
System.out.println(s.equals(sb));
//StringBuilder(int capacity) 构造一个字符串构建器,其中没有字符,并且具有 capacity参数指定的初始容量。
StringBuilder sb2 = new StringBuilder(10);
System.out.println(sb2.length());
System.out.println(sb2.capacity());
//StringBuilder(CharSequence seq) 构造一个字符串构建器,其中包含与指定的 CharSequence相同的字符。
//初始容量参数字符串长度+16
StringBuilder sb3 = new StringBuilder("abc");
System.out.println(sb3);
System.out.println(sb3.length()); //3
System.out.println(sb3.capacity()); //19
//String 与 StringBuilder ,StringBuffer 相互转换
//通过构造器
String str2 = new String(sb3);
}
}
常用方法:
public class StringDemo04 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(); //初始容量
//StringBuilder append(boolean b) 将 boolean参数的字符串表示形式追加到序列中。
sb.append("abc");
sb.append(false);
sb.append(123456);
sb.append("中国");
System.out.println(sb);
System.out.println(sb.length());
System.out.println(sb.capacity());
sb.append(123); //每次扩容: 原容量的2倍+2
System.out.println(sb);
System.out.println(sb.length());
System.out.println(sb.capacity());
//StringBuilder delete(int start, int end) 删除此序列的子字符串中的字符。 结束索引不包含
sb.delete(3,8);
System.out.println(sb);
//StringBuilder deleteCharAt(int index) 按此顺序删除指定位置的 char 。
sb.deleteCharAt(2);
System.out.println(sb);
//StringBuilder insert(int offset, Object obj) 将 Object参数的字符串表示形式插入此字符序列中。
sb.insert(5,"哈哈");
System.out.println(sb);
//StringBuilder reverse() 导致此字符序列被序列的反向替换。
sb.reverse();
System.out.println(sb);
System.out.println(sb.toString());
String s = "haha"+"hehe"; //几个字符串对象
//一个 编译的时候,常量会直接运算 String s = "hahahehe";
String a = "哈哈";
String b = "呵呵";
String c = a+b; //3个 "哈哈" "呵呵" "哈哈呵呵"
}
}