字符串
1. 获取字符串信息
1.1 连接
使用“+”可以将字符串与字符串或者其他类型数据进行连接。
1.2 获取字符串长度
使用str.length()可以获取字符串长度。
1.3 查找字符串
String类提供了两种查找方式:indexOf()和lastIndexOf(),分别用于查找指定字符串在字符串中首次和最后一次出现的索引位置(注意:字符串的索引为:0~length-1)。indexOf()若没有找到对应字符或字符串的索引,则返回-1,lastIndexOf()同理。
特殊运用:
lastIndexOf(" "),查找空格的索引位置,返回值为字符串的长度。
1.4 获取指定索引位置的字符
str.charAt()可以获取指定位置的字符,返回值为char类型。
点击查看代码(获取字符串信息代码汇总)
public class P_Strings {
public static void main(String[] args) {
String str ="this is a string";
fLength(str);
fIndex(str);
search(str);
}
//字符串的长度
public static void fLength(String str)
{
int size =str.length();
System.out.println("the lenth of str:"+size);
}
//字符串的查找索引
public static void fIndex(String str)
{
int p=str.indexOf("string");
System.out.println("the index of\"string\": "+p);
int q=str.lastIndexOf("p");
System.out.println("the last index of \"s\":"+q);
}
//字符串获取指定索引,超出长度的索引位置运行报错
public static void search(String str)
{
char ch=str.charAt(50);
System.out.println("字符串中索引位置为3的字符为:"+ch);
}
}
2. 字符串操作
2.1 获取子字符串
通过str.substring(beginIndex ,endIndex)可以获取从索引beginIndex开始,到索引endIndex结束的子字符串。
2.2 一些其他操作
通过str.trim()可以去掉字符串的首位空格;
通过str.replace(oldstring,newstring)可以进行对指定字符串的替换,返回值为一个新字符串;
str.startsWith()和str.endWith()可以判断是否以指定字符串开头或结尾;
str.toLowerCase()和sre.toUpperCase()可以实现字符串内容大小写的转换;
str.split(sign,limit)可以实现对字符串进行分割,sign为给定的分隔符,可以使用正则表达式;limit为限定的分割次数,为可选参数。
一个字符串分割的例子(点击查看代码)
public class P_Strings {
public static void main(String[] args) {
sp();
}
public static void sp()
{
String str="192.168.0.1";
//使用“.”作为分隔符进行分割
String[] arr1=str.split("\\.");
String[] arr2=str.split("\\.",2);
System.out.println("str的原值:"+"["+str+"]");
for ( String x:arr1)
System.out.print("["+x+"]");
System.out.println();
for (String x:arr2)
System.out.print("["+x+"]");
System.out.println();
}
}
运行结果:
一个重要的知识点:字符串的比较
字符串的比较方式一般有两种:“==”和"str.equals()"。
"=="比较的是地址;而equals()方法判断的是字符串的内容.
public class P_Strings {
public static void main(String[] args) {
// equal();
// equal_test();
}
public static void equal()
{
String str1=new String("123");
String str2=new String("123"); //虽然str1和str2两个字符串的内容相同,但是两个字符串指向的内存地址不同,因此使用”==“时,str1!=str2.
if (str1==str2)
System.out.println("使用==判断:两字符串相等");
if(str1.equals(str2))
System.out.println("使用equals方法判断:两字符串相等");
}
public static void equal_test()
{
String str1,str2;
str1="123";
str2=str1; //此时两者指向地址相同
if (str1==str2)
System.out.println("使用==判断,两字符串相等");
}
2.3 compareTo()方法:按照字典顺序比较两个字符串的大小,该比较基于字符串中各字符的Unicode值。存在以下几种情况:
- 按照顺序依次进行比较,但是str1中“b”大于str2中“a”,且Unicode值差为1,所以a的结果为1.
public static void compare()
{
String str1="abca";
String str2="aada";
int a=str1.compareTo(str2); //a=1
System.out.println(str1+"compareTo"+str2+":"+str1.compareTo(str2));
}
- 两个字符串前若干个字符均相等,到第n个字符时,一个字符串尚有字符可以进行比较,而零一字符串无字符可以比较,返回值为两个字符串之间长度的差。
public static void compare1()
{
String str1="abc";
String str2="abca";
int a=str1.compareTo(str2); //a=-1,此时返回的时str1与str2的长度差
System.out.println(str1+"compareTo"+str2+":"+str1.compareTo(str2));
}
- 两字符串前若干个字符均相同,在第n个字符不同,返回值为第n个字符的Unicode的差值。
public static void compare2()
{
String str1="ab";
String str2="ad";
int a=str1.compareTo(str2); //a=-2,返回值为b和d的ASCII码的值差
System.out.println(str1+"compareTo"+str2+":"+str1.compareTo(str2));
}
}
总结:当依次进行比较时,出现不同值时,返回值为该比较值之间的差值,并不再向后进行比较;如果较短字符串为长字符串的子集,那么返回值为两个字符串长度的差值
3. 格式化字符串
String类的静态format()方法用于创建格式化字符串。format()方法有两种重载形式。
(1)format(String format , Object ...args)
该方法使用指定的格式字符串和参数返回一个格式化字符串,格式化后的新字符串使用本地默认的语言环境;
(2)format(Local l;String format,Object ...args)
l:格式化过程中要应用的语言环境,如果l为null,则不进行本地化。
3.1日期和时间字符串的格式化
1.日期格式化:
转换符 | 说明 | 示例 |
---|---|---|
%te | 一个月中的某一天(1-31) | 3 |
%tb | 月份简称 | Feb(英文)、二月(中文) |
%tB | 月份全称 | February(英文、二月(中文)) |
%ta | 星期简称 | Mon(英文)、星期一(中文) |
%tA | 星期全称 | Monday(英文)、星期一(中文) |
%tc | 包括全部日期和时间信息 | 星期一 十二月 20 15:45:56 CST 2021 |
%tY | 4位年份 | 2021 |
%ty | 2位年份 | 21 |
%tm | 月份 | 03 |
%td | 一个月中的第几天 | 02 |
%tj | 一年中的第几天 | 101 |
实例如下:
import java.util.Date;
public class Eval {
public static void main(String[] args) {
Date date =new Date();
String s=String.format("%te",date);
System.out.println("今天是这个月的第"+s+ "天");
String Y=String.format("%tY",date);
System.out.println("今年是"+Y+"年");
String D =String.format("%td",date);
System.out.println("今天是是这个月的第"+D+"天");
String M=String.format("%tm",date);
System.out.println("现在是"+M+"月");
String C=String.format("%tc",date);
System.out.println(C);
}
}
输出结果为:
2.时间格式化:
时间格式化转换符表格如下所示:
转换符 | 说明 | 示例 |
---|---|---|
%tH | 2位数字的24时制的小时(00-23) | 14 |
%tl | 2位数字的12时制的小时(01-12) | 05 |
%tk | 2位数字的24时制的小时(0-23) | 5 |
%tl | 2位数字的12时制的小时(1-12) | 10 |
%tM | 2位数字的分钟(00-59) | 35 |
%tS | 2位数字的秒数(00-60) | 12 |
%tL | 3位数字的毫秒数(000-999) | 457 |
%tN | 9位数字的微秒数(000000000-999999999) | 456133879 |
%tp | 指定语言环境下的上午或者下午 | 下午(中文)、pm(英文) |
%tz | 相对于GMT RFC 82 格式的数字时区偏移量 | +0800 |
%tZ | 时区缩写形式的字符串 | CST |
%ts | 1970-01-01 00:00:00至现在经过的秒数 | 1639920069 |
%tQ | 1970-01-01 00:00:00至现在经过的毫秒数 | 1639920069437 |
实例如下: |
import java.util.Date;
public class GetDate {
public static void main(String[] args) {
Date date=new Date();
String hour=String.format("%tH",date);
String minute=String.format("%tM",date);
String second=String.format("%TS",date);
System.out.println("现在是"+hour +"-"+minute+"-"+second);
String ts=String.format("%ts",date);
System.out.println(ts);
String tQ=String.format("%tQ",date);
System.out.println(tQ);
}
}
运行结果图:
- 格式化常见的日期时间组合
常见的日期和时间组合的格式表格如下:
| 转换符 | 说明 | 示例 |
| ---- | ---- | ---- |
| %tF | “年-月-日”格式(4位年份) | 2021-12-19 |
|%tD|"月/日/年"格式(2位年份)|03/25/21|
|%tc|全部日期和时间信息|星期日 十二月 19 21:25:00 CST 2021|
|%tr|“时:分:秒 PM (AM)”格式(12时制)|09:25:00 下午|
|%tT|“时:分:秒”格式(24时制)|21:25:00|
|%tR|“时:分”格式(24时制)|21:25|
实例如下:
import java.util.Date;
public class DateandTime {
public static void main(String[] args) {
Date date =new Date();
String time=String.format("%tc",date);
String form=String.format("%tF",date);
System.out.println(time);
System.out.println(form);
}
}
运行结果图为:
3.2 常规类型格式化
表1 常规转换符
转换符 | 说明 | 示例 |
---|---|---|
%b,%B | 结果被格式化为布尔类型 | true |
%h,%H | 格式化为散列码 | A05A5198 |
%s,%S | 格式化为字符串类型 | "abcd" |
%c,%C | 格式化为字符类型 | 'a' |
%d | 格式化为十进制整数 | 40 |
%o | 格式化为八进制整数 | 11 |
%x,%X | 格式化为十六进制整数 | 4b1 |
%e | 格式化为计算机科学计数法表示的十进制数 | 1.7000000e+01 |
%a | 格式化为带有效位数和指数的十六进制浮点值 | 0X1.c00000000001P4 |
%n | 结果为特定与平台的行分隔符 | |
%% | 结果为“%” | % |
实例如下:
public class General {
public static void main(String[] args) {
String str=String.format("%d",400/2);
String str2=String.format("%b",3>5);
String str3=String.format("%x",200);
System.out.println("400/2="+str);
System.out.println("3>5?"+str2);
System.out.println("16进制表示200:"+str3);
}
}
运行结果:
3. 正则表达式
正则表达式通常用于判断语句中,用来检查某一字符串是否满足某一格式。正则表达式是含有一些特殊意义字符的字符串,这些特殊字符成为正则表达式的元字符。
元字符 | 正则表达式中的写法 | 意义 |
---|---|---|
. | . | 代表任意一个字符 |
\d | \d | 代表0-9任何一个字符 |
\D | \D | 代表任何一个非数字字符 |
\s | \s | 代表空白字符,如'\t','\n' |
\S | \S | 代表非空白字符 |
\w | \w | 代表可用作标识符的字符,但不包括“$” |
\W | \W | 代表不可用于标识符的字符 |
\p | \p | 代表小写字母a-z |
\p | \p | 代表大写字母A-Z |
\p | \p | 带包ASCII字符 |
\p | \p | 字母字符 |
\p | \P | 十进制数值,0-9 |
\p | \p | 数字或者字母字符 |
\p | \p | 标点符号 |
\p | \p | 可见字符 |
\p | \p | 可打印字符 |
\p | \p | 空格或制表符 |
\p | \p | 控制字符 |
说明:
在正则表达式中,"."表示任何一个字符,如果想要使用普通意义的点字符".",需要使用转义字符""。
在正则表达式中允许使用限定修饰符来限定元字符出现的次数。限定修饰符的用法如下:
限定修饰符 | 意义 | 示例 |
---|---|---|
? | 0次或1次 | A ? |
* | 任意次 | A* |
+ | 一次或多次 | A+ |
正好出现n次 | A | |
至少出现n次 | A | |
出现n~m次 | A |
实例如下:
public class Judge {
public static void main(String[] args) {
String regex="\\w+@\\w+(\\.\\w{2,3})*\\.\\w{2,3}";
//定义正则表达式
String str1="www@fdsihfh.com.com.cn.com";
String str2="fhwoeih@dshfoi.comcn,com.cn";
String str3="165467@163.com.cn";
if (str1.matches(regex))
System.out.println(str1+":合法邮箱地址");
if (str2.matches(regex))
System.out.println(str2+":合法邮箱地址");
if (str3.matches(regex))
System.out.println(str3+":合法邮箱地址");
}
}
运行结果为:
分析:定义的正则表达式依次进行分析,"\w+"表示任意字符出现一次或多次;"@"表示邮箱特有符号;"\w+"表示任意字符出现一次或多次;"(\.\w{2,3})*"表示“.xxx”或“.xx”可以出现任意次;后面再接"\.\w{2,3}",表示“.xxx”或“.xx”。
4. 字符串生成器
创建成功的字符串其长度是固定的,内容不能被改变和编译,使用“+”可以达到附加新字符或字符串的目的,但是会产生一个新的String实例,增加系统的开销。J2SE2.0新增了可变字符序列String-Builder类,大大提高了频繁增加字符串的效率。
实例如下:
public class Jerque {
public static void main(String[] args) {
String str=""; //创建空字符串
//定义对字符串操作的起始时间
long starTime=System.currentTimeMillis();
for(int i=0;i<100000;i++)
{
str=str+i;
}
long endTime=System.currentTimeMillis();
long time=endTime-starTime;
System.out.println("耗时:"+time);
StringBuilder builder=new StringBuilder("");
starTime=System.currentTimeMillis();
for (int j=0;j<100000;j++)
{
builder.append(j);
}
endTime=System.currentTimeMillis();
time=endTime-starTime;
System.out.println("StringBuilder耗时:"+time);
}
}
运行结果图:
该类的常用方法:
append(content):追加字符或字符串
insert(int offset;arg):在指定的索引位置插入字符或字符串
delete(int start,int end):从索引为start(包含)开始删除,直到end(不包含)位置。
实例代码:
public class Jerque {
public static void main(String[] args) {
StringBuilder builder=new StringBuilder("ABC");
System.out.println(builder);
builder.append("456");
System.out.println(builder); //bulider="ABC456"
builder.insert(3,"1");
System.out.println(builder);//builder="ABC1456"
builder.delete(2,4);
System.out.println(builder);//builder="AB456"
}
}