字符串中各类字符的总和汇总 字符串中子串个数查询实现

各类字符总和

import java.util.Scanner;

public class StringTest
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
       
        System.out.println("请输入一行字符串:");
        String s = sc.nextLine();

        int big=0,small=0,space=0,other=0;
        for(int i=0;i<s.length();i++)
        {
            char c = s.charAt(i);
            if(c>='a' && c<='z')
                small++;
            if(c>='A' && c<='Z')
                big++;
            if(c == 32)
                space++;
            else
                  other++;
        }
        System.out.println("小写字母为:" + small + "  大写字母为:"+ big + "  空格为:" + space + "  其他字符为" + other);
    }
}

 

子串个数查询:

 

使用subString

import java.util.*;


public class StringTest2
{

    public static void main(String args[])
    {

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一行字符串:");
        String s = sc.nextLine();
        System.out.println("请输入要查询个数的字符串");
        String aim= sc.nextLine();
        int number = 0;
        for(int i = 0;i<=s.length()-aim.length();i++)
        {
            if(s.substring(i,i+aim.length()).equals(aim))
            {
                  number++;
                  i = i + aim.length() - 1;
             }
        }
        System.out.println("字符串"+aim+"的个数为"+number+"!!!");
    }
}

 

 

使用indexOf:

import java.util.*;
public class StringTest3
{
      public static void main(String args[])
      {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一行字符串:");
        String s = sc.nextLine();
        System.out.println("请输入要查询个数的字符串");
        String aim= sc.nextLine();
        int number=0;
        int start = 0;
        while(s.indexOf(aim,start)>=0 && start < s.length())
        {
              number++;
              start = s.indexOf(aim,start) + aim.length();
        }
        System.out.println("出现的次数为" + number + "!");

      }
}

转载于:https://www.cnblogs.com/42577157/archive/2010/10/16/1853121.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值