String常用方法

1.字符串长度:length()

public int length();//返回该字符串的长度

public class StringTest {
    public static void main(String[] args) {
        String str = new String("我是豆豆");

        System.out.println(str.length());
    }
}
//结果:4
2.字符串某一位置字符:charAt()

public char charAt(int index)//返回字符串中指定位置的字符

注意:字符串中第一个字符索引是0,最后一个是length()-1

public class StringTest {
    public static void main(String[] args) {
        String str = new String("啦啦啦,我是豆豆");

        System.out.println(str.charAt(3));
    }
}
//结果:,
3.提取字串:substring()
  • public String substring(int beginIndex)

    该方法从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回

  • public String substring(int beginIndex,int endIndex)

    该方法从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符作为一个新的字符串返回

public class StringTest {
    public static void main(String[] args) {
        String str = new String("啦啦啦,我是豆豆");

        System.out.println(str.substring(4));
        System.out.println(str.substring(4,6));
    }
}
//结果:我是豆豆
//结果:我是
4.字符串比较:compareTo()、equals()
  • public int compareTo(String anotherString)

    该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。

  • public int compareToIgnore(String anotherString)

    与compareTo方法相似,但忽略大小写

  • public boolean equals(Object anotherObject)

    比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。

  • public boolean equalsIgnoreCase(String anotherString)

    与equals方法相似,但忽略大小写

public class StringTest {
    public static void main(String[] args) {
        String str1 = new String("abc");
        String str2 = new String("ABC");

        int a = str1.compareTo(str2);//a>0
        int b = str1.compareToIgnoreCase(str2);//b=o
        boolean c = str1.equals(str2);//c=false
        boolean d = str2.equalsIgnoreCase(str2);//d=true
    }
}
5.字符串连接:concat()

public String concat(String str)//将参数中的字符串str连接到当前字符串的后面,效果等价于"+"

public class StringTest {
    public static void main(String[] args) {
        String str1 = new String("啦啦啦");
        String str2 = str1.concat("我是豆豆");

        System.out.println(str2);
    }
}
//结果:啦啦啦我是豆豆
6.查找子串:indexOf()
  • public int indexOf(int ch/String str)

    用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。

  • public int indexOf(int ch/String str, int fromIndex)

    改方法与第一种类似,区别在于该方法从fromIndex位置向后查找。

  • public int lastIndexOf(int ch/String str)

    该方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找。

  • public int lastIndexOf(int ch/String str, int fromIndex)

    该方法与第二种方法类似,区别于该方法从fromIndex位置向前查找。

public class StringTest {
    public static void main(String[] args) {
        String str = new String("啦啦啦,我是豆豆");

        int a = str.indexOf('是');//5
        int b = str.indexOf("我是");//4
        int c = str.indexOf('啦',1);//1
        int d = str.lastIndexOf('啦');//2
        int e = str.lastIndexOf('豆',1);//-1
    }
}
7.字符串中字符的大小写转换:toLowerCase()、toUpperCase()
  • public String toLowerCase()

    返回将当前字符串中所有字符转换成小写后的新串

  • public String toUpperCase()

    返回将当前字符串中所有字符转换成大写后的新串

public class StringTest {
    public static void main(String[] args) {
        String str = new String("MynameisSHENJIANTAO");

        System.out.println(str.toLowerCase());
        System.out.println(str.toUpperCase());
    }
}
//结果:mynameisshenjiantao
//结果:MYNAMEISSHENJIANTAO
8.字符串中字符的替换:replace()
  • public String replace(char oldChar, char newChar)

    用字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串。

  • public String replaceFirst(String regex, String replacement)

    该方法用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,应将新的字符串返回。

  • public String replaceAll(String regex, String replacement)

    该方法用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,应将新的字符串返回。

public class StringTest {
    public static void main(String[] args) {
        String str = new String("啦啦啦,我是豆豆");

        System.out.println(str.replace('是','叫'));
        System.out.println(str.replaceFirst("啦","嘿"));
        System.out.println(str.replaceAll("啦","嘿"));
    }
}
//结果:啦啦啦,我叫豆豆
//结果:嘿啦啦,我是豆豆
//结果:嘿嘿嘿,我是豆豆
9.是否包含:contains()

public boolean contains(String str)

判断字符串中是否有子字符串,如果有则返回true,没有则返回false

public class StringTest {
    public static void main(String[] args) {
        String str = new String("啦啦啦,我是豆豆");

        System.out.println(str.contains("豆豆"));
    }
}
//结果:true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值