String类
字符串都是一个特殊的对象。字符串一旦被初始化就不可以被改变。
String类复写了object类中的equals方法,该方法用于判断字符串是否相同。
String和StringBuffer
String是一个特殊的对象,一旦被初始化,就不会被改变。
String s1="abc"; s1是一个类类型变量,“abc”是一个对象(初始化后不会改变)。
String s2=new String("abc");
s1和s2的区别:
s1在字符串常量池中创建了一个abc字符串
s2在堆中创建了两个对象一个是默认对象一个是字符串对象。
String中==和equals的区别
==比较的是地址,equals比较的是内容(因为String类复写了父类Object中的equals方法)。
1.获取
1.1 获取字符串长度(区分于数组的length,数组的length是属性,这里的length()是方法)
int length();
1.2 根据位置获取字符
char charAt(int index);
1.3 根据字符获取在字符中的位置
int indexof(int ch) 返回的是ch在字符串中第一个出现的位置
int indexof(int ch,int FromIndex) 从fromIndex指定位置开始,获取ch在字符串中出现的位置
int indexof(String str); 返回的是str在字符串中第一个出现的位置
int indexof(String str,int FromIndex) 从fromIndex指定位置开始,获取str在字符串中出现的位置
反响索引一个字符出现的位置。
int lastindexof(int ch) 返回的是ch在字符串中最后一次出现的位置
int lastindexof(int ch,int FromIndex) 返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
int lastindexof(String str); 返回指定子字符串在此字符串中最右边出现处的索引。
int lastindexof(String str,int FromIndex) 返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
1.4获取字符串中的一部分字符串,也叫子串。
String subString(int beginindex,intendindex) ;
String subString(int beginindex)
2.判断
2.1两个字符串是否相同
equals(Object obj)
equalsIgnoreCase(String str)<忽略大小写>
2.2字符串中是否包含某个字符串
contains(String str)
2.3两个字符串是否以指定字符串开头或结尾
boolean Startswith(String);//可以用来判断文件名
boolean endswith(String);//可以用来判断文件类型
2.4字符串是否为空
boolean isEmpty();
3.字符串转换
3.1将字符串变成字符串数组
String[] split(String regex);切割
3.2将字符串变成字符数组
char[] toCharArray();
3.1将字符串变成字节数组
byte[] getBytes();
3.4将字符串数组变成字符串
构造函数 String(char[])
String(char[],offset,count)将字符数组中的一部分转成字符串。
静态函数 static String copyValueof(char[])
static String copyValueof(char[],offset,count)将字符数组中的一部分转成字符串。
static String valueOf(int) 将基本数据类型转成字符串
static String valueOf(double)
3.5将字符串的字母大小写转换
String toUppercase();大写
String toLowercase();小写
3.6将字符串的内容替换
String repalce(char oldch,char newch);
String repalce(String s1,String s2);
3.7将字符串两端空格去掉
String trim();
3.8将字符串进行连接
String concat(String);
子串。获取字符串中的一部分。
String substring(begin);
String substring(begin,end);
4.比较
compareTo();小返回负数 等返回0 大返回正数(忽略大小写)
练习1:将一个字符串进行反转,将字符串中的一部分进行反转,"abcdefg"-->"abedcfg"
public class StringTest{
public static void main(String[] args) {
String ch = "abcdefg";
ch = reverseString(ch,2,5);
System.out.println(ch);
}
public static String reverseString(String s,int start,int end)//从start与end之间进行反转
{
//字符串变数组。
char[] chs = s.toCharArray();
//反转数组。
reverse(chs,start,end);
//将数组变成字符串并返回。
return new String(chs);
}
public static String reverseString(String s)//反转整个字符串
{
return reverseString(s,0,s.length());
}
private static void reverse(char[] arr,int x,int y)
{
for(int start=x,end=y-1; start<end ; start++,end--)
{
swap(arr,start,end);
}
}
private static void swap(char[] arr,int x,int y)
{
char temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
总结:写代码时尽量将方法进行封装,在主函数中操作所需的代码越少越好。
练习2:
获取一个字符串在另一个字符串中出现的次数."abkkcdkkefkkskk"中kk出现的次数。
class StringTest
{
public static int getSubCount(String str,String key)
{
int count = 0;
int index = 0;
while((index=str.indexOf(key))!=-1)
{
sop("str="+str);
str = str.substring(index+key.length());
count++;
}
return count;
}
方式二。
public static int getSubCount_2(String str,String key)
{
int count = 0;
int index = 0;
while((index= str.indexOf(key,index))!=-1)
{
sop("index="+index);
index = index + key.length();
count++;
}
return count;
}
public static void main(String[] args)
{
String str = "kkabkkcdkkefkks";
///sop("count====="+str.split("kk").length);不建议使用。
sop("count="+getSubCount_2(str,"kk"));
}
public static void sop(String str)
{
System.out.println(str);
}
}
思路:
1,定义个计数器。
2,获取kk第一次出现的位置。
3,从第一次出现位置后剩余的字符串中继续获取kk出现的位置。每获取一次就计数一次。
4,当获取不到时,计数完成。
StringBuffer
是个字符串缓冲区对象,用于存储数据的容器
特点:1.长度是可变的
2.可以存储不同类型数据
3.最终要转换成字符串使用
4.可以对字符串进行修改
功能
1.添加
append(data)将指定数据添加到已有数据的结尾处
insert(位置,字符串) 将数据插入到指定位置
2.删除
delete(start,end) 删除缓冲区中的数据,包含start不包含end
deleteCharAt(ine index)删除指定位置元素
StringBuffer.delete(0,sb.length());
3.查找
char charAt(index);
int indexof(string)
int lastindexof(String);
4.修改
StringBuffer replace(start,end.String)
void setCharAt(index,char)
setlength()设置长度
5.反转
StringBuffer reverse();
6.将缓冲区中的指定数据存储到字符数组中
void getChars(int srcBegin,int srcEnd,char[] dst,int dstbegin)
StringBuilder 线程不同步的(JDK1.5版本之后出现的)。StringBuffer是同步的,避免了数据错乱(一边添加一边删除就有概率会出现这种错误)。
以后开发建议使用StringBuilder效率快。