Java--Scanner和String类

Scanner类

  • Scanner用于接受接盘的输入
    • hasNextXXX()用来判断是否为某个数据类型的数据,将会返回一个boolean类型的值
    • Nextxxx()用来获取输入项
      Scanner类中的常用方法
  • nextInt()获取键盘输入的Int类型的值
  • next()获取键盘输入的字符串,将自动删除空格和回车键
  • nexiLine()获取键盘输入的任意字符串
  • 演示:
public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        String s = sc.nextLine();
        System.out.println(i);
        System.out.println(s);
    }
接受字符串的那行接收了一个回车键,因为在输入完int后用户会输入回车键,因此nextline录入了回车键。
因此,我们需要在接收完数据后重新生成一个scanner对象用来接受字符串或者用next来接收。

String类

  • String是字符串,字符串是常量,一旦声明就不可以修改。但是我们经常遇到这种情况:
String str="a"; 
str="b";
此时并不会报错,而且打印str的值也是b,这是为什么呢?
这里的不变是指字符串中的内容不能变,但指向这内容的变量是可以变化的。

在这里插入图片描述

  • String str=new String(“hello”)和String str=“hello”;的区别
    -在这里插入图片描述
public class Test6 {
    public static void main(String[] args) {
        String s1 = "Hello";
        String s2 = "Hello";
        String s3 = "Hel" + "lo";
        String s4 = "Hel" + new String("lo");
        String s5 = new String("Hello");
        String s6 = s5.intern();
        String s7 = "H";
        String s8 = "ello";
        String s9 = s7 + s8;

        //编译期 能不能确定字符串
        System.out.println(s1 == s2); //true
        System.out.println(s1 == s3);  //true
        System.out.println(s1 == s4);  //false
        System.out.println(s1 == s9); //  false
        System.out.println(s4 == s5);  //false




        System.out.println(s1 == s6); // true

        System.out.println(s6==s5);//false
    }
}
在编译期编译器可以直接运算得到结果的且内容相同的为true,如果编译期无法识别则为false,intern是其内部存储的内容
  • String类中的常用方法
  • 在这里插入图片描述
  • 构造方法的演示:
public class Test1 {
    public static void main(String[] args) {

        int []arr={1,2,3};
        byte [] bytes={1,2,3,4};
        char[]ch={'a','b'};
        String s = new String(bytes);//把字节数组转为字符串
        String s1 = new String(bytes, 0, 3);//把字节数组转为指定位置开始指定长度
        String s2 = new String(ch);//把字符数组转为字符串
        String s3 = new String(ch, 0, 1);//把指定位置开始指定长度的字符数组转为字符串
    }
}

在这里插入图片描述
-判断方法的演示:

public class Test2 {
    public static void main(String[] args) {
        String str="aaabbbccc";
        String str2="AAAbbbccc";
        //判断两个字符串的内容是否相同
        boolean b = str.equals(str2);//不忽略大小写
        boolean b1 = str.equalsIgnoreCase(str2);//忽略大小写
        System.out.println(b);//false
        System.out.println(b1);//true
        //判断是否包含某个字符串
        boolean b2 = str.contains("aaa");
        System.out.println(b2);//true
        //判断是否一某个字符串开头或结尾
        boolean b3 = str.startsWith("aaa");
        boolean b4 = str.endsWith("ccc");
        System.out.println(b3);//true
        System.out.println(b4);//true
        //判断是否为空
        boolean b5 = str.isEmpty();
        System.out.println(b5);//false
    }
}
  • 获取功能:
  • 在这里插入图片描述
  • 获取功能的演示:
public class Test3 {
    public static void main(String[] args) {
        String str="acbcdefg";
        //获取字符串的长度
        int length = str.length();//8
        System.out.println(length);
        //获取索引为1的字符
        char c = str.charAt(1);//c
        System.out.println(c);
        //获取指定字符第一次出现时的索引
        int d = str.indexOf('d');
        System.out.println(d);//4
        //获取指定字符在指定位置之后第一次出现的索引
        int c1 = str.indexOf('c', 1);
        System.out.println(c1);//1
        //获取指定字符串的索引
        int efg = str.indexOf("efg");
        System.out.println(efg);//5
        //从指定位置开始截取字符串
        String sb = str.substring(3,5);
        System.out.println(sb);//cd,含头不含尾
    }
}
  • 转换功能
  • 在这里插入图片描述
  • public String toLowerCase(): 把字符串转成小写。
  • public String toUpperCase(): 把字符串转成大写。
  • public String concat(String str): 把字符串拼接。
  • 转换方法的演示:
public class Test5 {
    public static void main(String[] args) {
        //String-->其他
        String str="abcdefg";
        byte[] bytes = str.getBytes();//转为字节数组
        char[] ch = str.toCharArray();//转为字符数组
        //其他-->String
        String s = String.valueOf(100);//100
        String a = String.valueOf('a');//a
        String s2 = String.valueOf(ch);//abcdefg
        String s1 = String.valueOf(true);//true
    }
}
大小写替换:
public class Test8 {
    public static void main(String[] args) {
        //需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)*/
        String s = "caBDfdaaffdfeadfeafdFAAFF";
        String concat = s.substring(0, 1).toUpperCase().concat(s.substring(1).toLowerCase());
        System.out.println(concat);
    }
}

  • 替换功能:
    • public String replace(char old,char new) 将指定字符进行互换
    • public String replace(String old,String new) 将指定字符串进行互换
  • 去除两端空格:public String trim()
  • String类的练习题:
    • 字符串的反转
import java.util.Scanner;

public class Test7 {
    public static void main(String[] args) {
        System.out.println("请输入需要反转的字符串");
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String str="";
        for (int i = s.length()-1; i >=0; i--) {
            char c = s.charAt(i);
            str +=String.valueOf(c);
        }
        System.out.println(str);
    }
}
//输入abc,输出结果为cba
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值