java- charAT- 工具类2

该篇博客通过示例展示了Java中字符串`charAt()`方法的使用,用于从字符串中获取字符。同时,讲解了如何统计一个字符串中特定字符出现的次数,并演示了如何将整数数组转换为逗号分隔的字符串。此外,还提供了将字符串拆分为数组并转化为整数数组的方法。
摘要由CSDN通过智能技术生成

java 字符串练习:charAt()

package com.test001;

/**
 *   charAt () 从字符串中获取找到字符
 */

public class CharAt {
    public static void main(String[] args) {
        String s1 = "大家好,我是小飞,今天我们开始学习新的知识点,它名字是charAt()。";
        String s2 = ", ,。! ?# % ¥ $ * ";

        int count = 0;
        for (int i = 0; i < s1.length(); i++) {
            char c = s1.charAt(i);     //  把字符串中的每个字符都拿出来查看是否存在于s2 中;
//            System.out.print(c);
            if (s2.indexOf(c) >= 0){    //  如有存在 count + 1  :表示有一句话
                count += 1;
            }
        }
        System.out.println("句子的数量为: " + count);


        String str = " xiamianduicharatzhegefangfajinxingcheshi";
        char cha = str.charAt(5);
        System.out.println(cha);   // 输出第五个字符


        /**
         *   把一个数组转化为字符串
         */

        int []  arr = {23, 34, 45, 56, 67, 78, 89};
        String result = "";
//        System.out.println(result.length());
        for (int i = 0; i < arr.length; i++) {
            if (result.length() > 0)
                result += ",";
                result = result + arr[i];
        }
        System.out.println(result);

        /**
         *   现在写一个方法,根据这个字符串生成一个int[]
         */

        String  strs = "13,33,22,45,89";
        String[] split = strs.split(",");
        int[] ints = new int[split.length];
        for (int i = 0; i < split.length; i++) {
                ints [i] = Integer.valueOf(split[i]);    //  这一步已经转化完成: int []  ints = {13,33,22,45,89};
        }
        for (int i = 0; i < ints.length; i++) {
            System.out.print(ints[i]);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值