String类的应用(作业)


有何不足希望大家指出


  1. 编写一个程序,输出一个字符串中的英文字母个数,数字个数
public class Zuoye1{

    public static void main(String [] args){

        String s = new String("Hellow + World + 12322246 ");
        String a = new String("A");
        String b = new String("z");
        String c = new String("0");
        String d = new String("9");
        int n=0;
        int m=0;
        for(int i = 0;i<s.length();i++){

            if(s.charAt(i)>=a.charAt(0)&&s.charAt(i)<=b.charAt(0))   //字母
                { m++;}

            if(s.charAt(i)>=c.charAt(0)&&s.charAt(i)<=d.charAt(0))   //数字
                { n++;}


        }
        System.out.println("数字"+n);
        System.out.println("字母"+m);
    }

}
  1. 编写一个方法,输出在一个字符串中指定字符串出现的次数

方法1

public class Zuoye2{
    public static void main(String [] args){
        String a ="123124112312521";
        int s=0;
        int m=0;
        for(int i = 0;i<a.length();i++){   
            int c =a.indexOf("12",m);   //从m开始检索,并返回其位置

            if(c>=0){           //将m设定为为检索到位置的后两位
                s++;
                m=c+2;
            }           
        }
        System.out.println(s);



    }

}

方法2

public class Zuoye22{
    public static void main(String [] args){
        String a ="123124512612315312";
        String b = "12";
        int s=0;
        for(int i = 0;i<(a.length()-1);i++){
            if(b.equals(a.substring(i,i+2)))

                s++;  
            }
        System.out.println(s);
    }
}
  1. 编写一个方法,返回一个double型二维数组,数组中的元素通过解析字符串参数获得,如字符串这里写图片描述
public class ArrryParser {
   public static void  main(String[] args) {
      String s = "1,2;3,4,5;6,7,8";
      double[][] d;
      String sFirst[] = s.split(";");             //按;划分一维数组sFirst  
      d = new double[sFirst.length][];            //定义double型数组
      for(int i=0;i<sFirst.length;i++ ) {
         String[] sSecond = sFirst[i].split(",");  //按,划分二维数组sSecond
         d[i] = new double[sSecond.length];          
         for(int  j=0;j<sSecond.length;j++ ) {
            d[i][j] = Double.parseDouble(sSecond[j]);    //
         }
      }

      for(int i=0;i<d.length;i++ ) {
         for(int j = 0;j<d[i].length;j++) {
            System.out.print(d[i][j]+" ");
         }
         System.out.println();
      }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值