• JAVA中的String类中的一些常用方法


    目录

    字符串比较方法:

    boolean equals(Object anObject):

     int compareTo(String s):

    int compareToIgnoreCase(String str)

    字符串查找方法:

    char charAt(int index):

    int indexOf(int ch):

     int indexOf(int ch, int fromIndex):

    int indexOf(String str):

    int indexOf(String str, int fromIndex):

    int lastIndexOf(int ch):

    int lastIndexOf(int ch, int fromIndex):

    int lastIndexOf(String str):

    int lastIndexOf(String str, int fromIndex):

    字符串的转化

    String.valueOf():

    String toUpperCase();String toLowerCase():

    char[] toCharArray();

    String(char value[]):

    字符串替换方法:

    String replaceAll(String regex, String replacement):

    String replaceFirst(String regex, String replacement)

    String[] split(String regex):

    String substring(int beginIndex, int endIndex):


    字符串比较方法:

    boolean equals(Object anObject):

    比较两个字符串是否相等,相等返回ture,否则返回false

    1. public static void main(String[] args) {
    2. String a = "asdf";
    3. System.out.println(a.equals("aaa"));
    4. System.out.println(a.equals("asdf"));
    5. }

     int compareTo(String s):

    比较两个字符串是否相等,先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值;如果前k个字符相等(k为两个字符长度最小值),返回两个字符串长度差值。

    1. public static void main(String[] args) {
    2. String a = "asdf";
    3. System.out.println(a.compareTo("aaa"));
    4. System.out.println(a.compareTo("asdf"));
    5. System.out.println(a.compareTo("asd"));
    6. }

    int compareToIgnoreCase(String str)

    忽略字符大小写进行比较,返回值规则为:

    • 先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值
    • 如果前k个字符相等(k为两个字符长度最小值),返回两个字符串长度差值。
    1. public static void main(String[] args) {
    2. String a = "asdf";
    3. System.out.println(a.compareToIgnoreCase("aaa"));
    4. System.out.println(a.compareToIgnoreCase("ASDF"));
    5. System.out.println(a.compareToIgnoreCase("asd"));
    6. }

    字符串查找方法:

    char charAt(int index):

    返回index位置上字符,如果index为负数或者越界,抛出IndexOutOfBoundsException异常。

    1. public static void main(String[] args) {
    2. String a = "asdf";
    3. System.out.println(a.charAt(0));
    4. System.out.println(a.charAt(3));
    5. System.out.println(a.charAt(5));
    6. }

    int indexOf(int ch):

    返回ch第一次出现的位置,没有则返回-1

    1. public static void main(String[] args) {
    2. String a = "asdddf";
    3. System.out.println(a.indexOf('d'));
    4. System.out.println(a.indexOf('a'));
    5. System.out.println(a.indexOf('h'));
    6. }

     int indexOf(int ch, int fromIndex):

    从fromIndex位置开始找 ch 返回第一次出现的位置,没有返回-1

    1. public static void main(String[] args) {
    2. String a = "asdddf";
    3. System.out.println(a.indexOf('d', 3));
    4. System.out.println(a.indexOf('a', 1));
    5. System.out.println(a.indexOf('h',0));
    6. }

    int indexOf(String str):

    返回str第一次出现的位置,没有返回-1

    1. public static void main(String[] args) {
    2. String a = "asdddf";
    3. System.out.println(a.indexOf("dd"));
    4. System.out.println(a.indexOf("ss"));
    5. }

    int indexOf(String str, int fromIndex):

    从fromIndex位置开始找str第一次出现的位置,没有返回-1

    1. public static void main(String[] args) {
    2. String a = "asdddf";
    3. System.out.println(a.indexOf("dd", 3));
    4. System.out.println(a.indexOf("ss", 0));
    5. }

    int lastIndexOf(int ch):

    后往前找返回ch第一次出现的位置,没有返回-1

    1. public static void main(String[] args) {
    2. String a = "asdddf";
    3. System.out.println(a.lastIndexOf('d'));
    4. System.out.println(a.lastIndexOf('s'));
    5. System.out.println(a.lastIndexOf('v'));
    6. }

    int lastIndexOf(int ch, int fromIndex):

    从fromIndex位置开始找,从后往前找ch第一次出现的位置,没有返回-1

    1. public static void main(String[] args) {
    2. String a = "asdddf";
    3. System.out.println(a.lastIndexOf('d', 2));
    4. System.out.println(a.lastIndexOf('d', 3));
    5. System.out.println(a.lastIndexOf('d', 4));
    6. System.out.println(a.lastIndexOf('g', 5));
    7. }

     

    int lastIndexOf(String str):

    从后往前找,返回str第一次出现的位置,没有返回-1

    1. public static void main(String[] args) {
    2. String a = "asdddf";
    3. System.out.println(a.lastIndexOf("dd"));
    4. System.out.println(a.lastIndexOf("as"));
    5. System.out.println(a.lastIndexOf("bv"));
    6. }

    int lastIndexOf(String str, int fromIndex):

    从后往前找str第一次出现的位置,如果此位置的下标不大于fromIndex则返回,否则继续往前找。没有返回-1

    1. public static void main(String[] args) {
    2. String a = "asdddf";
    3. System.out.println(a.lastIndexOf("dd", 3));
    4. System.out.println(a.lastIndexOf("dd", 2));
    5. System.out.println(a.lastIndexOf("dd", 1));
    6. System.out.println(a.lastIndexOf("as", 0));
    7. System.out.println(a.lastIndexOf("bv", 0));
    8. }

    字符串的转化

    数字转字符串

         字符串转整形

    1. public static void main(String[] args) {
    2. String str = "123";
    3. int a1 = Integer.parseInt(str);
    4. long a2 = Long.parseLong(str);
    5. System.out.println(a1+" "+a2);
    6. }

         字符串转浮点型: 

    1. public static void main(String[] args) {
    2. String str = "123";
    3. double a2 = Double.parseDouble(str);
    4. float a3 = Float.parseFloat(str);
    5. System.out.println(a2+" "+a3);
    6. }

     

    String.valueOf():

    所有基本类型值转化为字符串类型

    1. public static void main(String[] args) {
    2. String s1 = String.valueOf(1234);
    3. String s2 = String.valueOf(12.34);
    4. String s3 = String.valueOf(true);
    5. String s4 = String.valueOf('a');
    6. System.out.println(s1);
    7. System.out.println(s2);
    8. System.out.println(s3);
    9. System.out.println(s4);
    10. }

    String toUpperCase();
    String toLowerCase():

    返回一个将原字符串转为大写新串 。

    返回一个将原字符串转为小写新串 。

    1. public static void main(String[] args) {
    2. String s1 = "heLLo";
    3. String s2 = "HEllO";
    4. System.out.println(s1.toUpperCase());
    5. System.out.println(s2.toLowerCase());
    6. }

    char[] toCharArray();
    String(char value[]):

    字符串转为数组原字符串不会受到影响

    数组转为字符串原数组不会受到影响

    1. public static void main(String[] args) {
    2. String s = "hello";
    3. char[] ch = s.toCharArray();
    4. System.out.println(Arrays.toString(ch));
    5. String s2 = new String(ch);
    6. System.out.println(s2);
    7. }

    字符串替换方法:

    String replaceAll(String regex, String replacement):

    替换所有的指定内容

    1. public static void main(String[] args) {
    2. String str = "helloworld" ;
    3. System.out.println(str.replaceAll("l", "O"));
    4. }

    String replaceFirst(String regex, String replacement)

    替换首个内容

    1. public static void main(String[] args) {
    2. String str = "helloworld" ;
    3. System.out.println(str.replaceFirst("l", "O"));
    4. }

    String[] split(String regex):

    将字符串全部拆分

    1. public static void main(String[] args) {
    2. String str = "hello world hello" ;
    3. String[] result = str.split(" ") ; // 按照空格拆分
    4. for(String s: result) {
    5. System.out.println(s);
    6. }
    7. }

    String substring(int beginIndex, int endIndex):

    截取 [ beginIndex ,endIndex ) 范围内的字符串

    1. public static void main(String[] args) {
    2. String str = "helloworld" ;
    3. System.out.println(str.substring(0, 5));
    4. }

  • 相关阅读:
    Windows系统中搭建docker (ubuntu,Docker-desktop)
    设计神经网络的基本原则,如何设计神经网络结构
    聊一聊浏览器打印 - window.print
    【番外篇】C++语法学习笔记
    Git 分支管理流程探讨
    无缝迁移至阿里云RocketMQ:从私有化部署到云端的实用指南
    【C语言刷LeetCode】453. 最小操作次数使数组元素相等(M)
    可学习的FrameField优化建筑物提取CVPR2021
    Lustre和MARTE等建模语言的区别
    【深蓝学院】手写VIO第7章--VINS初始化和VIO系统--笔记
  • 原文地址:https://blog.csdn.net/2302_76339343/article/details/132778482