JAVA String类中的一些常用方法示例详解

 更新时间:2023年10月08日 10:58:29   作者:休息一下…  
在我们的工作中,常常要对一个字符串进行一些操作,这里提供一些常用的方法,常常需要这些方法进行组合处理字符串,这篇文章主要给大家介绍了关于JAVA String类中的一些常用方法,需要的朋友可以参考下

字符串比较方法:

boolean equals(Object anObject):

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

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

int compareTo(String s):

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

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

int compareToIgnoreCase(String str)

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

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

字符串查找方法:

char charAt(int index):

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

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

int indexOf(int ch):

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

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

int indexOf(int ch, int fromIndex):

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

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

int indexOf(String str):

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

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

int indexOf(String str, int fromIndex):

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

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

int lastIndexOf(int ch):

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

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

int lastIndexOf(int ch, int fromIndex):

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

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

int lastIndexOf(String str):

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

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

int lastIndexOf(String str, int fromIndex):

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

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

字符串的转化

数字转字符串

字符串转整形

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

字符串转浮点型: 

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

String.valueOf():

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

    public static void main(String[] args) {
        String s1 = String.valueOf(1234);
        String s2 = String.valueOf(12.34);
        String s3 = String.valueOf(true);
        String s4 = String.valueOf('a');
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);
    }

String toUpperCase();

String toLowerCase():

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

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

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

char[] toCharArray();

String(char value[]):

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

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

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

字符串替换方法:

String replaceAll(String regex, String replacement):

替换所有的指定内容

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

String replaceFirst(String regex, String replacement)

替换首个内容

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

String[] split(String regex):

将字符串全部拆分

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

String substring(int beginIndex, int endIndex):

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

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

总结

到此这篇关于JAVA String类中的一些常用方法的文章就介绍到这了,更多相关JAVA String常用方法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java中的类为什么只能用public修饰?

    java中的类为什么只能用public修饰?

    这篇文章主要介绍了java中的类为什么只能用public修饰,帮助大家更好的理解和使用Java,感兴趣的朋友可以了解下
    2020-12-12
  • mybatis的mapper特殊字符转移及动态SQL条件查询小结

    mybatis的mapper特殊字符转移及动态SQL条件查询小结

    mybatis mapper文件中条件查询符,如>=,<,之类是不能直接写的会报错的需要转移一下,本文给大家介绍了常见的条件查询操作,对mybatis的mapper特殊字符及动态SQL条件查询相关知识感兴趣的朋友一起看看吧
    2021-09-09
  • springcloud集成zookeeper的方法示例

    springcloud集成zookeeper的方法示例

    这篇文章主要介绍了springcloud集成zookeeper的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04
  • 如何将java或javaweb项目打包为jar包或war包

    如何将java或javaweb项目打包为jar包或war包

    本文主要介绍了如何将java或javaweb项目打包为jar包或war包,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • Java 集合框架之List 的使用(附小游戏练习)

    Java 集合框架之List 的使用(附小游戏练习)

    这篇文章主要介绍Java 集合框架中List 的使用,下面文章将围绕Java 集合框架中List 的使用展开话题,并附上一些小游戏练习,需要的朋友可以参考一下
    2021-10-10
  • java实现自动回复聊天机器人

    java实现自动回复聊天机器人

    这篇文章主要为大家详细介绍了java实现自动回复聊天机器人,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-08-08
  • MyBatis Plus 实现多表分页查询功能的示例代码

    MyBatis Plus 实现多表分页查询功能的示例代码

    这篇文章主要介绍了MyBatis Plus 实现多表分页查询功能,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • mybatis-plus返回map自动转驼峰配置操作

    mybatis-plus返回map自动转驼峰配置操作

    这篇文章主要介绍了mybatis-plus返回map自动转驼峰配置操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11
  • 浅谈SpringBoot项目如何让前端开发提高效率(小技巧)

    浅谈SpringBoot项目如何让前端开发提高效率(小技巧)

    这篇文章主要介绍了浅谈SpringBoot项目如何让前端开发提高效率(小技巧),主要介绍了Swagger和Nginx提高效率的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-04-04
  • 深入探究Java @MapperScan实现原理

    深入探究Java @MapperScan实现原理

    之前是直接在Mapper类上面添加注解@Mapper,这种方式要求每一个mapper类都需要添加此注解,麻烦。通过使用@MapperScan可以指定要扫描的Mapper类的包的路径,这篇文章深入探究Java @MapperScan的实现原理
    2023-01-01

最新评论