Java语言中字符串的基本操作:
1.获取字符串的长度
获取字符串的长度可以使用String类的length()方法,其定义如下:
public int length()
此方法返回字符创的字符个数,示例:
String s = "Hello World";
2.转换字符串的大小写
public String toLowerCase() //把字符串内容转换为小写
public String toUpperCase() //把字符串内容转换为大写
示例:
String s = "Hello World";
System.out.println(s.toLowerCase()); //输出:hello world
System.out.println(s.toUpperCase()); //输出:HELLO WORLD
3.去掉字符串的首尾空格
public String trim()
示例:
String s1 = " Hello World ";
String s2 = "Hello World";
System.out.println(s1.equals(s2)); //输出false
System.out.println(s1.trim().equals(s2)); //输出true
4.比较字符串的大小
4.1使用equals()方法比较字符串
public boolean equals(Object str)
示例:
String s1 = "Hello World";
String s2 = "Hello World";
String s3 = "Hello world";
System.out.println(s1.equals(s2)); //输出true
System.out.println(s2.equals(s3)); //输出false
4.2使用equalsIgnoreCase()方法比较字符串
public boolean equalsIgnoreCase() //该方法忽略大小写
示例:
String s1 = "Hello World";
String s2 = "hello world";
System.out.println(s1.equalsIgnoreCase(s2)); //输出true
5.字符串查找
5.1在字符串中获取匹配字符(串)的索引值
5.1.1 indexOf()方法
indexOf()方法有四种重载形式,如下:
public int indexOf(int ch) //返回在指定字符的第一个发生的字符串中的索引。
public int indexOf(int ch, int fromIndex) //返回在指定字符的第一个发生的字符串中的索引,在指定的索引处开始搜索。
public int indexOf(String str) //返回此字符串的指定子字符串中第一个出现的索引
public int indexOf(String str, int fromIndex) //返回此字符串的指定子字符串中第一个出现在索引中,从指定索引处开始。
5.1.2 lastIndexOf方法
lastIndexOf()与indexOf()类似,不同之处在于lastIndexOf()是从后往前搜索lastIndexOf()方法也有四种重载形式,如下:
public int indexOf(int ch)
public int indexOf(int ch, int fromIndex)
public int indexOf(String str)
public int indexOf(String str, int fromIndex)
5.1.3 获取指定索引位置的字符
public char charAt(int index)
示例:
String s = "Hello World";
char ch = s.charAt(7);
System.out.println(ch); //输出o
6.字符串分割
String类提供的split()方法,可以按指定的分隔符对目标字符串进行分割,分割后的内容存放在字符串数组中,该方法主要有如下两种重载形式:
public String [] split(String regex, int limit)
public String [] split(String regex)
其中,regex为指定的分割符,可以是任意字符串,也可以是正则表达式;limit表示分割后生成的字符串的限制个数,如果不指定,则表示不限制,直到将整个目标字符串完全分割为止:
示例:
String s = "H;e;l;l;o;w;o;r;l;d";
String [] array1 = s.split(";");
String [] array2 = s.split(";",6);
System.out.println("array1数组中的元素如下:");
for(int i = 0; i<array1.length; i++){
System.out.print(array1[i]+"\t");
}
System.out.println("\narray2数组中的元素如下:");
for(int i = 0; i<array2.length; i++){
System.out.print(array2[i]+"\t");
}
上述代码的执行结果是:
array1数组中的元素如下:
H e l l o w o r l d
array2数组中的元素如下:
H e l l o w;o;r;l;d
7.字符串截取
String类的subString()方法用于对字符串进行截取,该方法主要有如下两种重载形式:
public String subString(int beginIndex)
public String subString(int beginIndex, int endIndex)
示例:
String s = "Hello World";
String s1 = s.subString(6); //返回索引为6到末尾的字符串
String s2 = s.subString(2,5); //返回索引为2~5之间的字符组成的字符串,包括索引为2的字符
System.out.println(s1); //输出World
System.out.println(s2); //输出llo
8.字符串替换
8.1 使用replace()方法
public String replace(char oldChar, char newChar) //replace()方法会将所有oldChar替换成newChar
示例:
String s = "Hello World";
String news = s.replace("World","Java");
System.out.println(news); //输出:Hello Java
8.2使用replaceFirst()方法
public String replaceFirst(String regex, String replacement)
//replacement()方法用于将目标字符串匹配指定的正则表达式的第一个子字符串替换为新的字符串。
示例:
String s = "Hello World Hello World";
String news = s.replaceFirst("World","Java");
System.out.println(news); //输出Hello Java Hello World
8.3使用replaceAll()方法
public String replaceAll(String regex, String replacement)
//replacement()方法用于将目标字符串匹配指定的正则表达式的所有子字符串替换为新的字符串。
示例:
String s = "Hello World Hello World";
String news = s.replaceFirst("World","Java");
System.out.println(news); //输出Hello Java Hello Java
9.字符串连接
在Java中,字符串的连接方式有两种:一种是使用“+”运算符来对多个该字符串进行连接,另一种是使用String类的concat()方法。先介绍使用concat()方法进行连接。
public String concat(String str)
//其中,参数str表示连接到字符串后面的字符串。注:String类的concat()方法一次只能连接两个字符串,如果需要连接多个字符串,需要多次调用concat()方法。
示例:
String str = "Hello!";
String name = "My name is Michael Scofield!";
System.out.println(str.concat(name)); //输出:Hello!My name is Michael Scofield!