在我们学习java的过程当中常常对一个字符串进行操作,而字符串的应用又非常的广泛,我们的Java中十分人性化的提供了引用类型String让我们方便进行对字符串的操作,既然String是类那么它当中肯定有各种方法方便我们的编程,下面我就为大家介绍几种String类中常用的几种方法吧
一.字符串的构造
1.使用常量串构造
public static void main(String[] args) {
String str="hello word";
System.out.println(str);
}
2.使用new String对象构造
public static void main(String[] args) {
String str=new String("hello word");
System.out.println(str);
}
3.使用字符数组进行构造
public static void main(String[] args) {
char[] array = {'h','e','l','l','o','w','o','r','d'};
String str = new String(array);
System.out.println(str);
}
二.字符串的比较
1.使用 ==比较是否引用同一个对象
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 10;
System.out.println(a == b); // false
System.out.println(a == c); // true
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = new String("world");
String s4 = s1;
System.out.println(s1 == s2); // false
System.out.println(s2 == s3); // false
System.out.println(s1 == s4); // tru
}
注意:对于内置类型,==比较的是变量中的值;对于引用类型 ==比较的是引用的地址
2.使用boolean equals()比较
public static void main(String[] args) {
String str1 = new String("hello");
String str2 =new String( "word");
String str3 = new String("hello");
System.out.println(str1.equals(str2));
System.out.println(str1.equals(str3));
}
equals比较:String对象中的逐个字符,虽然str1与str3引用的不是同一个对象,但是两个对象中放置的内容相同,因此输出true, s1与s2引用的不是同一个对象,而且两个对象中内容也不同,因此输出false
- 使用boolean equalsIgnoreCase()比较
public static void main(String[] args) {
String str1 = new String("HELLO");
String str2 = new String("hello");
System.out.println(str1.equals(str2));
System.out.println(str1.equalsIgnoreCase(str2));
}
equalsIgnoreCase()比较与equals比较比较方式相同,只不过equalsIgnoreCase()比较的时候忽略要比较字符串的大小写比较
4.使用int compareTo(String str )比较
public static void main(String[] args) {
String str1 = new String("abc");
String str2 = new String("ABC");
String str3=new String("abcd");
String str4=new String("abc");
System.out.println(str1.compareTo(str2));
System.out.println(str1.compareTo(str3));
System.out.println(str1.compareTo(str4));
}
compareTo( )比较:按字典顺序比较两个字符串,返回值是整型,它是先比较对应字符的大小(ASCII码顺序),如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值,如果第一个字符和参数的第一个字符相等,则以第二个字符和参数的第二个字符做比较,以此类推,直至比较的字符或被比较的字符有一方,如果参数字符串等于此字符串,则返回值 0,如果此字符串小于字符串参数,则返回一个小于 0 的值,如果此字符串大于字符串参数,则返回一个大于 0 的值
5.使用int compareToIgnoreCase(String str)比较
public static void main(String[] args) {
String str1 = new String("abc");
String str2 = new String("ABC");
String str3=new String("abc");
System.out.println(str1.compareToIgnoreCase(str2));
System.out.println(str1.compareTo(str3));
}
compareToIgnoreCase与compareTo方式相同,只不过compareToIgnoreCase忽略大小写比较
三.字符串的查找
1.使用char charAt(int index)查找
public static void main(String[] args) {
String str1 = new String("hello word");
System.out.println(str1.charAt(6));
}
charAt()查找:返回index位置上字符,如果index为负数或者越界,抛出IndexOutOfBoundsException异常
2.使用 int indexOf(String str) 查找
public static void main(String[] args) {
String str1 = new String("hello word");
System.out.println(str1.indexOf("e"));
System.out.println(str1.indexOf("s"));
}
indexOf(int ch)查找:返回ch第一次出现的位置,没有返回-1
3.使用int indexOf(String str, int fromIndex)查找
public static void main(String[] args) {
String str1 = new String("hello word");
System.out.println(str1.indexOf("o",5));
}
indexOf(String str, int fromIndex)查找:返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1
4.使用int indexOf(int ch)查找
public static void main(String[] args) {
String str1 = new String("123456");
System.out.println(str1.indexOf("3"));
}
int indexOf(int ch)查找 :返回ch第一次出现的位置,没有返回-1
5.使用int lastIndexOf(String ch) 查找
public static void main(String[] args) {
String str = "Hello Word";
int res1 = str.lastIndexOf("o");
int res2 = str.indexOf("s");
System.out.println(res1);
System.out.println(res2);
}
lastIndexOf(String ch) 查找:从后往前找,返回ch第一次出现的位置,没有返回-1
6.使用int lastIndexOf(String str, int fromIndex)查找
public static void main(String[] args) {
String str = "Hollo Word";
System.out.println(str.lastIndexOf("o",6));
System.out.println(str.lastIndexOf("s",6));
}
lastIndexOf(String str, int fromIndex)查找:从fromIndex个字符中找,返回指定字符在此字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1
7.使用contains()查找
public static void main(String[] args) {
String str = "Hello Word";
System.out.println(str.contains("ll"));
System.out.println(str.contains("s"));
}
contains()查找:判断字符串中是否有指定字符
8.使用endsWith()查找
public static void main(String[] args) {
String str = "Hello Word";
System.out.println(str.endsWith("d"));
System.out.println(str.endsWith("s"));
}
endsWith()查找:查找字符串是否以指定的后缀结束
9.使用startsWith(String prefix)查找
public static void main(String[] args) {
String str = "Hello Word";
System.out.println(str.startsWith("H"));
System.out.println(str.startsWith("h"));
}
startsWith(String prefix)查找:查找字符串是否以指定的字符开始
10.使用 startsWith(String prefix, int offIndex)查找
public static void main(String[] args) {
String str = "Hello Word";
System.out.println(str.startsWith("H",0));
System.out.println(str.startsWith("h",0));
System.out.println(str.startsWith("e",1));
System.out.println(str.startsWith("l",2));
System.out.println(str.startsWith("l",3));
}
startsWith(String prefix, int offIndex)查找:查找int offIndex下标处是否为String prefix字符
四.字符串转
1.使用toCharArray() 转换
String Str = new String("hello");
char[]ch=Str.toCharArray();
for (int i=0;i<ch.length;i++){
System.out.println(ch[i]);
}
System.out.println(ch);
System.out.println( Str.toCharArray() );
}
toCharArray() 转换:将字符串转换为字符数组
2.数值和字符串转化
数值转化为字符串
public static void main(String[] args) {
String str1=String.valueOf(1234);
String str2=String.valueOf(12.34);
String str3=String.valueOf(true);
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
}
字符串转化为数字(使用包装类自带的方法将字符串转化为数字)
public static void main(String[] args) {
int data1 = Integer.parseInt("1234");
double data2 = Double.parseDouble("12.34");
System.out.println(data1);
System.out.println(data2);
}
3.格式化转化输出
public static void main(String[] args) {
String str=String.format("%d-%d-%d",2022,11,25);
System.out.println(str);
}
五.字符串替换
1.使用String replaceAll(String regex, String replacement) 替换
public static void main(String[] args) {
String str=new String("hello word");
System.out.println(str.replaceAll("l","a"));
}
String replaceAll(String regex, String replacement) 替换:将字符串中的 l全部替换为 a
2.使用String replaceFirst(String regex, String replacement) 替换
public static void main(String[] args) {
String str=new String("hello word");
System.out.println(str.replaceFirst("l","a"));
}
String replaceFirst(String regex, String replacement) 替换:将字符串中第一个 l替换为 a
3.使用replace()替换
public static void main(String[] args) {
String str=new String("hello word");
System.out.println(str.replace("hello","你好!"));
}
replace()替换:将指定的字符串替换为目标字符串
六.字符串的拆分
- 使用split(String regex)拆分
public static void main(String[] args) {
String str1 = "认真1好好学习1天天向上";
String[] strs = str1.split("1");
for (String s : strs) {
System.out.println("目标----" + s);
}
}
split(String regex)拆分:将字符串全部拆分
2.使用split(String regex, int limit)拆分
public static void main(String[] args) {
String str1 = "认真1好好学习1天天向上";
String[] strs = str1.split("1",2);
for (String s : strs) {
System.out.println("目标----" + s);
}
}
split(String regex, int limit)拆分:将字符串以指定的格式,拆分为limit组
注意事项:
- 字符"|“,”*“,”+"都得加上转义字符,前面加上 “\” .
- 而如果是 “” ,那么就得写成 “\\” .
- 如果一个字符串中有多个分隔符,可以用"|"作为连字符
七.字符串的截取
1.使用 substring(int beginIndex)截取
public static void main(String[] args) {
String str1 = "abcdefghi";
String str2 = str1.substring(2);//截取下标为2(含2)
System.out.println(str2);
}
substring(int beginIndex)截取:从指定索引截取到结尾
2.使用substring(int beginIndex, int endIndex)截取
public static void main(String[] args) {
String str1 = "abcdefghi";
String str2 = str1.substring(2,5);//从下标为2的字符开始截取,截取到下标为5的字符(含2不含5)
System.out.println(str2);
}
substring(int beginIndex, int endIndex)截取:截取部分内容
八.其他方法
- 字符串长度(length)
public static void main(String[] args) {
String str1 = new String("abcdefghi");
System.out.println(str1.length());
}
2.小写转大写(toUpperCase())
public static void main(String[] args) {
String str1 = new String("abcdefghi");
System.out.println(str1.toUpperCase());
}
3.大写转小写(toLowerCase(Locale.ROOT))
public static void main(String[] args) {
String str1 = new String("ABCDEFGHI");
System.out.println(str1.toLowerCase(Locale.ROOT));
}
- 去掉前后空格(中间保留)(trim())
public static void main(String[] args) {
String str1 = new String(" Hello Word ");
System.out.println(str1);
System.out.println(str1.trim());
}
- 字符串连接(一般用+) (concat())
public static void main(String[] args) {
String str1 = new String("Helle");
String str2=new String("Word");
System.out.println(str1.concat(str2));
System.out.println(str1+str2);
}
- 判断是否是空字符串( boolean isEmpty())
public static void main(String[] args) {
String str1 = new String("Helle Word");
System.out.println(str1.isEmpty());
}