zoukankan      html  css  js  c++  java
  • 实用类(二)

    一,Random类
      概述:此类的实例是用于生成随机数的
      使用步骤:
        1,导包
        2,Random(); 构造器
        3,nextInt(); 调方法,返回随机int数 参数可限制随机数范围
     
    二,String类
      概述:String类代表字符串,java程序中所有的字符串文字都被看成此类的实现
      方法:比较字符串,搜索字符串,提取字符串,大小写等
      特点:1,字符串时常量,一旦生成有长度、下标,不可变性。
        2,可以共享性。
        3,等效性。
      如何使用String类:
        1,String(); 空参构造
        2,String(char[ ] char); 可传char数组
        3,String(byte[ ] byte); 可传byte数组
      常用方法:
        1,比较判断功能:
          equals           用于比较字符串的内容。当比较两方有一方为常量,一方为变量时,推荐常量写外面,变量写里面 a.equals(b);
          ==             用于比较双方是否为同一对象,比较的是对象地址
          equalsIgnoreCase()   忽略大小写比较(只对英文),常用于验证码
        2,获取功能:
          length();           获取字符串长度属性
          concat();           拼接字符串
          charAt(int index);         返回指定索引的char值
          indexOf();           搜索指定字符,找到了返回该字第一次出现位置下标,没找到返回-1。
          subString(int index);       截取从参数位置到结尾的新字符串,返回。(含开头、左,不含结尾、右)
          subString(int begin,int end);   截取从begin开始到end结尾的心字符串。(含头,不含尾)
        3,转换功能:
          toCharArray();           将String---->char[ ]
          getBytes();             将String---->byte[ ]
          replace("a","b")         将"a"--替换成"b"
        4,分割功能:
          split(String regex);       按regex规则分割成字符段
     
     
    package cn.kgc.string;
    import java.util.Scanner;
    // 输入字符串,判断里面含有多少个大写字母,小写字符,数字,其他
    public class Zuoye {
    public static void main(String[] args) {
    // 键盘录入
    System.out.println("请输入字符串:");
    Scanner input = new Scanner(System.in);
    // 定义类型
    int upperCount=0;
    int lowerCount=0;
    int numCount=0;
    int otherCount=0;
    // 获取字符串转为字符数组
    String in = input.next();
    char[] str = in.toCharArray();
    // 循环遍历数组,判断各类型count
    for (int i = 0; i <str.length ; i++) {
    if (str[i]>='a' && str[i]<='z'){
    lowerCount++;
    }else if(str[i]>='A' && str[i]<='Z'){
    upperCount++;
    }else if (str[i]>='0' && str[i]<='9'){
    numCount++;
    }else {
    otherCount++;
    }
    }
    // 输出各类型个数
    System.out.println("大写字母个数:"+upperCount+" "+",小写字母个数:"+lowerCount+" "+",数字个数:"+numCount+" "+",其他个数:"+otherCount+" ");
    }
    }
  • 相关阅读:
    TextBox 只有下划线
    can't find web control library(web控件库)
    DropDownListSalesAC”有一个无效 SelectedValue,因为它不在项目列表中。
    IDE、SATA、SCSI、SAS、FC、SSD 硬盘类型
    如何打印1px表格
    CSS控制打印 分页
    Virtual Server could not open its emulated Ethernet switch driver. To fix this problem, reenable the Virtual Server Emulated Et
    Xml中SelectSingleNode方法中的xpath用法
    热带水果莫入冰箱?水果存放冰箱大法
    探索Asp.net的Postback机制
  • 原文地址:https://www.cnblogs.com/kide1412/p/10902034.html
Copyright © 2011-2022 走看看