String类使用举例

package Gaojiyuyan;
import java.util.Scanner;
/*
定义一个方法,把数组(1,2,3)按指定格式拼接成一个新字符串,格式如下:【world1#world2#world3】
 */
public class StringleiUse {
    public static void main(String[] args) {
        /*

        int[] array = {1, 2, 3};
        String result = fromArrayToString(array);
        System.out.println(result);
    }
    public static String fromArrayToString(int[] array) {
        String str = "{";
        for (int i = 0; i < array.length; i++) {
            if (i == array.length - 1) {
                str += "world" + array[i] + "}";
            } else {
                str += "world" + array[i] + "#";

            }

        }
        return str;
    }
}
*/
        /*
        键盘输入一个字符串,并统计其中各种字符串出现的次数。种类有:大写字母,小写字母,数字,其他

        思路:
        1.键盘输入,是Scanner
        2.键盘输入字符串,那么:String str=sc。next();
        3.定义四个变量,分别代表四种字符各自出现的次数
        4.需要对字符串一个字一个字的检查,String-->char[],方法就是toCharArray()
        5,遍历char【】字符数组,对当前字符的进行判断,并用四种变量进行++操作
        6,打印输出四个变量,分别代表四种字符出现的次数。
         */
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        String input=sc.next();
        int countUpper=0;
        int countLower=0;
        int countNumber=0;
        int countOther=0;
        char[] charArray=input.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            char ch=charArray[i];//当前字符
            if('A'<=ch&&ch<='Z'){
                countUpper++;
            } else if ('a' <= ch && ch <= 'Z') {

                countLower++;
            } else if ('0' <= ch && ch <= '9') {

                countNumber++;
            } else countOther++;
            }
        System.out.println("大写字母:"+  countUpper);
        System.out.println("小写字母:"+  countLower);
        System.out.println("数字:"+  countNumber);
        System.out.println("其他:"+  countOther);
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值