【Java字符串】String类、字符串操作、格式化字符串、正则表达式

tech2022-09-19  104

1、String类

创建字符串 String str1,str2; str1="My name"; str2="My name is"; 连接字符串 public class Text1 { public static void main(String[] args){ String s1=new String("hello"); String s2=new String("world"); System.out.println(s1+" "+s2); } } 获取字符串信息

(1)获取字符串长度

   使用String类的length()方法可获取声明的字符串对象的长度。

str.length();

   例1:获取字符串长度

public class Text1 { public static void main(String[] args){ String str="My name is fan"; int size=str.length();//str字符串名 System.out.println("字符串长度:"+size); } } 字符串查找

 (1)indexOf()法

str.indexOf(s)

str:任意字符串

s:要搜索的字符串

如果没有检索到,返回值为-1。

   例2:查找n在字符串str中的索引位置。

public class Text1 { public static void main(String[] args){ String str="My name is fan"; int size=str.indexOf("n");//str字符串名 System.out.println("字符串位置:"+size); } }

(2)lastIndexOf()法

  返回指定字符串最后一次出现的索引位置。

str.lastIndexOf(s)

str:任意字符串

s:要搜索的字符串

如果没有检索到,返回值为-1。

获取指定索引字符串 str.charAt(int index)

str:任意字符串

index:整数型,用于指定要返回字符的下标。

   例3:在类中查找第六位字符是。

public class Text1 { public static void main(String[] args){ String str="My name is fan"; char s=str.charAt(6);//str字符串名,查找第六位字符串是? System.out.println("字符串是:"+s); } }

2、字符串操作

获取子字符串

(1)substring(int beginindex)

   返回的是指定的索引位置开始截取直到该字符串结尾的子串。

str.substring(int beginindex)

   例4:截取字符串

public class Text1 { public static void main(String[] args){ String str="My name is fan"; String s=str.substring(3);//str字符串名,截取3到最后 System.out.println("字符串是:"+s); } }

(2)substring(int beginindex,int endindex)

beginindex:开始截取子字符串的索引位置。

endindex:字符串在整个字符串结束的位置。

例5:截取

public class Text1 { public static void main(String[] args){ String str="My name is fan"; String s=str.substring(3,6);//str字符串名,截取3到最后 System.out.println("字符串是:"+s); } } 去除空格 str.trim()

  trim方法返回字符串副本,忽略前导空格、尾部空格。

  例6:

public class Text1 { public static void main(String[] args){ String str=" My name is fan "; int size1=str.length();//str字符串名 int size2=str.trim().length(); System.out.println("字符串长度:"+size1); System.out.println("忽略前导和尾部空格字符串长度:"+size2); } } 字符串替换 str.replace(char oldchar,char newchar)

oldchar:老的字符。

newchar:新的字符。

  例7:把a替换成A

public class Text1 { public static void main(String[] args){ String str=" My name is fan "; String s=str.replace('a','A');//str字符串名 System.out.println("字符串为:"+s); } } 判断字符串开始与结尾

(1)startsWith()方法

   判断当前字符串对象的前缀是否为参数指定字符串。

(2)endsWith()方法

   判断当前字符串是否为以给定的子字符串结束。

   例8:

public class Text1 { public static void main(String[] args){ String str="My name is fan"; boolean s=str.startsWith("My"); boolean s1=str.endsWith("My"); System.out.println("字符串str是否以My开头?"+s); System.out.println("字符串str是否以My结尾?"+s1); } } 判断字符串是否相等

   不能使用==来进行字符串相等的比较。

(1)equals()方法

  如果字符串具有相同的字符和长度,则使用equals()的方法进行比较时,则返回true。(区分大小写)

str.equals(String otherstr)

  str、otherstr是要比较的两个字符串对象。

(2)equalsIgnoreCase()    (不区分大小写)

str.equalsIgnoreCase(String otherstr)

  例9:

public class Text1 { public static void main(String[] args){ String s1=new String("FAN"); String s2=new String("fan"); boolean b1=s1.equals(s2); boolean b2=s1.equalsIgnoreCase(s2); System.out.println("equals法比较:"+b1); System.out.println("equalsIgnoreCase法比较:"+b2); } } 按字典顺序比较两个字符串 str.compareTo(String otherstr)

  str、otherstr要比较的两个字符串。

字符串大小写转换

(1)toLowerCase()方法(数字和非字符串不受影响)

  将String转换成小写,str要转换的字符。

str.toLowerCase()

(2)toUpperCase()方法(数字和非字符串不受影响)

  将String转换成大写。

str.toUpperCase()

  例10:

public class Text1 { public static void main(String[] args){ String s1=new String("FAN"); String s2=new String("fan"); String s3=s1.toLowerCase(); String s4=s2.toUpperCase(); System.out.println("原始字符串"+s1+"小写转换"+s3); System.out.println("原始字符串"+s2+"大写转换"+s4); } } 字符串分割

(1)split(String sign)

  给定的分割符对字符串进行拆分。

3、格式化字符串

日期格式化

  例11:

import java.util.Date;//导入类 public class Text1 { public static void main(String[] args){ Date date=new Date(); String year=String.format("%tY",date); String month=String.format("%tB",date); String day=String.format("%td",date); System.out.println("今年是:"+year+"年"); System.out.println("现在是:"+month); System.out.println("今天是:"+day); } } 时间格式化

  例12:

import java.util.Date;//导入类 public class Text1 { 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+"秒"); } } 格式化常见的日期时间组合

例13:

import java.util.Date;//导入类 public class Text1 { 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); } } 全部的时间信息:星期四 九月 03 11:40:01 CST 2020 年-月-日格式:2020-09-03 Process finished with exit code 0 常规类型格式化

4、使用正则表达式

5、字符串生成器

利用StringBuilder类中的方法可动态的执行添加、删除、和插入等字符串的操作。

(1)append(content)

  content表示要追加到字符串生成器中的内容。

(2)insert(int offset arg)

offset:字符生成器的位置。

arg:将插入至字符生成器的位置。

例14:

public class Text1 { public static void main(String[] args){ StringBuilder bf=new StringBuilder("hello"); bf.insert(5,"world"); System.out.println(bf.toString()); } }

(3)delete(int start,int end)

start:将要删除的字符串的起始位置。

end:将要删除的字符串的终点位置。

例15:

public class Text1 { public static void main(String[] args){ StringBuilder bf=new StringBuilder("hello"); bf.delete(2,3); System.out.println(bf.toString()); } } helo

 

最新回复(0)