Java String类的常用方法汇总
一、String类
String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象。java把String类声明的final类,不能有类。String类对象创建后不能修改,由0或多个字符组成,包含在一对双引号之间。
二、String类对象的创建
字符串声明:StringstringName;
字符串创建:stringName=newString(字符串常量);或stringName=字符串常量;
三、String类构造方法
1、publicString()
无参构造方法,用来创建空字符串的String对象。
Stringstr1=newString();
2、publicString(Stringvalue)
用已知的字符串value创建一个String对象。
Stringstr2=newString("asdf");2Stringstr3=newString(str2);
3、publicString(char[]value)
用字符数组value创建一个String对象。
char[]value={"a","b","c","d"};
Stringstr4=newString(value);//相当于Stringstr4=newString("abcd");
4、publicString(charchars[],intstartIndex,intnumChars)
用字符数组chars的startIndex开始的numChars个字符创建一个String对象。
char[]value={"a","b","c","d"};
Stringstr5=newString(value,1,2);//相当于Stringstr5=newString("bc");
5、publicString(byte[]values)
用比特数组values创建一个String对象。
byte[]strb=newbyte[]{65,66};
Stringstr6=newString(strb);//相当于Stringstr6=newString("AB");
四、String类常用方法
1、求字符串长度
publicintlength()//返回该字符串的长度
Stringstr=newString("asdfzxc");
intstrlength=str.length();//strlength=7
2、求字符串某一位置字符
publiccharcharAt(intindex)//返回字符串中指定位置的字符;注意字符串中第一个字符索引是0,最后一个是length()-1。
Stringstr=newString("asdfzxc");
charch=str.charAt(4);//ch=z
3、提取子串
用String类的substring方法可以提取字符串中的子串,该方法有两种常用参数:
1)publicStringsubstring(intbeginIndex)//该方法从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回。
2)publicStringsubstring(intbeginIndex,intendIndex)//该方法从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符作为一个新的字符串返回。
Stringstr1=newString("asdfzxc"); Stringstr2=str1.substring(2);//str2="dfzxc" Stringstr3=str1.substring(2,5);//str3="dfz"
4、字符串比较
1)publicintcompareTo(StringanotherString)//该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。
2)publicintcompareToIgnore(StringanotherString)//与compareTo方法相似,但忽略大小写。
3)publicbooleanequals(ObjectanotherObject)//比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。
4)publicbooleanequalsIgnoreCase(StringanotherString)//与equals方法相似,但忽略大小写。
Stringstr1=newString("abc"); Stringstr2=newString("ABC"); inta=str1.compareTo(str2);//a>0 intb=str1.compareTo(str2);//b=0 booleanc=str1.equals(str2);//c=false booleand=str1.equalsIgnoreCase(str2);//d=true
5、字符串连接
publicStringconcat(Stringstr)//将参数中的字符串str连接到当前字符串的后面,效果等价于"+"。
Stringstr="aa".concat("bb").concat("cc"); 相当于Stringstr="aa"+"bb"+"cc";
6、字符串中单个字符查找
1)publicintindexOf(intch/Stringstr)//用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。
2)publicintindexOf(intch/Stringstr,intfromIndex)//改方法与第一种类似,区别在于该方法从fromIndex位置向后查找。
3)publicintlastIndexOf(intch/Stringstr)//该方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找。
4)publicintlastIndexOf(intch/Stringstr,intfromIndex)//该方法与第二种方法类似,区别于该方法从fromIndex位置向前查找。
Stringstr="Iamagoodstudent"; inta=str.indexOf('a');//a=2 intb=str.indexOf("good");//b=7 intc=str.indexOf("w",2);//c=-1 intd=str.lastIndexOf("a");//d=5 inte=str.lastIndexOf("a",3);//e=2
7、字符串中字符的大小写转换
1)publicStringtoLowerCase()//返回将当前字符串中所有字符转换成小写后的新串
2)publicStringtoUpperCase()//返回将当前字符串中所有字符转换成大写后的新串
Stringstr=newString("asDF"); Stringstr1=str.toLowerCase();//str1="asdf" Stringstr2=str.toUpperCase();//str2="ASDF"
8、字符串中字符的替换
1)publicStringreplace(charoldChar,charnewChar)//用字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串。
2)publicStringreplaceFirst(Stringregex,Stringreplacement)//该方法用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,应将新的字符串返回。
3)publicStringreplaceAll(Stringregex,Stringreplacement)//该方法用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,应将新的字符串返回。
Stringstr="asdzxcasd"; Stringstr1=str.replace('a','g');//str1="gsdzxcgsd" Stringstr2=str.replace("asd","fgh");//str2="fghzxcfgh" Stringstr3=str.replaceFirst("asd","fgh");//str3="fghzxcasd" Stringstr4=str.replaceAll("asd","fgh");//str4="fghzxcfgh"
9、其他类方法
1)Stringtrim()//截去字符串两端的空格,但对于中间的空格不处理。
Stringstr="asd"; Stringstr1=str.trim(); inta=str.length();//a=6 intb=str1.length();//b=4
2)booleanstatWith(Stringprefix)或booleanendWith(Stringsuffix)//用来比较当前字符串的起始字符或子字符串prefix和终止字符或子字符串suffix是否和当前字符串相同,重载方法中同时还可以指定比较的开始位置offset。
Stringstr="asdfgh"; booleana=str.statWith("as");//a=true booleanb=str.endWith("gh");//b=true
3)regionMatches(booleanb,intfirstStart,Stringother,intotherStart,intlength)//从当前字符串的firstStart位置开始比较,取长度为length的一个子字符串,other字符串从otherStart位置开始,指定另外一个长度为length的字符串,两字符串比较,当b为true时字符串不区分大小写。
4)contains(Stringstr)//判断参数s是否被包含在字符串中,并返回一个布尔类型的值。
Stringstr="student"; str.contains("stu");//true str.contains("ok");//false
5)String[]split(Stringstr)//将str作为分隔符进行字符串分解,分解后的字字符串在字符串数组中返回。
Stringstr="asd!qwe|zxc#";
String[]str1=str.split("!|#");//str1[0]="asd";str1[1]="qwe";str1[2]="zxc";
五、字符串与基本类型的转换
1、字符串转换为基本类型
java.lang包中有Byte、Short、Integer、Float、Double类的调用方法:
1)publicstaticbyteparseByte(Strings)
2)publicstaticshortparseShort(Strings)
3)publicstaticshortparseInt(Strings)
4)publicstaticlongparseLong(Strings)
5)publicstaticfloatparseFloat(Strings)
6)publicstaticdoubleparseDouble(Strings)
例如:
intn=Integer.parseInt("12"); floatf=Float.parseFloat("12.34"); doubled=Double.parseDouble("1.124");
2、基本类型转换为字符串类型
String类中提供了StringvalueOf()放法,用作基本类型转换为字符串类型。
1)staticStringvalueOf(chardata[])
2)staticStringvalueOf(chardata[],intoffset,intcount)
3)staticStringvalueOf(booleanb)
4)staticStringvalueOf(charc)
5)staticStringvalueOf(inti)
6)staticStringvalueOf(longl)
7)staticStringvalueOf(floatf)
8)staticStringvalueOf(doubled)
例如:
Strings1=String.valueOf(12);
Strings1=String.valueOf(12.34);
3、进制转换
使用Long类中的方法得到整数之间的各种进制转换的方法:
Long.toBinaryString(longl)
Long.toOctalString(longl)
Long.toHexString(longl)
Long.toString(longl,intp)//p作为任意进制
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。