一、String构造方法
String为不可变字符串类(字符串进行拼接等修改操作时,会创建新的字符串对象而可变字符串不会创建)!(更具体的可以参考API)
创建String对象可以通过构造方法实现,常用的构造方法如下:
- String() :使用空字符创建并初始化一个新的String对象。
- Sting(String original):使用另一个字符串创建并初始化一个新的String对象。
- String(StringBuffer buffer)/String(StringBuilder builder):使用可变字符串对象(StringBuffer/StringBuilder)创建并初始化一个新的String对象。
- String(byte [ ]bytes):使用平台默认字符集解码指定的byte数组,通过byte数组创建并初始化一个新的对象。
- String(char [ ] value):通过字符串数组…
- String(char [ ]value,int offset,int count):通过字符数组的字数组创建并初始化一个新的String对象;offset参数是子数组第一个字符的索引,count是子数组的长度;
String str=“abc”;
相当于:
char data[ ]={‘a’,‘b’,‘c’};
String str=new String(data);
public class StringTT {
public static void main(String []agrs){
String s1=new String();
String s2=new String("No girlfriend");
String s3=new String("\u0048\u0065\u006c......");//字符的Unicode码
System.out.println("s2="+s2);
System.out.println("s3="+s3);
char chars[]={'a','b','c','d','e'};
//通过字符数组创建字符对象
String s4=new String(chars);
//通过子字符串创建字符对象
String s5=new String(chars,1,4);
System.out.println("s4="+s4);
System.out.println("s5="+s5);
byte btyes[]={97,98,99};
String s6=new String(btyes);
System.out.println("s6="+s6);
}
}
结果:
s2=No girlfriend
s3=Hel…
s4=abcde
s5=bcde
s6=abc
二、字符串池(String Pool)
Java中的不可变量字符串String采用字符串词(String Pool)管理技术,字符串词是一种字符串驻留技术。
当采用字符串常量赋值时,会在字符串池中查找是否已经存在该常量。如果不存在,则在String Pool中创建一个对象,然后将其地址返回;如果在String Pool中查找时已经存在该对象,则不创建任何对象,直接将String Pool中的这个对象地址返回。此原理不适用于new创建的字符串对象。
示例:
public class StringPoll{
public static void main(String []agrs){
String s1="abc";//查找String Pool中有没有“abc”字符串,没有则在String Pool中创建“abc”对象;
String s2="abc";//查找String Pool中有没有有“abc”字符串,有则把“abc”引用赋值给s2;
String s3=new String("abc");//直接创建对象不查找String Pool
String s4=new String("abc");//直接再创建对象不查找String Pool
System.out.printf("s1==s2:%b\n",s1==s2);
System.out.printf("s1==s3:%b\n",s1==s3);
System.out.printf("s3==s4:%b\n",s3==s4);
}
}
输出结果:
s1== s2:true
s1== s3:false
s3== s4:false
三、字符拼接
String字符串拼接可以使用**+运算符或String的concat**(String str)方法。+运算符的优势是可以连接任何类型数据拼接成为字符串,而concat方法只能拼接String类型字符串。
举个栗子:
java.util.Date time=new java.util.Date();//java.util.Date是日期类
String str="今天是:"+time;
System.out.print(str);
运行结果:
今天是:Tue Oct 01 22:08:55 CST 2019
这是与对象进行拼接,Java中所有对象都有一个toString()方法,该方法可以将对象转换为字符串,拼接过程会调用该对象的toString()方法,将该对象转换成字符串后再进行拼接。
四、字符串查找
在String类中提供了indexOf和lastIndexOf方法用于查找字符或字符串,返回值是查找的字符或字符串所在位置,-1是没有找到。这两个方法有多个重载版本。
- int indexOf(int ch):从前往后搜索字符ch,返回第一次找到字符ch所在处的索引。
- int indexOf(int ch,int fromIndex):从指定的索引开始从前往后搜索字符ch,返回第一次找到字符ch所在处的索引。
- int indexOf(String str): 从前往后搜索字符串str,返回第一次找到字符串str所在处的索引。
- int indexOf(String str,int fromIndex):从指定的索引开始从前往后搜索字符串str,返回第一次找到字符串str所在处的索引。
- int lastIndexOf(int ch):从后往前搜索字符ch,返回第一次找到字符ch所在处的索引。
- int lastIndexOf(int ch,int fromIndex):从指定的索引开始从后往前搜索字符ch,返回第一次找到字符ch所在处的索引。
- int lastIndexOf(String str): 从后往前搜索字符串str,返回第一次找到字符串str所在处的索引。
- int lastIndexOf(String str,int fromIndex):从指定的索引开始从后往前搜索字符串str,返回第一次找到字符串str所在处的索引。
字符串的本质是字符数组,因此它有索引,索引从0开始。String的charAt(int
index)方法可以返回索引index所在位置的字符。
举个栗子:
public class StringLook{
public static void main(String []args){
String example="It's impossible to have a girlfriend";
//获得字符串长度
int len=example.length();
//获得索引为11的字符
char ch=example.charAt(11);
//查找字符和子字符串
int firstChar1=example.indexOf('r');
int lastChar1=example.lastIndexOf('r');
int firstStr1=example.indexOf("girl");
int lastStr1=example.lastIndexOf("girl");
int firstChar2=example.indexOf('s',5);
int lastChar2=example.lastIndexOf('s',5);
System.out.println("原始字符串:"+example);
System.out.println("原始字符串长度:"+len);
System.out.println("索引为11的字符为:"+ch);
System.out.println("从前往后搜索'r',第一次找到他的索引为:"+firstChar1);
System.out.println("从后往前搜索'r',第一次找到他的索引为:"+lastChar1);
System.out.println("从前往后搜索girl字符串,第一次找到他的索引为:"+firstStr1);
System.out.println("从后往前搜索girl字符串,第一次找到他的索引为:"+lastStr1);
System.out.println("从索引5开始,从前往后搜索's',第一次找到他的索引为:"+firstChar2);
System.out.println("从索引5开始,从后往前搜索's',第一次找到他的索引为:"+lastChar2);
}
}
结果:
原始字符串:It’s impossible to have a girlfriend
原始字符串长度:36
索引为11的字符为:i
从前往后搜索’r’,第一次找到他的索引为:28
从后往前搜索’r’,第一次找到他的索引为:31
从前往后搜索girl字符串,第一次找到他的索引为:26
从后往前搜索girl字符串,第一次找到他的索引为:26
从索引5开始,从前往后搜索’s’,第一次找到他的索引为:9
从索引5开始,从后往前搜索’s’,第一次找到他的索引为:3
五、字符串比较
基操:比较相等、比较大小、比较前缀和后缀!!!!!!!!
1. 比较相等
String提供的比较字符串相等的方法:
- boolean equals(Object anObject):比较两个字符串内容是否相等。
- boolean equalsIgnoreCase(String anotherString):类似equals方法,只是忽略大小写。
2.比较大小
String提供比较大小的方法:
-
int compareTo(String another
String):按字典顺序比较两个字符串。如果参数字符串等于此字符串,则返回值0;如果此字符串小于参数字符串,则返回一个小于0的数;大于则返回一个大于0的值。 -
int compareToIgnore(String str):类似compareTo,只是忽略大小写。
3.比较前缀和后缀
- boolean endsWith(String suffix):测试此字符串是否以指定的后缀结束。
- boolean startsWith(String prefix):测试此字符串是否以指定的前缀开始。
Look Look代码栗子
/*trim()方法可以去除字符串前后空格。
* toLowerCase()方法可以将字符串转换成小写
* toUpperCase()方法,可将字符串全部转换成大写*/
public class StringTT{
public static void main(String []args){
String s1=new String("Hello girl!");
String s2=new String("Hello girl!");
//比较字符串是否是相同引用
System.out.println("s1==s2:"+(s1==s2));//原理参考上面的String Pool
//比较内容是否相等
System.out.println("s1.equal(s2):"+(s1.equals(s2)));
//判断文件夹中文件名
String[] docFolder={"java.docx","Javabean.docx","Objecitive-C.xlsx","Swift.docx"};
int wordCount=0;
//查找文件夹中word个数(利用后缀查找)
for(String doc:docFolder)
{
//去掉前后空格
doc=doc.trim();
//比较后缀是否有.docx字符串
if(doc.endsWith(".docx"))
{
wordCount++;
}
}
System.out.println("文件夹中word数目有:"+wordCount);
//查找文件夹中关于java文件的个数
int javadoc=0;
for(String doc:docFolder)
{
//去掉前后空格
doc=doc.trim();
//全部转换成小写
doc=doc.toLowerCase();
//比较前缀java字符串
if(doc.startsWith("java"))
{
javadoc++;
}
}
System.out.println("文件夹中关于java的文件数目有:"+javadoc);
}
}
结果:
s1==s2:false
s1.equal(s2):true
文件夹中word数目有:3
文件夹中关于java的文件数目有:2
六、字符串截取
- String substring(int beginInedex):从指定索引beginIndex开始截取一直到字符串结束的子字符串。
- String substring(int beginIndex,int
endIndex):从指定索引beginIndex开始截取直到endIndex-1处的字符,注意包括索引为beginIndex处的字符,但不包括endIndex处的字符。 - String还提供了字符串分隔方法:split(String rgex);参数是分隔字符串,返回值String[ ]。
举个栗子:
String str="boo:and:foo";
String[] array1=str.split(":");
System.out.println("以:分隔:");
for(String result:array1)
{
System.out.println(result);
}
System.out.println("以o分隔:");
String[] array2=str.split("o");
for(String result:array2)
{
System.out.println(result);
}
/*结果:
以:分隔:
boo
and
foo
以o分隔:
b
:and:f
*/