2024年最新Java——String类常见方法_string[] a 判断长度,C C++开发必会技术

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

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

使用方法:

   public static void main(String[] args) {
        String s1 = new String("hello"); //h:104
        String s2 = new String("aello"); //a:97
        int ret1 = s1.compareTo(s2); //104 - 97 = 7
        System.out.println(ret1);

        String s3 = new String("abc"); //长度为3
        String s4 = new String("ab");  //长度为2
        int ret2 = s3.compareTo(s4);    // 3 - 2 = 1
        System.out.println(ret2);
    }

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

返回值:与equal一样

    public static void main(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("Hello");
        System.out.println(s1.equalsIgnoreCase(s2));

        String s3 = new String("hello");
        String s4 = new String("aello");
        System.out.println(s3.equalsIgnoreCase(s4));

    }

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

返回值:与compareTo一样

  public static void main(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("Hello");
        System.out.println(s1.compareToIgnoreCase(s2));

        String s3 = new String("hello");
        String s4 = new String("aello");
        System.out.println(s3.compareToIgnoreCase(s4));

    }


字符串查找

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

返回值:同上

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


转化

其他类型转为字符串:

数字转字符串
    String s1 = String.valueOf(100);
        String s2 = String.valueOf(3.1415);
布尔类型转字符串
      String s3 = String.valueOf(true);
        String s4 = String.valueOf(false);
实例化对象转字符串
class Student{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public static void main(String[] args) {
        String s1 = String.valueOf(new Student("张三",18));
    }
}

字符串转为其他类型:

字符串转数字

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

字符串转布尔类型

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

字符串与数组之间转化:

字符串转数组
  public static void main(String[] args) {
        String s1 = "hello";
        char[] ch = s1.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            System.out.print(ch[i]);
        }
    }

数组转字符串
    public static void main(String[] args) {
        char[] ch = {'h','e','l','l','o'};
        String s1 = new String(ch);
        System.out.println(s1);
    }

大小写转化:

小写转大写
  public static void main(String[] args) {
        String s1 = "hello";
        System.out.println(s1.toUpperCase());
    }

小写转大写
  public static void main(String[] args) {
        String s1 = "HELLO";
        System.out.println(s1.toLowerCase());
    }

格式化:

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


字符串替换

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

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

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

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


字符串拆分

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

将字符串全部拆分

返回值:数组

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

将字符串拆分成指定组

返回值:数组

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

以下是特殊的:

以 . 号拆分

需要加两个转义字符:

 public static void main(String[] args) {
        String s1 = "192.168.1.1";
        String[] s2 = s1.split("\\.");
        for (String s: s2) {
            System.out.println(s);
        }
    }

以 \ 号拆分

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

  public static void main(String[] args) {
        String s1 = "2022\\08\\12";
        System.out.println("原来是:" + s1);
        String[] s2 = s1.split("\\\\");
        System.out.println("现在是:");
        for (String s: s2) {
            System.out.println(s);
        }
    }

多次拆分(分步拆分)

    public static void main(String[] args) {
        String s1 = "mp.csdn.net/2022";
        System.out.println("原来是:" + s1);
        System.out.println("现在是:");
        String[] s2 = s1.split("/");
        for (String s: s2) {
            String[] ss = s.split("\\.");
            for (String temp: ss) {
                System.out.println(temp);
            }
        }
    }


字符串截取

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

从指定位置截取到结尾

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

" + s1);

    System.out.println("现在是:");
    String[] s2 = s1.split("/");
    for (String s: s2) {
        String[] ss = s.split("\\.");
        for (String temp: ss) {
            System.out.println(temp);
        }
    }
}

![](https://img-blog.csdnimg.cn/57af6c532b87468e9fa13238fb444442.png)





字符串截取

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

从指定位置截取到结尾

[外链图片转存中…(img-cqXsj3X0-1715659013007)]
[外链图片转存中…(img-L4tXbBds-1715659013007)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 11
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值