12. 字符串
String s1 = "abc";
String s2 = new String("abc");
s1在内存中有一个对象;s2在内存中有两个对象。
public class Demo{
public static void main(String[] args) {
String s1 = "abc";
String s2 = new String("abc");
String s3 = "abc"; System.out.println(s1 == s2);
System.out.println(s1 == s3);
}
}
输出结果 false true
对字符串的常见操作:
(1)获取:int length() 获取长度
char charAt(int index) 根据位置获取位置上的某个字符
int indexOf(int ch) 返回的是ch在字符串中第一次出现的位置
int indexOf(int ch, int fromIndex) 从fromIndex指定位置开始,获取ch在字符串中出现的位置
int indexOf(String str)
int indexOf(String ch, int fromIndex)
int lastIndexOf(int ch) ...
(2)判断:
boolean contains(str) 字符串中是否包含某一个子串;也可以indexOf(str) 索引str第一次出现的位置,如果返回-1,表示str不在字符串中存在。所以也可以用于对指定字符串判断是否包含。
boolean isEmpty() 字符串中是否有内容
boolean startWith(str) 字符串是否是以指定内容开头
boolean endsWith(str) 字符串是否是以指定内容结尾
boolean equals(str) 判断字符串内容是否相同。复写了Object类中的equals方法。
boolean equalsIgnoreCase() 判断内容是否相同,忽略大小写
(3)转换:
将字符数组转成字符串
构造函数:String(char[])
String(char[], offset, count) 将字符数组中的一部分转成字符串
静态方法:static String copyValueOf(char[])
static String copyValueOf(char[], offset, count)
static String valueOf(char[])
将字节数组转成字符串
String(byte[])
String(byte[], offset, count) 将字节数组中的一部分转成字符
将字符串转成字符数组
char[] toCharArray()
将字符串转成字节数组
byte[] getBytes()
将基本数据类型转成字符串
static String valueOf(int) static String valueOf(double)
注:字符串和字节数组在转换过程中,是可以指定编码表的。
(4)替换:
String replace(oldchar, newchar)
(5)切割:
String[] split(regex)
(6)子串:获取字符串中的一部分
String subString(begin) 从指定位置到结尾。如果角标不存在,会出现字符串角标异常
String subString(begin, end) 包含头,不包含尾
(7)转换,去除空格,比较:
将字符串转成大写或小写: String toUpperCase() String toLowerCase()
将字符串两端的空格去除: String trim()
对两个字符串进行自然顺序的比较: int compareTo(String) 如果参数字符串等于此字符串,则返回0;小于的话返回一个负数;大于返回一个正数。
字符串操作 例:
(1)模拟一个trim方法,去除字符串两端的空格。
思路:判断字符串第一个位置是否是空格,如果是,继续向下判断,直到不是空格为止。结尾处判断空格也是如此;当开始和结尾都判断到不是空格时,就是要获取的字符串。
public class Demo{
public static void main(String[] args) {
String s = myTrim(" abc de ");
System.out.println(s);
} public static String myTrim(String str){
int start = 0;
int end = str.length() - 1;
while(start <= end && str.charAt(start++) == ' ');
while(start <= end && str.charAt(end--) == ' ');
str = str.substring(start - 1, end + 2);
return str;
}
}
(2)将一个字符串进行反转。将字符串中指定部分进行反转,“abcdefg”;“abfedcg”
思路:将字符串变成数组,对数组进行反转;将反转后的数组变回字符串;只要将反转的部分的开始和结束的位置作为参数传递即可。
public class Demo{
public static void main(String[] args) {
String s = myReverse("abcdefg", 2, 6);
System.out.println(s);
} public static String myReverse(String str, int start, int end){
char[] arr = str.toCharArray(); reverse(arr, start, end); return new String(arr);
} public static void reverse(char[] arr, int start, int end){
for(int i = start, j = end - 1; i < j; i++, j--){
swap(arr, i, j);
}
}
public static void swap(char[] arr, int i, int j){
char tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
(3)获取一个字符串在另一个字符串中出现的次数。“abkkcdkkefkkskk”
思路:定义一个计数器;获取kk第一次出现的位置;从第一次出现位置后剩余的字符串中继续获取kk出现的位置,每获取一次就计数一次;当获取不到时,计数完成。
public class Demo{
public static void main(String[] args) {
int num = getSubCount("abkkcdkkefkkskk", "kk");
System.out.println(num);
}
public static int getSubCount(String str, String key){
int count = 0;
int index = 0;
while((index = str.indexOf(key)) != -1){
str = str.substring(index + key.length());
count++;
}
return count;
}
}
或者:
public class Demo{
public static void main(String[] args) {
int num = getSubCount("abkkcdkkefkkskk", "kk");
System.out.println(num);
}
public static int getSubCount(String str, String key){
int count = 0;
int index = 0;
while((index = str.indexOf(key, index)) != -1){
index += key.length();
count++;
}
return count;
}
}
(4)获取两个字符串中最大相同子串。
思路:将短的那个子串按照长度递减的方式获取到;将每获取到的子串,去长串中判断是否包含。如果包含,已经找到。
public class Demo{
public static void main(String[] args) {
String s1 = "abcwerthelloyuiodef";
String s2 = "cvhellobnm";
String s = getMaxSubString(s1, s2);
System.out.println(s);
} public static String getMaxSubString(String s1, String s2){
String max = "", min = "";
max = (s1.length() > s2.length()) ? s1 : s2;
min = (max == s1) ? s2 : s1; for(int x = 0; x < min.length(); x++){
for(int y = 0, z = min.length() - x; z != min.length() + 1; y++, z++){
String tmp = min.substring(y, z);
if(max.contains(tmp)){
return tmp;
}
// if(max.indexOf(tmp) != -1){
// return tmp;
// }
}
}
return null;
}
}
StringBuffer是字符串缓冲区,是一个容器,长度可变化,并可以操作多个数据类型,最终会通过toString方法变成字符串。
(1)存储: StringBuffer append() 将指定数据作为参数添加到已有数据的结尾处
StringBuffer insert(index, 数据) 可以将数据插入到指定index位置
(2)删除:StringBuffer delete(start, end) 删除缓冲区中的数据,包含start,不包含end
StringBuffer deleteCharAt(index) 删除指定位置的字符
对象.delete(0, 对象.length()); 清空缓冲区
(3)获取:char charAt(int index)
int indexOf(String str)
int lastIndexOf(String str)
int length()
String substring(int start, int end)
(4)修改:StringBuffer replace(start, end, String)
void setCharAt(int index, char ch)
(5)反转:reverse()
(6)void getChar s(int srcBegin, int srcEnd, char[] dst, int dstBegin)
StringBuffer线程同步,StringBuilder线程不同步
注:
public class Demo{
public static void main(String[] args) {
Integer a = 127;
Integer b = 127;
System.out.println(a == b); Integer m = 128;
Integer n = 128;
System.out.println(m == n);
}
}
输出结果:true false
因为a和b指向了同一个Integer对象。当数值在byte范围内(-128~127),如果该数值已经存在,则不会重新开辟新空间。