Java中的字符串是由双引号括起来的多个字符。Java中的字符采用Unicode编码。
单个字符如果用双引号括起来,由其表示的是字符串,而不是字符。
Java SE提供了三个字符串类:String、StringBuffer、StringBuilder。String是不可变字符串,StringBuffer和StringBuilder是可变字符串。
不可变字符串和可变字符串的区别在于当字符串进行拼接等修改操作时,不可变字符串会创建新的字符串对象,而可变字符串不会创建对象。
Java中不可变字符串类是String,属于java.lang包,它也是java非常重要的类。
String():使用空字符串创建并初始化一个新的String对象。
String(String original):使用另外一个字符串创建并初始化一个新的String对象。
String(StringBuffer buffer):使用可变字符串对象(StringBuffer)创建并初始化一个新的String对象。
String(StringBuffer builder):使用可变字符串对象(StringBuffer)创建并初始化一个新的String对象。
String(byte[] bytes):使用平台的默认字符集解码指定的byte数组,通过byte数组创建并初始化一个新的String对象。
String(char[] value):通过字符数组创建并初始化一个新的String对象。
String(char[] value, int offset, int count):通过字符数组的子数组创建并初始化一个新的String对象;offset参数是子数组第一个字符的索引,count参数指定子数组的长度。
创建字符串对象示例代码如下:
package helloworld;
public class helloworld {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1=new String();
String s2=new String("Hello World");
String s3=new String("\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064");
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 bytes[]= {97,98,99};
String s6=new String(bytes);
System.out.println("s6 = "+s6);
System.out.println("s6字符串长度 = "+s6.length());
}
}
输出结果:
s2 = Hello World
s3 = Hello World
s4 = abcde
s5 = bcde
s6 = abc
s6字符串长度 = 3
字符串池:(代码错误,未掌握)
Java中的不可变字符串String常量采用字符串池(String Pool)管理技术,字符串池是一种字符串驻留技术 。
代码如下:
package helloworld;
public class helloworld {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s7=new String("Hello");
String s8=new String("Hello");
String s9="Hello";
String s10="Hello";
System.out.printf("s7 == s8 : %b%n, s7==s8");
System.out.printf("s9 == s10: %b%n, s9==s10");
System.out.printf("s7 == s9 : %b%n, s7==s9");
System.out.printf("s8 == s9 : %b%n, s8==s9");
}
}
运行结果:
s7 == s8 : Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%b'
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at helloworld.helloworld.main(helloworld.java:13)
字符串拼接:
字符串拼接示例如下:
package helloworld;
public class helloworld {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1="Hello";
//使用+运算符连接
String s2=s1+" ";
String s3=s2+"World";
System.out.println(s3);
String s4="Hello";
//使用+运算符连接,支持+=赋值运算符
s4+=" ";
s4+="World";
System.out.println(s4);
String s5="Hello";
//使用concat方法连接
s5=s5.concat(" ").concat("World");
System.out.println(s5);
int age=18;
String s6="Her ages is "+ age +" age old.";
System.out.println(s6);
char score='A';
String s7="Her English score is "+score+".";
System.out.println(s7);
java.util.Date now=new java.util.Date();
String s8="Today is "+now +".";
System.out.println(s8);
}
}
输出结果:
Hello World
Hello World
Hello World
Her ages is 18 age old.
Her English score is A.
Today is Fri Jan 07 13:32:37 CST 2022.
字符串查找:
在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所在处的索引。
字符串查找示例代码如下:
package helloworld;
public class helloworld {
public static void main(String[] args) {
// TODO Auto-generated method stub
String sourceStr ="There is a string accessing example.";
//获得字符串长度
int len= sourceStr.length();
//获得索引位置16的字符
char ch= sourceStr.charAt(16);
//查找字符和子字符串
int firstChar1= sourceStr.indexOf('r');
int lastChar1= sourceStr.lastIndexOf('r');
int firstStr1= sourceStr.indexOf("ing");
int lastStr1= sourceStr.lastIndexOf("ing");
int firstChar2= sourceStr.indexOf('e', 15);
int lastChar2= sourceStr.lastIndexOf('e', 15);
int firstStr2= sourceStr.indexOf("ing", 5);
int lastStr2= sourceStr. lastIndexOf ("ing", 5);
System.out.println("原始字符串:"+ sourceStr);
System.out.println("字符串长度:"+len);
System.out.println("索引16的字符:"+ch);
System.out.println("从前往后搜索r字符,第一次找到它所在索引:" +firstChar1);
System.out.println("从后往前搜索r字符,第一次找到它所在索引:" +lastChar1);
System.out.println("从前往后搜索ing字符串,第一次找到它所在索引:" +firstStr1);
System.out.println("从后往前搜索ing字符串,第一次找到它所在索引:" +lastStr1);
System.out.println("从索引为15位置开始,从前往后搜索e字符,第一次找到它所在索引:"+firstChar2);
System.out.println("从索引为15位置开始,从后往前搜索e字符,第一次找到它所在索引:"+lastChar2);
System.out.println("从索引为5位置开始,从前往后搜索1ng字符串,第一次找到它所在索引:"+firstStr2);
System.out.println("从索引为5位置开始,从后往前搜索ing字符串,第一次找到它所在索引:"+lastStr2);
}
}
输出结果如下:
原始字符串:There is a string accessing example.
字符串长度:36
索引16的字符:g
从前往后搜索r字符,第一次找到它所在索引:3
从后往前搜索r字符,第一次找到它所在索引:13
从前往后搜索ing字符串,第一次找到它所在索引:14
从后往前搜索ing字符串,第一次找到它所在索引:24
从索引为15位置开始,从前往后搜索e字符,第一次找到它所在索引:21
从索引为15位置开始,从后往前搜索e字符,第一次找到它所在索引:4
从索引为5位置开始,从前往后搜索1ng字符串,第一次找到它所在索引:14
从索引为5位置开始,从后往前搜索ing字符串,第一次找到它所在索引:-1
字符串比较:
1、比较相等
boolean equals(Object anObject):比较两个字符串中内容是否相等。
boolean equalsIgnoreCase(String anotherString):类似equals方法,只是忽略大小写。
2、比较大小
int compareTo(String anotherString):按字典顺序比较两个字符串。如果参数字符串等于此字符串,则返回值0;如果此字符串小于参数字符串,则返回一个小于0的值;如果此字符串大于参数字符串,则返回一个大于0的值。
int compareToIgnoreCase(String str):类似compareTo,只是忽略大小写。
3、比较前缀和后缀
boolean endsWith(String suffix):测试此字符串是否以指定的后缀结束。
boolean startsWith(String prefix):测试些字符串是否以指定的前缀开始。
字符串比较示例代码如下:
package helloworld;
public class helloworld {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1=new String("Hello");
String s2=new String("Hello");
//比较字符串是否是相同的引用
System.out.println("s1==s2:"+(s1==s2));
//比较字符串内容是否相等
System.out.println("s1.equals(s2):"+(s1.equals(s2)));
String s3="HELLO";
//忽略大小写比较字符串内容是否相等
System.out.println("s1.equalsIgnoreCase(s3):"+(s1.equalsIgnoreCase(s3)));
//比较大小
String s4="java";
String s5="Swift";
//比较字符串大小 s4>s5
System.out.println("s4.compareTo(s5):"+(s4.compareTo(s5)));
//忽略大小写比较字符串大小 s4<s5
System.out.println("s4.compareToIgnoreCase(s5):"+(s4.compareToIgnoreCase(s5)));
//判断文件夹中文件名
String[] docFolder= {"java.docx","JavaBean.docx","Objecitive-C.xlsx","Swift.docx"};
int wordDocCount=0;
//查找文件夹中Word文档个数
for(String doc:docFolder) {
//去掉前后空格
doc=doc.trim();
//比较后缀是否有.docx字符串
if(doc.endsWith(".docx")) {
wordDocCount++;
}
}
System.out.println("文件夹中word文档个数是: "+wordDocCount);
int javaDocCount=0;
//查找文件夹中Java相关文档个数
for(String doc: docFolder) {
//去掉前后空格
doc=doc.trim();
//全部字符转成小写
doc=doc.toLowerCase();
//比较前缀是否有java字符串
if(doc.startsWith("java")) {
javaDocCount++;
}
}
System.out.println("文件夹中Java相关文档个数是: "+javaDocCount);
}
}
输出结果:
s1==s2:false
s1.equals(s2):true
s1.equalsIgnoreCase(s3):true
s4.compareTo(s5):23
s4.compareToIgnoreCase(s5):-9
文件夹中word文档个数是: 3
文件夹中Java相关文档个数是: 2
字符串截取:
String substring(int beginIndex):从指定索引beginIndex开始截取一直到字符串结束的子字符串。
String substring(int beginIndex, int endIndex):从指定索引beginIndex开始截取直到索引endIndex-1处的字符,注意包括索引为beginIndex处的字符,但不包括索引为endIndex处的字符。
字符串截取方法示例代码如下:
package helloworld;
public class helloworld {
public static void main(String[] args) {
// TODO Auto-generated method stub
String sourceStr="There is a string accessing example.";
//截取example.子字符串
String subStr1=sourceStr.substring(28);
//截取string子字符串
String subStr2=sourceStr.substring(11,17);
System.out.printf("subStr1= %s%n", subStr1);
System.out.printf("subStr2=%s%n", subStr2);
//使用split方法分隔字符串
System.out.println("-----使用split方法-----");
String[] array= sourceStr.split(" ");
for(String str:array) {
System.out.println(str);
}
}
}
输出结果:
subStr1= example.
subStr2=string
-----使用split方法-----
There
is
a
string
accessing
example.
可变字符串:
Java提供了两个可变字符串类StringBuffer和StringBuilder,中文翻译为“字符串缓冲区”。
StringBuffer和StringBuilder:
字符串追加:
字符串插入、删除和替换