String常用方法
// 细节:考点
private static void method_9() {
String s1 = "hello";
String s2 = "world";
String s3 = s1 + s2;
//字符串,变量链接时,会在常量池,先开辟空间,然后链接
//常量链接时,会在常量池中先找,有直接用,没有,开辟空间
System.out.println(s3 == (s1+s2));//false
System.out.println(s3 == ("hello"+"world"));
System.out.println("helloworld" == ("hello"+"world"));//true
}
// 切割
private static void method_8() {
String str = "周润发.周星驰.周杰伦";
String[] data = str.split("\\.");
for(String s : data)
System.out.println(s);
}
// 转换
private static void method_7() throws Exception {
String str = " a bcDeF ";
//以下方法形成的都是新的字符串
System.out.println("concat(xxx)-->"+str.concat("xxx"));
System.out.println("replace(b,u)-->"+str.replace('b','u'));
System.out.println("toLowerCase()-->"+str.toLowerCase());
System.out.println("toUpperCase()-->"+str.toUpperCase());
System.out.println("trim()-->"+str.trim()+"-----");
// ---重点--------------
/*String data = "中国";
// ---gbk----
// 中 -42 -48
// 国 -71 -6
// ---utf-8----
// 中 -28 -72 -83
// 国 -27 -101 -67
byte[] bys = data.getBytes("utf-8");
for(byte b : bys)
System.out.println(b);*/
}
// 获取
private static void method_6() {
String str = "abcdecbmn";
System.out.println("charAt(3)-->"+str.charAt(3));//'d'
System.out.println("indexOf(i)-->"+str.indexOf('i'));//-1,代表没有
System.out.println("indexOf(c,3)-->"+str.indexOf('c',3));//5
System.out.println("lastIndexOf(c)-->"+str.lastIndexOf('c'));//5
System.out.println("length()-->"+str.length());//9
System.out.println("substring(1, 4)-->"+str.substring(1, 4));//bcd
System.out.println("substring(0, length)-->"+str.substring(0, str.length()));//bcd
}
// 判断
private static void method_5() {
String str = " B dcM O ";
System.out.println(str.contains("B"));//true
System.out.println(str.equalsIgnoreCase(" B DCM O "));//true
System.out.println(str.isEmpty());//false
System.out.println(str.startsWith(" "));//true
System.out.println(str.endsWith(" "));//true
}
// char[]转成String
private static void method_4() {
char[] data = {'x','p','q'};
String str = new String(data,1,2);
System.out.println(str);
}
// byte[]--->String
private static void method_3() {
byte[] data = {97,49,98};
String str = new String(data);
System.out.println(str);
}
// 字符串常量和字符串构造器对象对比
private static void method_2() {
String str = "abc";
String str2 = new String("abc");
String str3 = "abc";
System.out.println(str==str2);//false
System.out.println(str.equals(str2));//true
System.out.println(str == str3);//true
}
// 字符串一经初始化就不会改变,改的是引用指向。
private static void method_1() {
String str = "abc";
str = "xxx";
}
String、StringBuffer 和 StringBuilder 的区别
String:字符串常量,字符串长度不可变。Java中String 是immutable(不可变)的。用于存放字符的数组被声明为final的,因此只能赋值一次,不可再更改。
StringBuffer:字符串变量(Synchronized,即线程安全)。如果要频繁对字符串内容进行修改,出于效率考虑最好使用 StringBuffer,如果想转成 String 类型,可以调用 StringBuffer 的 toString() 方法。Java.lang.StringBuffer 线程安全的可变字符序列。在任意时间点上它都包含某种特定的字符序列,但通过某些方法调用可以改变该序列的长度和内容。可将字符串缓冲区安全地用于多个线程。
StringBuilder:字符串变量(非线程安全)。在内部 StringBuilder 对象被当作是一个包含字符序列的变长数组。
基本原则:
- 如果要操作少量的数据用 String ;
- 单线程操作大量数据用StringBuilder ;
- 多线程操作大量数据,用StringBuffer。
String,描述字符串java.lang.String,有索引:从0开始
字符串是常量;它们的值在创建之后不能更改
字符串常量在方法区的常量池中。
----byte[]转成String----------offset:索引-----length:个数----
new String(byte[] bytes)
new String(byte[] bytes, String charsetName)
new String(byte[] bytes, int offset, int length)
new String(byte[] bytes, int offset, int length, String charsetName)
----char[]转成String-------------------
new String(char[] value)
new String(char[] value, int offset, int count)
new String(StringBuffer buffer)
new String(StringBuilder builder)
A.判断
boolean contains(CharSequence s) 判断是否包含指定字符串
CharSequence是String父接口,可以接收String对象--多态
boolean equalsIgnoreCase(String anotherString) 忽略大小写的equals
boolean isEmpty() 判断是否是空
boolean startsWith(String prefix) 是否以prefix开头
boolean endsWith(String suffix) 是否以suffix结尾
int compareTo(String anotherString)
B:获取
int length() 返回此字符串的长度。
char charAt(int index) 返回指定索引处的 char 值。
根据字符/字符串查找对应的索引位,从0索引位开始向后查找
int indexOf(int ch)
int indexOf(int ch, int fromIndex)
int indexOf(String str)
int indexOf(String str, int fromIndex)
根据字符/字符串查找对应的索引位,从最大索引向前查找
int lastIndexOf(int ch)
int lastIndexOf(int ch, int fromIndex)
int lastIndexOf(String str)
int lastIndexOf(String str, int fromIndex)
获取子串,从一个字符串中取一部分出来
String substring(int beginIndex)
String substring(int beginIndex, int endIndex) 左闭右开
C:转换
String concat(String str) 连接字符串
---String转成byte[]------
byte[] getBytes()
byte[] getBytes(String charsetName)
--替换---
String replace(char oldChar, char newChar) oldChar-->newChar
String replace(String target, String replacement) target-->replacement
char[] toCharArray() String转成字符串数组
String toLowerCase() 转小写
String toUpperCase() 转大写
StringBuilder JDK1.5 线程不安全的。效率高
字符串缓冲区对象-->可变字符序列
构造方法摘要
new StringBuffer()
new StringBuffer(int capacity)
new StringBuffer(String str)
方法摘要:
添加 :
StringBuffer append(xxx b)
删除:
StringBuffer delete(int start, int end) 左闭右开
StringBuffer deleteCharAt(int index)
插入:
StringBuffer insert(int offset, xxx b)
在offset索引处插入b,原数据向后顺延
修改
void setCharAt(int index, char ch)
将index对应的字符换成ch
其他方法
int capacity() 获取 容量
int length() 数据个数
StringBuffer reverse() 反转数据