1.Scanner类

1. Scanner类概述:一个可以使用正则表达式来解析基本类型和字符串的简单文本扫描器
2. Scanner的构造方法:Scanner(InputStream source)
3. Scanner类下有一个静态的字段:public static final InputStream in;(标准输入流)
4. 常用方法
(1)hasNextXxx():判断下一个是否是某种类型的元素
(2)nextXxx():获取下一个输入项

public class ScannerDemo01 {public static void main(String[] args) {InputStream is = System.in;Scanner sc = new Scanner(is);  //等同于:Scanner sc = new Scanner(System.in);//录入数字int i = sc.nextInt();double d = sc.nextDouble();long l = sc.nextLong();//录入布尔类型boolean b = sc.nextBoolean();//录入字符串String s = sc.nextLine();}
}

5. 有一个小bug需要注意一下

public class ScannerDemo02 {public static void main(String[] args) {//当我们录入完一个整数的时候,接着录入字符串,发现字符串并没有录入进去//因为当我们录入完整数后,敲击回车键,IDEA会把“回车键”当做字符串进行录入/*Scanner sc = new Scanner(System.in);System.out.println("请输入一个数字");int i = sc.nextInt();System.out.println(i);System.out.println("请输入一个字符串");String s = sc.nextLine();System.out.println(s);*///解决办法:在录入完整数后,重新创建一个Scanner对象Scanner sc = new Scanner(System.in);System.out.println("请输入一个数字");int i = sc.nextInt();System.out.println(i);//重新创建Scanner对象sc = new Scanner(System.in);System.out.println("请输入一个字符串");String s = sc.nextLine();System.out.println(s);}
}

6. 让用户输入整数并打印的小案例

public class ScannerDemo03 {public static void main(String[] args) {while(true){Scanner sc = new Scanner(System.in);System.out.println("请输入一个整数");if(sc.hasNextInt()){int i = sc.nextInt();System.out.println(i);break;}else {System.out.println("输入的数据不对,请重新输入");}}}
}

2.String类

1. 字符串的概述:字符串是由多个字符组成的一串数据(字符序列)
2. 字符串可以看成是字符数组,字符串的每个字符,从左往右编有索引,从0开始
3. 构造方法
(1)public String():空参构造
(2)public String(String original):把字符串常量值转成字符串

public class TestDemo01 {public static void main(String[] args) {//空字符序列String s = new String();System.out.println(s);  ////String类重新了toString方法,直接打印字面值System.out.println(s.toString());   //String s2 = new String("abc");System.out.println(s2);  //abcSystem.out.println(s2.toString());  //abc}
}

(3)public String(byte[] bytes):把字节数组转成字符串
(4)public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串(index:表示的是从第几个索引开始, length表示的是长度)

public class TestDemo02 {public static void main(String[] args) {//定义一个字节数组byte[] bytes = {97,98,99,100};String s1 = new String(bytes);System.out.println(s1);  //abcdString s2 = new String(bytes,0,2);System.out.println(s2);  //ab}
}

(5)public String(char[] value):把字符数组转成字符串
(6)public String(char[] value,int index,int count):把字符数组的一部分转成字符串

public class TestDemo03 {public static void main(String[] args) {char[] chars = {'a','b','c','英','雄','联','盟'};String s1 = new String(chars);System.out.println(s1);  //abc英雄联盟String s2 = new String(chars,3,4);System.out.println(s2);   //英雄联盟}
}

4. 获取字符串长度:public int length()

public class TestDemo01 {public static void main(String[] args) {String s1 = new String("abcde");int i1 = s1.length();System.out.println(i1);  //5//字符串字面值"cdef"也可以看成是一个字符串对象。int i2 = "cdef".length();System.out.println(i2);  //4}
}

5. 字符串的特点:字符串常量一旦被创建,就不能被改变。因为字符串的值是在堆内存的字符串常量池中划分的,分配地址值的
(1)案例演示①

public class TestDemo02 {public static void main(String[] args) {String s = "LOL";s = "hello " + "world";System.out.println(s);  //hello world}
}

(2)案例演示②:String s1 = new String(“hello”) 和 String s2 = "hello"的区别

public class TestDemo03 {public static void main(String[] args) {String s1  = new String("hello");String s2 = "hello";}
}



(3)案例演示③

public class TestDemo04 {public static void main(String[] args) {String s1 = new String("hello");String s2 = new String("hello");System.out.println(s1 == s2);           //falseSystem.out.println(s1.equals(s2));      //trueString s3 = new String("hello");String s4 = "hello";System.out.println(s3 == s4);           //falseSystem.out.println(s3.equals(s4));      //trueString s5 = "hello";String s6 = "hello";System.out.println(s5 == s6);          //trueSystem.out.println(s5.equals(s6));     //true}
}

(4)案例演示④

public class MyTest4 {public static void main(String[] args) {//当我们采用 字符串字面值 这种方式来定义一个字符串的时候,// 他会先去字符串常量池去查找有没有有该字符串,如果没有,就构建这个字符串。//如果有,就把字符串常量池中这个字符串的地址值,赋值给这个新的引用String s1 = "hello";String s2 = "hello";String s3 = "abc";String s4 = new String("world");System.out.println(s1 == s2); //trueSystem.out.println(s2 == s3); //falseSystem.out.println(s4 == s3);//false}
}


6. String类的常用方法

(1)判断功能

