String类型拓展

JAVA中String类型

1.Scanner概述

  • Scanner的概述:基本用于获取用户的键盘输入

  • Scanner原理

    • Scanner(InputStream source)
    • System类下有一个静态的字段:
      public static final InputStream in; 对应着键盘录入。
  • Scanner中的hasNextXxx()和nextXxx()

    • 基本格式
      • hasNextXxx() ——判断下一个是否是某种类型的元素,其中Xxx可以是Int,Double等。如果需要判断是否包含下一个字符串,则可以省略Xxx
        nextXxx() 获取下一个输入项。Xxx的含义和上个方法中的Xxx相同
  • Scanner获取数据的常用方法

    • public int nextInt():获取一个int类型的值
    • public String nextLine():获取一个String类型的值
    • public String next():获取一个String类型的值
  • 为防止先输入整数再录入字符串时程序直接结束问题

    • 先获取一个数值后,在创建一个新的键盘录入对象获取字符串。
    • 把所有的数据都先按照字符串获取,再转换类型

2.String类

  • 字符串字面值"abc"也可以看成是一个字符串对象。

  • 字符串是常量,一旦被创建,就不能被改变。

  • 构造方法

    • 常见构造方法
      • public String()——空构造
      • public String(byte[] bytes)——把字节数组转成字符串
      • public String(byte[] bytes,int index,int length)——把字节数组的一部分转成字符串
      • public String(char[] value)——把字符数组转成字符串
      • public String(char[] value,int index,int count)——把字符数组的一部分转成字符串
      • public String(String original)——把字符串常量值转成字符串
  • String一旦被创建就不能改变——内容不能变,引用可以变

  • String类的常见面试题

看程序写结果
	String s1 = new String("hello");
	String s2 = new String("hello");
	System.out.println(s1 == s2);//false
	System.out.println(s1.equals(s2));//true

	String s3 = new String("hello");
	String s4 = "hello";
	System.out.println(s3 == s4);//false
	System.out.println(s3.equals(s4));//true

	String s5 = "hello";
	String s6 = "hello";
	System.out.println(s5 == s6);//true
	System.out.println(s5.equals(s6));//true
  • String类的判断功能

    • public boolean equals(Object obj): 比较字符串的内容是否相同,区分大小写
    • public boolean equalsIgnoreCase(String str): 比较字符串的内容是否相同,忽略大小写
    • public boolean contains(String str): 判断字符串中是否包含传递进来的字符串
    • public boolean startsWith(String str): 判断字符串是否以传递进来的字符串开头
    • public boolean endsWith(String str): 判断字符串是否以传递进来的字符串结尾
    • public boolean isEmpty(): 判断字符串的内容是否为空串""
  • 演示:模拟用户登录

public class MyTest3 {
    public static void main(String[] args) {
 
        String name = "zhangsan";
        String password = "123456";
        for(int i=2;i>=0; i--){
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入用户名");
            String uname = sc.nextLine();
            System.out.println("请输入密码");
            String upwd = sc.nextLine();
            if (name.equals(uname) && password.equals(upwd)) {
                System.out.println("输入正确");
                break;
            } else {
                System.out.println("还剩" + i + "次机会");

            }

        }
    }
}
  • String类的获取功能

    • public int length(): 获取字符串的长度。
    • public char charAt(int index): 获取指定索引位置的字符
    • public int indexOf(int ch): 返回指定字符在此字符串中第一次出现处的索引。
    • public int indexOf(String str): 返回指定字符串在此字符串中第一次出现处的索引。
    • public int indexOf(int ch,int fromIndex): 返回指定字符在此字符串中从指定位置后第一次出现处的索引。
    • public int indexOf(String str,int fromIndex): 返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
    • public String substring(int start): 从指定位置开始截取字符串,默认到末尾。
    • public String substring(int start,int end): 从指定位置开始到指定位置结束截取字符串。

3.统计不同类型字符个数

public class MyTest {
    public static void main(String[] args) {
        //2.案例演示:需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
        String str = "asdfAASFsdfesadsSFFkKJDFk1255asdfasdf554755";
        int big = 0;
        int small = 0;
        int num = 0;
        int other = 0;
        for (int i = 0;i<str.length();i++)
        {
            char yzl = str.charAt(i);
            if (yzl >= 'A' && yzl <= 'Z') {
                big++;
            }
            if (yzl >= 'a' && yzl <= 'z') {
                small++;
            }
            if (yzl >= '0' && yzl <= '9') {
                num++;
            }
        }
        System.out.println("字符串中:大写字母" + big + "个,小写字母" + small + "个,数字" + num + "个");
    }
}

4.String类的转换功能

  • public byte[] getBytes(): 把字符串转换为字节数组。

  • public char[] toCharArray(): 把字符串转换为字符数组。

  • public static String valueOf(char[] chs): 把字符数组转成字符串。

  • public static String valueOf(int i): 把int类型的数据转成字符串。

  • public String toLowerCase(): 把字符串转成小写。

  • public String toUpperCase(): 把字符串转成大写。

  • public String concat(String str): 把字符串拼接。

  • 转换字符

public class MyTest8 {
    public static void main(String[] args) {
       /*需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)*/
        String str = "aasdfeASFDFasdfeasdfadsfasdf";
        String s=str.substring(0,1);
        String d=s.toUpperCase();
        String q=str.substring(1,str.length());
        String x=q.toLowerCase();
        System.out.println(d+x);
    }
}

5.String类的其他功能

  • String的替换功能
    public String replace(char old,char new) 将指定字符进行互换
    public String replace(String old,String new) 将指定字符串进行互换

  • String的去除字符串两空格
    public String trim() 去除两端空格

  • String的按字典顺序比较两个字符串

    public int compareTo(String str) 
    

    ​ 会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果
    ​ 如果前面几个字母一样会根据两个字符串的长度进行减法运算返回的就是这个减法的结果
    ​ 如果连个字符串一摸一样 返回的就是0

    public int compareToIgnoreCase(String str)  
    **忽略大小写的比较** 
    

6.去除空格

public class MyTest2 {
    public static void main(String[] args) {
        //1. 去除左端空格
        String s = "     aaaa    ";
       int a=s.indexOf('a');
       String d=s.substring(a,s.length()-1);
        System.out.println(d);
        //2.去除右端空格
        String s8 = "     a  a  a  a    ";
        int b=s8.lastIndexOf('a');
        String f=s8.substring(0,b+1);
        System.out.println(f);

7.在大串中查找小串出现的次数

public class MyTest8{
    public static void main(String[] args) {
       /* 需求:统计大串中小串出现的次数
        举例:
        "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun" 中java出现了5次*/

        String str = "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";
        String s="java";
        int n=0;
        for(int i=0;i<str.length()-4;i++){
            if(str.substring(i,i+4).equals(s)){
                n++;
            }

        }
        System.out.println("JAVA出现了"+n+"次");


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值