0


Java——String类常见方法


本篇文章清楚总结了一些常用String类中包含的方法,熟练运用后即可在题海中节省大量时间


字符串构造

赋值字符串构造

  1. //通过字符串构造
  2. String str1 = "hello";
  3. System.out.println("str1 = " + str1);

通过new对象构造

  1. //通过new对象构造
  2. String str2 = new String("hello");
  3. System.out.println("str2 = " + str2);

通过字符数组构造

  1. //通过字符数组构造
  2. char[] arr = {'h','e','l','l','o'};
  3. String str3 = new String(arr);
  4. System.out.println("str3 = " + str3);


字符串数组本质

因为String属于引用类型,所以str内存储的并不是字符串内容本身。

我们进入String源码可以看到str数组内部存储的是对象的地址字符串内容其实是存在**字符数组value[ ]**中的:

调试起来看:

这是它的简化内存布局图:


字符串长度

求字符串长度

返回值:字符串或数组长度

  1. String str1 = "hello";
  2. System.out.println(str1.length());

判断字符串长度是否为0

为0返回ture;

不为0返回false

  1. String str1 = "hello";
  2. System.out.println(str1.isEmpty());

String对象比较

==的使用

内置类型:比较两个变量的值是否相等;

引用类型:比较的是引用的地址.

返回值:相同返回true,否则返回false.

  1. public static void main(String[] args) {
  2. int a = 10;
  3. int b = 10;
  4. System.out.println(a == b);
  5. String s1 = new String("hello");
  6. String s2 = new String("hello");
  7. System.out.println(s1 == s2);
  8. }

具体的在下文中字符串常量池中。

boolean equeal(Object anObject) 方法:按照字典序比较是否相同

返回值:相同返回true,否则返回false.

我们知道所有类都继承着Object这个父类,虽然Object中的equal方法是按照==来比较的,但是在

String类中重写了equal方法,这样就可以按照字面值来比较了:

  1. public static void main(String[] args) {
  2. String s1 = new String("hello");
  3. String s2 = new String("hello");
  4. System.out.println(s1.equals(s2));
  5. }

int compareTo(String s) 方法:按照字典序比较大小

通过comparaTo源码可以看到:

返回值是int类型:

如果有不同的字符,返回字符的ASCII值差值;

如果没有不同的字符,返回长度的差值.

  • 使用方法:*
  1. public static void main(String[] args) {
  2. String s1 = new String("hello"); //h:104
  3. String s2 = new String("aello"); //a:97
  4. int ret1 = s1.compareTo(s2); //104 - 97 = 7
  5. System.out.println(ret1);
  6. String s3 = new String("abc"); //长度为3
  7. String s4 = new String("ab"); //长度为2
  8. int ret2 = s3.compareTo(s4); // 3 - 2 = 1
  9. System.out.println(ret2);
  10. }

boolean equalsIgnoreCase(Object anObject) 方法:忽略大小写比较是否相等

返回值:与equal一样

  1. public static void main(String[] args) {
  2. String s1 = new String("hello");
  3. String s2 = new String("Hello");
  4. System.out.println(s1.equalsIgnoreCase(s2));
  5. String s3 = new String("hello");
  6. String s4 = new String("aello");
  7. System.out.println(s3.equalsIgnoreCase(s4));
  8. }

int compareToIgnoreCase(String s) 方法:忽略大小比较字符串大小

返回值:与compareTo一样

  1. public static void main(String[] args) {
  2. String s1 = new String("hello");
  3. String s2 = new String("Hello");
  4. System.out.println(s1.compareToIgnoreCase(s2));
  5. String s3 = new String("hello");
  6. String s4 = new String("aello");
  7. System.out.println(s3.compareToIgnoreCase(s4));
  8. }


字符串查找

char charAt(int index) 方法:得到指定位置的字符

  1. public static void main(String[] args) {
  2. String s1 = "hello world";
  3. char ch = s1.charAt(4);
  4. System.out.println("第四个字符是:" + ch);
  5. }

如果指定位置为负或者越界:抛出 IndexOutOfBoundsException异常。

int indexOf(int ch) 方法:得到指定字符首次出现的位置

返回值:存在则返回第一次出现的位置;否则返回-1;

  1. public static void main(String[] args) {
  2. String s1 = "hello world";
  3. int ch = s1.indexOf('l');
  4. System.out.println("字符o第一次出现的位置是:" + ch);
  5. }

int indexOf(int ch, int fromIndex) 方法:从fromIndex位置开始找字符ch第一次出现的位置

返回值:存在则返回在整个字符串中的位置;否则返回-1;

  1. public static void main(String[] args) {
  2. String s1 = "hello world";
  3. int ch = s1.indexOf('o',5);//位置5处是空格
  4. System.out.println("从位置5开始字符o第一次出现的位置是:" + ch);
  5. }

int indexOf(String s) 方法:得到字符串s第一次出现的位置