  • public boolean equals(Object obj):比较两个字符串的内容是否相同(区分大小写)
  • public boolean equalsIgnoreCase(Object obj) :比较两个字符串的内容是否相同(不区分大小写)
public class TestDemo01 {public static void main(String[] args) {//public boolean equals(Object obj):比较两个字符串的内容是否相同(区分大小写)boolean b1 = "abc".equals("ABC");System.out.println(b1);  //false//public boolean equalsIgnoreCase(Object obj) :比较两个字符串的内容是否相同(不区分大小写)boolean b2 = "abc".equalsIgnoreCase("ABC");System.out.println(b2);  //true}
}
  • public boolean contains(String str):字符串中是否包含传递进来的字符串
  • public boolean startsWith(String str):判断字符串是否以传进来的字符串开头
  • public boolean endWith(String str):判断字符串是否以传进来的字符串结尾
  • public boolean isEmpty():判断字符串是否是空字符串
public class TestDemo02 {public static void main(String[] args) {String str1 = "啦啦啦德玛西亚";//public boolean contains(String str):字符串中是否包含传递进来的字符串boolean b1 = str1.contains("德玛");System.out.println(b1);  //true// public boolean startsWith(String str):判断字符串是否以传进来的字符串开头boolean b2 = str1.startsWith("啦啦啦");System.out.println(b2);  //true//public boolean endWith(String str):判断字符串是否以传进来的字符串结尾boolean b3 = str1.endsWith("哈哈");System.out.println(b3);  //false//public boolean isEmpty():判断字符串是否是空字符串boolean b4 = str1.isEmpty();System.out.println(b4);   //falseString str2 = "";System.out.println(str2.isEmpty());  //false}
}
  • 案例演示:模拟登陆,给三次机会,并提示还有几次
import java.util.Scanner;public class TestDemo03 {public static void main(String[] args) {String userName = "vn";String passWord = "123456";Scanner sc = new Scanner(System.in);for (int i = 1; i <= 3; i++) {System.out.println("请输入用户名");String uName = sc.nextLine();System.out.println("请输入密码");String psw = sc.nextLine();if(uName.equals(userName) && psw.equals(passWord)){System.out.println("登陆成功");break;}else {if((3-i) != 0){System.out.println("登陆失败,你还有" + (3 - i) + "次机会" );}else {System.out.println("账号已被冻结");}}}}
}

(2)获取功能

