java中String类中常用函数

这篇博客详细介绍了Java中字符串的各种操作,包括charAt()用于获取指定位置的字符,compareTo()进行字典顺序比较,以及equals()和equalsIgnoreCase()判断字符串是否相等。此外,还涉及到了startsWith()和endsWith()检查字符串开头和结尾,indexOf()和lastIndexOf()查找子串位置,substring()截取字符串,toLowerCase()和toUpperCase()转换大小写,trim()去除空格,以及repeat()重复字符串。这些方法对于字符串处理至关重要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        char charAt(int idex); //得到第inde个位置上的字符

         int compareTo(String other); //按照字典顺序,如果当前对象位于other之前,返回正数;若相等返回0;若在之后,则返回一个负数。

         new String(int[] codePoints, int offset, int count); //用数组codePoints中从offset开始的count个元素构造一个字符串

         boolean empty(); boolean blank(); //若当前字符串为空或由空格组成,则返回true,否则返回false

         boolean equals(Object other); //如果当前字符串与other相等,则返回true

         boolean equalsIgnoreCase(Object other); //如果当前字符串与other相等,则返回true                  boolean startsWith(String other); //当前字符串以other开头,则返回true

         boolean endsWith(String other); //当前字符串以other结尾,则返回true

         int indexOf(String str); //获取当前字符串中,str第一次开始的位置 int

        indexOf(String str, int fromIndex); //从下标fromIndex开始匹配,str在当前字符串第一次出现的位置

        int lastIndexOf(String str); //获取当前字符串中,str最后出现的位置

        int lastIndexOf(String str, int fromIndex); //获取当前字符串中,从第fromIndex个元素开始,str最后出现的下标

        int length(); //获取当前字符串长度

        int codePointCount(int startIndex, int endIndex); //获取从第startIndex开始,到第endIndex-1的元素个数

        String substring(int beginIndex); //截取当前字符串,从第beginIndex个字符开始到末尾结束         String substring(int beginIndex, int endIndex); //截取当前字符串,从第beginIndex个字符开始到第endIndex-1个字符结束

        String toLowerCase(); //将当前字符串转换为小写并返回

        String toUpperCase(); //将当前字符串转换为大写并返回

        String trim(); //去掉当前字符串中首尾的空格,并返回

        String repeat(int count); //将当前字符串重复count次,并返回

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值