返回值:存在返回首字符第一次出现的位置;否则返回-1;

  1. public static void main(String[] args) {
  2. String s1 = "hello world";
  3. int ch = s1.indexOf("llo");
  4. System.out.println("字符llo第一次出现的位置是:" + ch);
  5. }

int indexOf(String s, int fromIndex) 方法:得到字符串s从fromIndex位置开始第一次出现的位置

返回值:存在则返回在整个字符串中的位置;否则返回-1;

  1. public static void main(String[] args) {
  2. String s1 = "hello hello world";
  3. int ch = s1.indexOf("llo",4);
  4. System.out.println("从位置4开始字符llo第一次出现的位置是:" + ch);
  5. }

int lastIndexOf(int ch) 方法:从后往前找,得到字符ch第一次出现的位置

返回值:存在则返回从后往前第一次出现的位置;否则返回-1

  1. public static void main(String[] args) {
  2. String s1 = "hello hello world"; //第一个o在4位置 第二个o在10位置 第三个o在13位置
  3. int ch = s1.lastIndexOf('o');
  4. System.out.println("从后往前找,字符o第一次出现的位置是:" + ch);
  5. }

int lastIndexOf(int ch, int fromIndex) 方法:从fromIndex位置开始,从后往前找,得到ch第一次的位置

返回值:存在则返回所在位置;否则返回-1

  1. public static void main(String[] args) {
  2. String s1 = "hello hello world"; //第一个o在4位置 第二个o在10位置 第三个o在13位置
  3. int ch = s1.lastIndexOf('o',12);
  4. System.out.println("从12位置起,从后往前找,字符o第一次出现的位置是:" + ch);
  5. }

int lastIndexOf(String s) 方法:从后往前找,得到字符串s在整个字符串中第一次出现的位置

返回值:找到返回s的首元素在整个字符串中第一次出现的位置;否则返回-1

  1. public static void main(String[] args) {
  2. String s1 = "hello hello world";
  3. int ch = s1.lastIndexOf("llo");
  4. System.out.println("从后往前找,字符串llo第一次出现的位置是:" + ch);
  5. }

int lastIndexOf(String s, int fromIndex) 方法:从fromIndex位置开始,从后往前找,得到字符串第一次的位置

返回值:同上

  1. public static void main(String[] args) {
  2. String s1 = "hello hello world";
  3. int ch = s1.lastIndexOf("llo",7);
  4. System.out.println("从位置7起,从后往前找,字符串llo第一次出现的位置是:" + ch);
  5. }


转化

其他类型转为字符串:

数字转字符串

  1. String s1 = String.valueOf(100);
  2. String s2 = String.valueOf(3.1415);

布尔类型转字符串

  1. String s3 = String.valueOf(true);
  2. String s4 = String.valueOf(false);

实例化对象转字符串

  1. class Student{
  2. public String name;
  3. public int age;
  4. public Student(String name, int age) {
  5. this.name = name;
  6. this.age = age;
  7. }
  8. public static void main(String[] args) {
  9. String s1 = String.valueOf(new Student("张三",18));
  10. }
  11. }

字符串转为其他类型:

字符串转数字

  1. int num1 = Integer.parseInt("123321");
  2. double num2 = Double.parseDouble("3.1415926");

字符串转布尔类型

  1. boolean flag1 = Boolean.parseBoolean("true");
  2. boolean flag2 = Boolean.parseBoolean("false");

字符串与数组之间转化:

字符串转数组

  1. public static void main(String[] args) {
  2. String s1 = "hello";
  3. char[] ch = s1.toCharArray();
  4. for (int i = 0; i < ch.length; i++) {
  5. System.out.print(ch[i]);
  6. }
  7. }

数组转字符串

  1. public static void main(String[] args) {
  2. char[] ch = {'h','e','l','l','o'};
  3. String s1 = new String(ch);
  4. System.out.println(s1);
  5. }

大小写转化:

小写转大写

  1. public static void main(String[] args) {
  2. String s1 = "hello";
  3. System.out.println(s1.toUpperCase());
  4. }

小写转大写

  1. public static void main(String[] args) {
  2. String s1 = "HELLO";
  3. System.out.println(s1.toLowerCase());
  4. }

格式化:

  1. public static void main(String[] args) {
  2. String s = String.format("%d - %d - %d",2022,8,12);
  3. System.out.println(s);
  4. }


字符串替换

Sting replaceAll() 方法:用给定字符串替换所有指定的字符串

  1. public static void main(String[] args) {
  2. String s1 = "hello hello World";
  3. String s2 = s1.replaceAll("ll","a");
  4. System.out.println(s2);
  5. }

String replaceFirst() 方法:只替换原字符串中第一次出现的指定字符串

  1. public static void main(String[] args) {
  2. String s1 = "hello hello World";
  3. String s2 = s1.replaceFirst("ll","a");
  4. System.out.println(s2);
  5. }


