day15 JavaSe之String类的其他功能

替换功能
String replace(char old,char new)
String replace(String old,String new)
去除字符串两空格
String trim()
按字典顺序比较两个字符串
int compareTo(String str)
int compareToIgnoreCase(String str)

package com.shujia.java.day15;

/*
            替换功能
                String replace(char old,char new)
                String replace(String old,String new)

            去除字符串两空格	******
                String trim()

            按字典顺序比较两个字符串
                int compareTo(String str)
                int compareToIgnoreCase(String str)

 */
public class StringDemo11 {
    public static void main(String[] args) {
        String s = "helloworld";

        //String replace(char old,char new)
        //将字符串中所有的l替换成a,返回一个新的字符串
        String replaceString = s.replace('l', 'a');
        System.out.println(replaceString);
        System.out.println(s);

        //String replace(String old,String new)
        String replaceString2 = s.replace("owo", "wow");
        System.out.println(replaceString2);

        String replaceString3 = s.replace("owo", "qwerdf");
        System.out.println(replaceString3);

        //如果被替换的字符串不存在,返回原来的字符串
        String replaceString4 = s.replace("opi", "qwe");
        System.out.println(replaceString4);

        //String trim()去除字符串两边的空格,不能去除字符串中间的空格
        String s1 = " hello world ";
        String trimString = s1.trim();
        System.out.println(s1);
        System.out.println(trimString);

        //int compareTo(String str)
        String s2 = "hello"; //h的ASCLL码值:104
        String s3 = "hello";
        String s4 = "abc"; // a的ASCLL码值:97
        String s5 = "qwe"; // q的ASCLL码值:113
        System.out.println(s2.compareTo(s3)); // 0
        System.out.println(s2.compareTo(s4)); // 7
        System.out.println(s2.compareTo(s5)); // -9

//如果前面的几个字符都是一样的怎么办呢?
        String d = "hello";
        String d3 = "hellobigdata";
        String d2 = "hel";
        System.out.println(d.compareTo(d2));//d2-d  2
        System.out.println(d3.compareTo(d));//d3-d  7



    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值