字符串的其他综合练习*3

 练习1:其他PAI练习


import java.util.Arrays;


public class Demo4 {
    public static void main(String[] args) {
        String s = "abcdef";
        // String类的替换功能
        //在新的字符串中,用新(new)字符,替换旧(old)字符
        //String replace(char old,char new)
        //
        //在新的字符串中,用新的字符串(new), 替换旧(old)字符串
        //String replace(String old, String new)
        String s1 = s.replace("ef", "xyz");
        System.out.println(s1);

        //在新的字符串中,去掉开头和结尾的空格字符
        //String trim()
        String s2 = " aaa ";
        System.out.println("s2.trim() = " + s2);
        System.out.println("s2.trim() = " + s2.trim());


        //分隔功能
        //将字符串按照符号分隔成字符串数组 "a,b,c,d"
        //String[] split(String re)
        String s3 = "a#b#c#d";
        String[] split = s3.split("#");
        System.out.println(Arrays.toString(split));

    }
}

练习2:

需求:

1.给出一个英文句子:“i want a bing dun dun”

2.每个单词的首字母都转换为大写并输出

3.使用 solit() 方法

代码实现:

import java.util.Arrays;

public class Ex5 {
    public static void main(String[] args) {
        // 定义一个字符串
        String s = "i want a bing dun dun";
        // 使用split 按照空格进行分割
        String[] split = s.split(" ");
        System.out.println(Arrays.toString(split));
        // 得到字符串数组
        // 定义空字符串 用于拼接
        String newStr = "";
        for (String str : split) {
            // 遍历字符串数组
            // 把每个字符串的首字母拿出来 转换
            String concat = str.substring(0, 1).toUpperCase().concat(str.substring(1));
            // 重写拼接
            newStr += concat + " ";
        }
        // 输出结果
        System.out.println(newStr.trim());
    }
}

练习3:自然排序

需求:

1.将字符串bdcage 

2.对字符中的字符进行排序,最终得到abcdefg

代码实现:

import java.util.Arrays;

public class Demo {
    public static void main(String[] args) {
        String s = "bdcaegf";
        // 转换为字符数组
        char[] chars = s.toCharArray();
        System.out.println("排序前:");
        System.out.println(Arrays.toString(chars));
        // 排序算法
        //bubbleSort(chars);

        // 简单方法  借助Arrays
        Arrays.sort(chars);

        System.out.println("排序后:");
        System.out.println(Arrays.toString(chars));
        // chars ---> String
        String s1 = new String(chars);
        System.out.println(s1);

    }

    private static void bubbleSort(char[] chars) {
        for (int i = 0; i < chars.length - 1; i++) {
            for (int j = 0; j < chars.length - 1 - i; j++) {
                // 核心思想 两两交互 大的放后面 小的放前面
                if (chars[j] > chars[j + 1]) {
                    // 使用临时变量做交换
                    char temp = chars[j];
                    chars[j] = chars[j + 1];
                    chars[j + 1]= temp;
                }
            }
        }

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值