  • public int length():获取字符串的长度
  • public char charAt(int index):获取指定索引位置的字符
  • public int indexOf(char ch):返回指定字符在此字符串中第一次出现处的索引
  • public int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引
  • public int indexOf(char ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引
  • public int indexOf(String str,int fromIndex) :返回指定字符串在此字符串中从指定位置后第一次出现处的索引
  • public String substring(int start):从指定位置开始截取字符串,默认到末尾
  • public String substring(int start,int end): 从指定位置开始到指定位置结束截取字符串(包括前面,不包括后面)
public class TestDemo01 {public static void main(String[] args) {String str1 = "啦啦啦德玛西亚";//public int length():获取字符串的长度int length = str1.length();System.out.println(length);  //7//public char charAt(int index):获取指定索引位置的字符char c = str1.charAt(5);System.out.println(c);   //西//public int indexOf(char ch):返回指定字符在此字符串中第一次出现处的索引int i1 = str1.indexOf('德');System.out.println(i1);   //3//public int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引int i2 = str1.indexOf("啦德玛");System.out.println(i2);  //2//public int indexOf(char ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引int i3 = str1.indexOf('啦', 1);System.out.println(i3);  //1//public int indexOf(String str,int fromIndex) :返回指定字符串在此字符串中从指定位置后第一次出现处的索引int i4 = str1.indexOf("德玛", 2);System.out.println(i4);  //3// String substring(int start):从指定位置开始截取字符串,默认到末尾String substring1 = str1.substring(2);System.out.println(substring1);   //啦德玛西亚//public String substring(int start,int end):  从指定位置开始到指定位置结束截取字符串(包括前面,不包括后面)String substring2 = str1.substring(1, 4);System.out.println(substring2);   //啦啦德}
}
  • 案例演示:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
public class TestDemo02 {public static void main(String[] args) {String str = "asdfASDJ18E9czc12";int numDa = 0;int numXiao = 0;int numShu = 0;for (int i = 0; i < str.length(); i++) {if(str.charAt(i) >= 'A' && str.charAt(i) <= 'Z'){numDa++;}else if(str.charAt(i) >= 'a' && str.charAt(i) <= 'z'){numXiao++;}else if(str.charAt(i) >= '0' && str.charAt(i) <= '9'){numShu++;}}System.out.println("大写字符有:" + numDa + "个");  //大写字符有:5个System.out.println("小写字符有:" + numXiao + "个");  //小写字符有:7个System.out.println("数字有:" + numShu + "个");   //数字有:5个}
}

(3)转换功能

  • public byte[] getBytes():把字符串转换为字节数组
  • public char[] toCharArray():把字符串转换为字符数组
  • public static String valueOf(char[] chs):把字符数组转成字符串
  • public static String valueOf(int i):把int类型的数据转成字符串
  • 注意:String类的valueOf方法可以把任意类型的数据转成字符串
  • public String toLowerCase():把字符串转成小写
  • public String toUpperCase():把字符串转成大写
  • public String concat(String str):拼接字符串
public class TestDemo01 {public static void main(String[] args) {//public byte[] getBytes():把字符串转换为字节数组String str1 = "abcd";byte[] bytes = str1.getBytes();for (int i = 0; i < bytes.length; i++) {System.out.println(bytes[i]);   //97 98 99 100}//把字节数组转化成字符串String s = new String(bytes);System.out.println(s);   //abcdSystem.out.println("=================");//public char[] toCharArray():把字符串转换为字符数组String str2 = "德玛西亚";char[] chars = str2.toCharArray();for (int i = 0; i < chars.length; i++) {System.out.println(chars[i]);    //德 玛 西 亚}//把字符数组转换成字符串String s1 = new String(chars);System.out.println(s1);   //德玛西亚System.out.println("=================");//public static String valueOf(char[] chs):把字符数组转成字符串char[] chs = {'伊','泽','瑞','尔'};String str3 = String.valueOf(chs);System.out.println(str3);   //伊泽瑞尔System.out.println("=================");//public static String valueOf(int i):把int类型的数据转成字符串String str4 = String.valueOf(100);System.out.println(str4);   //100System.out.println("=================");//public String toLowerCase():把字符串转成小写//public String toUpperCase():把字符串转成大写String str5 = "ADfkjASC";String s2 = str5.toLowerCase();System.out.println(s2);           //adfkjascString s3 = str5.toUpperCase();System.out.println(s3);           //ADFKJASCSystem.out.println("=================");//public String concat(String str):拼接字符串String str = "金克斯".concat("没有胸哈哈哈");System.out.println(str);   //金克斯没有胸哈哈哈}
}
  • 案例演示:给定一个字符串,除了首字母大写外,其余全是小写
public class TestDemo02 {public static void main(String[] args) {String str = "ASKLJjklcaSA";//链式编程String s = str.substring(0, 1).toUpperCase().concat(str.substring(1).toLowerCase());System.out.println(s);   //Askljjklcasa}
}

(4)其他功能

  • public String replace(char old,char new):将指定字符进行互换
  • public String replace(String old,String new):将指定字符串进行互换
  • public String trim() :去除两端空格
  • public int compareTo(String str):会对照ASCII 码表 从第一个字母进行减法运算,返回的就是这个减法的结果。如果前面几个字母一样会根据两个字符串的长度进行减法运算返回的就是这个减法的结果。如果连个字符串一摸一样,返回的就是0
public class TestDemo01 {public static void main(String[] args) {//public String replace(char old,char new):将指定字符进行互换String str1 = "周淑怡没有胸";String s1 = str1.replace('胸', '*');System.out.println(s1);   //周淑怡没有*//public String replace(String old,String new):将指定字符串进行互换String str2 = "特朗普是傻逼";String s2 = str2.replace("傻逼", "**");System.out.println(s2);   //特朗普是**//public String trim():去除两端空格String str3 = "  略略略   ";System.out.println(str3);   //  略略略String s3 = str3.trim();System.out.println(s3);   //略略略//public int compareTo(String str):会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果int i1 = "abc".compareTo("Abc");System.out.println(i1);   //32  a->97  A->65int i2 = "abc".compareTo("abc");System.out.println(i2);   //0int i3 = "abc".compareTo("abcd");System.out.println(i3);   //-1   abc->3位数   abcd->4位数}
}

Java基础(12)Scanner类、String类相关推荐

  1. java高级:Scanner和String类介绍

    1.Scanner 概述:简单的文本扫描器 next()String(直接获取内容,不介绍回车键) nextLine String(获取输入的返回值) nextByte() byte nextShor ...

  2. java实现linkstring,【JAVA SE基础篇】32.String类入门

    [JAVA SE基础篇]32.String类入门 1.字符串 1.String类又称作不可变字符序列 2.String位于java.lang包中,java程序默认导入java.lang包下所有的类 3 ...

  3. Java学习记录(补充三:String类)

    String类的基本概念package day6;import java.util.Arrays;public class Demo02 { public static void main(Strin ...

  4. JavaSE学习总结(八)常用类(上)Object类==与equals方法的区别浅克隆的特点Scanner类String类String两种创建对象方式的区别String类的各种功能

    JavaSE学习总结(八)常用类(上)/Object类/==与equals方法的区别/浅克隆的特点/Scanner类/String类/String两种创建对象方式的区别/String类的各种功能 常用 ...

  5. Java基础学习——第十章 枚举类注解

    Java基础学习--第十章 枚举类&注解 一.枚举类(enum) 1. 枚举类的概念 枚举类:类的对象只有有限个,确定的 线程状态:创建.就绪.运行.阻塞.死亡 当需要定义一组常量时,强烈建议 ...

  6. Java基础语法(七)——类和对象

    文章目录 Java基础语法(七)--类和对象 一.类和对象的初步认识 二. 类和类的实例化 1.类的定义 2.实例化对象 3.如何使用类中的数据 三. 类的成员 1. 字段/属性/成员变量 (1)实例 ...

  7. Java黑皮书课后题第10章:**10.23(实现String类)在Java库中提供了String类,给出你自己对下面方法的实现(将新类命名为MyString2)

    **10.23(实现String类)在Java库中提供了String类,给出你自己对下面方法的实现(将新类命名为MyString2) 题目 简短的吐槽 代码:这里将类名改用Test23_MyStrin ...

  8. java源码详解——String类

    java源码详解--String类目录: Java String 类 下面开始介绍主要方法: Java charAt() 方法 Java compareTo() 方法 int compareTo(St ...

  9. abstract类_012 JAVA 抽象类、接口、String类的基础了解

    1.抽象方法和抽象类 抽象方法:使用abstract修饰的方法,没有方法体,只有声明.抽象方法可以当做是一种规范,让子类必须实现. 注意: 1.抽象方法没有方法体,只能以分号结尾 2.抽象方法只能声明 ...

  10. Java基础---API概述---常用类(Object类/String类)---equals和==

    API概述 API:application programming interface,应用程序编程接口 用于规定方法名称的规则集合,定义了方法的修饰符.返回值类型.方法的名称.方法的参数列表.方法的 ...

最新文章

  1. 【怎样写代码】小技巧 -- 关于引用类型的两种转换方式
  2. MATLAB获取一个目录中的所有文件
  3. (实用)将wordpad添加到Windows PowerShell中
  4. 56.ISE综合,在chipscope信号列表看不到
  5. python文本字符串比对_python-模糊字符串比较
  6. CAReplicatorLayer复制Layer和动画, 实现神奇的效果
  7. LeetCode 5178. 四因数
  8. 小学信息技术用计算机编辑文档教案,小学信息技术《初识文字处理软件》教案.doc...
  9. linux精灵进程之crond
  10. 新增字段属性“是否转义”,提高列表展示性能
  11. LINUX下类似画图板Paint的工具
  12. python实现app自动签到器_python实现网页自动签到功能
  13. Selenium官网打不开,这里看过来☺
  14. 中兴c600olt数据配置_中兴OLT业务配置
  15. Xilinx XC7Z020双核ARM+FPGA开发板试用
  16. 闰秒及其对计算机系统影响,闰秒原理及其对计算机系统影响
  17. 51单片机LED流水灯、走马灯的实现
  18. Magento 1.4 EAV 属性中的新东西
  19. Collections的用法
  20. SpringBoot集成Quartz+数据库存储

热门文章

  1. 运动耳机排行榜10强,推荐其中六款好用的运动耳机
  2. 链表逆序 递归 java_将链表逆序(递归方式)
  3. 比拼多多更变态的模式,邀请6个人就能赚几万,这个点子绝了
  4. Centos7下安装RabbitMQ教程
  5. Java之JavaDoc标签
  6. 互联网三大巨头宣布将支持FIDO无密码登录,虹膜识别被纳入其中
  7. 前端程序员大厂面试精选100道算法题2
  8. YOLOv5 数据集划分及生成labels
  9. 使用JQuery快速高效制作网页交互特效第六章所有上机
  10. Go channel 底层结构及实现