1、String类
JAVA中用String类来描述字符串。
字符串是一个特殊的对象。其最大的特点是一旦被初始化就不可以被改变。
格式:
-String str1=”abc”;
-String str2=newString(“abc”);
str1与str2的区别:str1在内存中有一个对象,str2在内存中有两个对象。
String类复写了Object类中的equals方法,该方法用于判断字符串内容是否相同。
2、String类常用方法
2.1、获取和判断
(1)int length():获取字符串长度。
(2)charcharAt(int index):根据位置获取某位置上的字符。当访问到字符串不存在的角标时会发生StringIndexOutOfBoundsException异常。
(3)intindexOf(int ch):返回某字符在字符串中第一次出现的位置。
int indexOf(int ch,int fromIndex):从fromIndex位置开始,返回某字符在字符串中第一次出现的位置。如果没有找到则返回-1。
int indexOf(String str):返回str在字符串中第一次出现的位置。如果没有找到则返回-1。
int indexOf(String str,int fromIndex):从fromIndex位置开始,返回str在字符串中第一次出现的位置。如果没有找到则返回-1。
(4)int lastIndexOf(int ch):返回指定字符在此字符串中最后一次出现处的索引。如果未出现该字符,则返回-1
。
(5)boolean startsWith(String str):测试此字符串是否以指定的前缀开始。若是则返回 true
(6)booleanendsWith(String str):测试此字符串是否以指定的后缀结束。若是则返回true
(7)booleanisEmpty():仅当length()为0时,返回true。
(8)booleancontains(str):当且仅当此字符串包含指定的 char 值序列时,返回 true。
(9)booleanequals(str):判断字符串是否相同。若相同返回true。
(10)booleanequalsIgnoreCase():判断内容是否相同,并忽略大小写。
2.2、转换
(1)构造函数String(char[]ch):分配一个新的 String
,使其表示字符数组参数中当前包含的字符序列。
构造函数String(char[]ch,int offset,int count):分配一个新的String
,它包含取自字符数组参数一个子数组的字符。
(2)staticString copyValueOf(char[] ch):返回指定数组中表示该字符序列的 String
static String copyValueOf(char[] ch,intoffset,int count):返回指定数组中表示该字符序列的 String。
static String valueOf(char[] ch):返回 char
数组参数的字符串表示形式。
static String valueOf(基本数据类型):将基本数据类型的数据转换成字符串
(3)char[]toCharArray():将此字符串转换为一个新的字符数组。
byte[] getBytes():使用指定的字符集将此 String
编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
2.3、替换与切割
(1)Stringreplace(char oldChar,char newChar):返回一个新的字符串,它是通过用 newChar
替换此字符串中出现的所有oldChar
得到的。若要替换的字符不存在,返回的还是原串。
String replace(CharSequence target, CharSequencereplacement):使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。该替换从字符串的开头朝末尾执行。
(2)String[] split(String regex):根据给定字符串分此字符串。
(3)Stringsubstring(int begin):返回从指定位置开始到结尾处的子串。
String substring(int begin,int end):返回从指定的 begin
处开始,直到索引 end - 1
处的字符串。
2.4、去除空格和比较
(1)StringtoUpperCase():将字符串转成大写。
String toLowerCase():将字符串转成小写。
(2)String Trim():将字符串两端的多个空格去除。
(3)int compartTo(String str):对两个字符串进行自然顺序的比较。如果参数字符串等于此字符串,则返回值 0
;如果此字符串按字典顺序小于字符串参数,则返回一个小于0
的值;如果此字符串按字典顺序大于字符串参数,则返回一个大于 0
的值。
3、字符串练习
classStringTest
{
public static void main(String[] args)
{
String s=” ad cd ”;
System.out.println(myTrim(s1));
System.out.println(reverseString(s));
String s1=“abkkcdkkefkkskk”;
System.out.println(getSubCount(s1,”kk”);
String str1=”abcwerthelloyueodef”;
String str2=”cvhellobnm”;
System.out.println(getMaxSubString(str1,str2));
}
//练习一,去除字符串两端空格
public static String myTrim(String str)
{
int start=0,end=str.length();
while(start<=end&&str.charAt(start)==’’)
start++;
while(start<=end&&str.charAr(end)==’’)
end—;
return str.substring(start,end+1);
}
/*
练习二,将一个字符串反转
思路:
1、将字符串变成数组
2、将数组反转
3、将数组变成字符串
*/
publicstatic String reverseString(String str)
{
char[] ch=str.toCharArray();
for(intstart=0,end=ch.length-1;start<end;start++,end--)
{
char temp=ch[start];
ch[start]=ch[end];
ch[end]=temp;
}
return new String(ch);
}
/*
练习三:获取一个字符串在另一个字符串中出现的次数,”abkkcdkkefkkskk”
思路:
1,定义一个计数器
2,获取kk第一次出现的位置
3,从第一次出现位置后剩余的字符串中继续获取kk出现的位置,每获取一次就计数一次
4,当获取不到时,计数完成
*/
public static int getSubCount(Stringstr,String key)
{
int count=0;
int index=0;
while((index=str.indexOf(key))!=-1)
{
str=str.substring(index+key.length());
count++;
}
return count;
}
//练习三:方式二
public static int getSubCount(Stringstr,String key)
{
int count=0;
int index=0;
while((index=str.indexOf(key,index))!=-1)
{
index=index+key.length();
count++;
}
return count;
}
/*
练习四:获取两个字符串中最大相同子串。”abcwerthelloyueodef”,”cvhellobnm”
思路:
1、将短的那个子串按照长度递减的方式获取到
2、将每获取到的子串去长串中判断是否包含,如果包含,已经找到
*/
publicstatic 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 temp=min.substring(y,z);
if(max.contains(temp)
return temp;
}
}
}
}
3、StringBuffer
3.1、特点:
(1)StringBuffer是字符串缓冲区,是一个容器。
(2)而且长度是可变化的。
(3)可以直接操作多个数据类型。
(4)最终会通过toString方法变成字符串。
3.2、StringBuffer常用方法:
(1)StringBuffer append(数据类型 数据):将指定数据作为参数添加到已有数据结尾处。
(2)StringBuffer insert(index,数据):将数据插入到指定index位置。
(3)StringBuffer delete(int start,int end):删除缓冲区中的数据,包含start,不包含end。
(4)StringBuffer deleteCharAt(int index):删除指定位置的字符。
(5)int indexOf(String str):返回第一次出现的指定子字符串在该字符串中的索引。
(6)int lastindexOf(String str):返回最右边出现的指定子字符串在此字符串中的索引。
(7)int length():返回长度(字符数)。
(8)String substring(int start,int end):返回一个新的 String
,它包含此序列当前所包含的字符子序列。
(9)StringBufferreplace(int start,int end,String str):使用给定 String
中的字符替换此序列的子字符串中的字符。
(10)void setCharAt(intindex,char ch): 将给定索引处的字符设置为ch
。
(11)StringBufferreverse():将此字符序列用其反转形式取代。
(2)voidgetChars(int srcBegin,int srcEnd, char[] dst, int dstBegin)
:
将字符从此序列复制到目标字符数组dst
。
4、StringBuilder
StringBuilder是JDK1.5版本之后出现的,此类提供一个与 StringBuffer
兼容的 API,但不保证同步。StringBuilder与StringBuffer的区别是,StringBuffer是线程同步的,StringBuilder是线程不同步的。单线程时建议使用StringBuilder,多线程使用StringBuffer。
5、基本数据类型对象包装类
(1)基本数据类型包装类包括Byte、Short、Integer、Long、Boolean、Float、Double和Character。
(2)基本数据类型对象包装类的最常见作就是用于基本数据类型和字符串之间做转换。
(3)基本数据类型转成字符串方式:
――基本数据类型+””
――基本数据类型包装类.toString(基本数据类型值)
如:Integer.toString(34)
(4)字符串转成基本数据类型:xxxa=基本数据类型包装类.parseXxx(String str),Xxx是基本数据类型。如int a=Integer.parseInt(“123”)
(5)基本数据类型包装类新特性:
-自动装箱:如Integer x=4,相当于Integerx=new Integer(4)
-自动拆箱:如x=x+4,先将x 变成int型,再和2进行加法运算,再将和进行装箱赋给 x。