字符串拆分

可以将字符串按照字符串内已有符号进行分割;

将字符串全部拆分

返回值:数组

  1. public static void main(String[] args) {
  2. String s1 = "hello@hello@world";
  3. String[] s2 = s1.split("@");
  4. for (String s: s2) {
  5. System.out.println(s);
  6. }
  7. }

将字符串拆分成指定组

返回值:数组

  1. public static void main(String[] args) {
  2. String s1 = "hello@hello@world";
  3. String[] s2 = s1.split("@",2);
  4. for (String s: s2) {
  5. System.out.println(s);
  6. }
  7. }

以下是特殊的:

以 . 号拆分

需要加两个转义字符:

  1. public static void main(String[] args) {
  2. String s1 = "192.168.1.1";
  3. String[] s2 = s1.split("\\.");
  4. for (String s: s2) {
  5. System.out.println(s);
  6. }
  7. }

以 \ 号拆分

因为\表示一个\,所以需要在前面再加两个\:

  1. public static void main(String[] args) {
  2. String s1 = "2022\\08\\12";
  3. System.out.println("原来是:" + s1);
  4. String[] s2 = s1.split("\\\\");
  5. System.out.println("现在是:");
  6. for (String s: s2) {
  7. System.out.println(s);
  8. }
  9. }

多次拆分(分步拆分)

  1. public static void main(String[] args) {
  2. String s1 = "mp.csdn.net/2022";
  3. System.out.println("原来是:" + s1);
  4. System.out.println("现在是:");
  5. String[] s2 = s1.split("/");
  6. for (String s: s2) {
  7. String[] ss = s.split("\\.");
  8. for (String temp: ss) {
  9. System.out.println(temp);
  10. }
  11. }
  12. }


字符串截取

从一个完整的字符串中截取部分;

从指定位置截取到结尾

返回值:字符串

  1. public static void main(String[] args) {
  2. String s1 = "hello world";
  3. String s2 = s1.substring(6);
  4. System.out.println(s2);
  5. }

截取指定长度

长度是(起始位置,终点位置):左闭右开

  1. public static void main(String[] args) {
  2. String s1 = "hello world";
  3. String s2 = s1.substring(6,10);
  4. System.out.println(s2);
  5. String s3 = s1.substring(6,11);
  6. System.out.println(s3);
  7. }

去掉字符串左右空格,保留中间空格

trim() 可以字符串开头和结尾的空格、制表符、回车等;

  1. public static void main(String[] args) {
  2. String s1 = " hello world \t " +
  3. " ";
  4. String s2 = " " +
  5. " tomorrow ";
  6. System.out.println( "[" + s1.trim() + s2.trim() + "]");
  7. }

标签: jvm 数据结构 java

本文转载自: https://blog.csdn.net/m0_65190367/article/details/126308342
版权归原作者 @Main. 所有, 如有侵权,请联系我们删除。

“Java&mdash;&mdash;String类常见方法”的评论:

还没有评论
关于作者
...
overfit同步小助手
文章同步
文章导航
字符串构造赋值字符串构造通过new对象构造通过字符数组构造字符串数组本质字符串长度求字符串长度判断字符串长度是否为0String对象比较==的使用boolean equeal(Object anObject) 方法:按照字典序比较是否相同int compareTo(String s) 方法:按照字典序比较大小boolean equalsIgnoreCase(Object anObject) 方法:忽略大小写比较是否相等int compareToIgnoreCase(String s) 方法:忽略大小比较字符串大小字符串查找char charAt(int index) 方法:得到指定位置的字符int indexOf(int ch) 方法:得到指定字符首次出现的位置int indexOf(int ch, int fromIndex) 方法:从fromIndex位置开始找字符ch第一次出现的位置int indexOf(String s) 方法:得到字符串s第一次出现的位置int indexOf(String s, int fromIndex) 方法:得到字符串s从fromIndex位置开始第一次出现的位置int lastIndexOf(int ch) 方法:从后往前找,得到字符ch第一次出现的位置int lastIndexOf(int ch, int fromIndex) 方法:从fromIndex位置开始,从后往前找,得到ch第一次的位置int lastIndexOf(String s) 方法:从后往前找,得到字符串s在整个字符串中第一次出现的位置int lastIndexOf(String s, int fromIndex) 方法:从fromIndex位置开始,从后往前找,得到字符串第一次的位置转化其他类型转为字符串:字符串转为其他类型:字符串与数组之间转化:大小写转化:格式化:字符串替换Sting replaceAll() 方法:用给定字符串替换所有指定的字符串String replaceFirst() 方法:只替换原字符串中第一次出现的指定字符串字符串拆分将字符串全部拆分将字符串拆分成指定组以 . 号拆分以 \ 号拆分多次拆分(分步拆分)字符串截取从指定位置截取到结尾截取指定长度去掉字符串左右空格,保留中间空格