String类
- String是系统完成的一个类,String 类代表字符串
构造方法
1. public String() :空构造
2. public String(byte[] bytes)
3. public String(byte[] bytes,int offset,int length)
4. public String(char[] value)
5. public String(char[] value,int offset,int count)
6. public String(String original)
方法
1. 判断功能
boolean equals()
boolean equalsIgnoreCase()
boolean isEmpty()
boolean startsWith()
boolean endsWith()
boolean contains()
String s1 = "Hello";
String s2 = "HELLO";
String s3 = "Hello";
String s4 = "world";
System.out.println(s1.equals(s2)); //false
System.out.println(s1.equals(s3)); //true
System.out.println(s1.equals(s4)); //false
System.out.println(s1.equalsIgnoreCase(s2)); //true
System.out.println(s1.equalsIgnoreCase(s3)); //true
System.out.println(s1.equalsIgnoreCase(s4)); //false
System.out.println(s1.startsWith("He")); //true
System.out.println(s1.startsWith("he")); //false
System.out.println(s1.startsWith("HE")); //false
System.out.println(s1.endsWith("lo")); //true
System.out.println(s1.endsWith("Lo")); //false
System.out.println(s1.endsWith("LO")); //false
面试题:String s = new String(“Hello”);和String s = “Hello”;的区别?
String s = new String(“Hello”)该语句,会首先在栈中创建一个String类型的引用s。之后在堆中的常量池创建一个字符串“Hello”,之后在堆中创建一个变量,将“Hello”拷贝进来。之后将引用s的地址设为刚刚创建的对象的地址。
String s = "Hello“;会将s变量的引用直接设置为常量池中"Hello"的地址。
如果重复创建呢?
String s1 = new String("Hello");
String s2 = new String("Hello");
String s3 = new String("Hello");
会创建四个对象,常量池中一个。堆中3个。
栈中三个。
String s1 = "Hello“;
String s2 = "Hello“;
String s3 = "Hello“;
会创建一个对象,常量池中一个
栈中三个。
2. 获取功能
int length()
获取String中的字符个数。
char charAt(int index)
获取指定索引处的字符
int indexOf(int ch) //获取字符在字符数组中位置
int indexOf(String str) //获取字符串在字符数组中位置
int indexOf(int ch,int fromIndex)
int indexOf(String str,int fromIndex)
String substring(int start) //截取到末尾
String substring(int start,int end)
String s = "好好学习!";
String s2 = "abcd";
System.out.println(s.length()); //5
System.out.println(s.charAt(2));//学
System.out.println(s.indexOf('好')); //0
System.out.println(s.indexOf('好', 1)); //1
System.out.println(s2.indexOf(97)); //0 a的ANSII码为97
System.out.println(s2.substring(1)); //从index=1开始截取,直至字符串尾 bcd
System.out.println(s2.substring(1,3)); //从1开始,截取到2,[1,3)包左不包右 bc
除了indexOf()之外,还有lastIndexOf() 用法与indexOf一致,不过获取到的位置是最后出现位置
3. 转化功能
byte[] getBytes()
将字符串转化为byte[]返回
Arrays.toString(bytes) 此方法将数组转化为字符串型输出(int型)
new String(bytes) 此方法将数组转化为字符串型输出(字符型)
char[] toCharArray()
将字符串转化为char[]返回
static String valueOf(char[] chs)
静态方法:String.valueOf(),将字符串类型转化为String类型。但是如果是空指针类型会报空指针异常
static String valueOf(int i)
String toLowerCase()
将字符串数组转化为小写返回
String toUpperCase()
将字符串数组转化为大写返回
String concat(String str)
拼接两个字符串
String s1 = "abcdefg";
String s2 = "dfFAlkFdEr";
byte[] bytes = s1.getBytes();
char[] chars = s1.toCharArray();
System.out.println(bytes); //[B@6d6f6e28
System.out.println(Arrays.toString(bytes)); //[97, 98, 99, 100, 101, 102, 103]
System.out.println(new String(bytes)); //abcdefg
System.out.println(chars); //println重写了字符串的输出方法,直接输出字符串
System.out.println(String.valueOf(chars)); //abcdefg
System.out.println(String.valueOf(bytes)); //
//String toLowerCase()
//String toUpperCase()
System.out.println(s2.toLowerCase()); //dffalkfder
System.out.println(s2.toUpperCase()); //DFFALKFDER
valueOf的源码: return new String(data);
4. 替换功能
String replace(char old,char new)
用新字符new替换旧字符old
String replace(String old,String new)
用新字符串替换旧字符串
String trim()
修剪字符串两端的空白
int compareTo(String str)
将字符串与str按字典序比较
int compareToIgnoreCase(String str)
将字符串与str忽略大小写比较
String s = "我叫周兵,我叫周兵,我叫周兵";
String s2 = " hello, world haha ";
String s3 = "abcd";
System.out.println(s.replace('我','你')); //你叫周兵,你叫周兵,你叫周兵
System.out.println(s.replace("我叫","你是")); //你是周兵,你是周兵,你是周兵
System.out.println("***" + s2.trim() + "***"); //***hello, world haha***
System.out.println(s3.compareTo("abcd")); //0
System.out.println(s3.compareTo("Abcd")); //32
System.out.println(s3.compareTo("abc")); //1
compareTo的源码:
public int compareTo(String anotherString) {
int len1 = value.length; //原数组的长度
int len2 = anotherString.value.length; //比较数组的长度
int lim = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;
int k = 0;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
return len1 - len2;
}