字符串和StringBuilder的运用

public static void main(String[] args) {
    stuName();
    xiaoMi();
    showChar();
    printIsContain();
    xiaoMii();
    xiaoMia();
    xiaoMib();
    xiaoMic();
    xiaoMid();
    xiaoMie();
}

public static void stuName() {
    String a = "小明";
    int b = 17;
    float c = 92.5f;
    String d = a + "的年龄是" + b + ',' + "成绩是" + c;
    System.out.println(d);
}

public static void xiaoMi() {
    String l = "http://www.baidu.com";
    String k = "字符长度是";
    int xiaoMi = l.length();
    System.out.println(k + xiaoMi);

}

public static void showChar() {
    String str = "123456789";
    char one = str.charAt(0);
    char two = str.charAt(5);
    char three = str.charAt(8);
    System.out.println(one + "   " + two + "   " + three + "  ");
}

public static void printIsContain() {
    String str = "wei_xue_yuan_is_good";
    String a = "Warmtel";
    String b = "arm";
    boolean c = a.contains(b);
    String s = c ? "包含" : "不包含";
    System.out.println(c);
}

public static void xiaoMii() { // 使用两种方式实现向字符串"欢迎来到java"后追加字符串"世界";
    StringBuilder sb = new StringBuilder();
    sb.append("欢迎来到java");
    sb.append("世界");
    System.out.println(sb);

}

public static void xiaoMia() {
    StringBuilder sb = new StringBuilder("欢迎来到java");
    sb.append("世界");
    System.out.println(sb);
}

public static void xiaoMib() { // 向字符串"welme"第2个字符后插入字符串"co",生成新的字符串
                                // "welcome"
    StringBuilder sb = new StringBuilder("welme");
    sb.insert(3, "co");
    System.out.println(sb);

}

public static void xiaoMic() {// 删除字符串"helloworld"中第5个字符和第7个字符
    StringBuilder sb = new StringBuilder("helloworld");
    sb.deleteCharAt(4);
    sb.deleteCharAt(6);
    System.out.println(sb);
}

public static void xiaoMid() {// 从字符串"welcometoscxh"中获取子字符串"come"
    StringBuilder sb = new StringBuilder("welcometoscxh");
    String a = sb.substring(3, 7);
    System.out.println(a);
}

public static void xiaoMie() { // replace()方法,用来替换字符串中所有指定的子串,
    StringBuilder sb = new StringBuilder(
            "The url of weixueyuan is www.warmtel.com!");
    sb.replace(11, 21, "warmtel"); // 例如
                                    // "The url of weixueyuan is www.warmtel.com!"
    System.out.println(sb); // 替换weixueyuan 为warmtel.

}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值