Scanner(用于接收键盘录入数据)
构造方法:ppublic Scanner(InputStream source)
Scanner sc=new Scanner(System.in);
System 类 in 成员变量(静态)
public static final InputStream in;标准输出流
hasNextxxx()和nextxxx的方法
hasNextxxx():xxx可以是int byte double float..判断录入的数据是不是xx类型
nextxxx():xxx可以是int byte double float.. 获取xx数据
问题及解决方法
int nextInt();
String nextLine();
先获取Int,再获取String。会出现问题(只能输入int,String没机会输入)
原因:输入int完毕后,敲了回车“\t\n”,不属于int,就被String接收
而所有的数据输入默认以"\t\n"结束,so String的值为空
解决:重新创建Scanner对象or所有数据都用字符串,然后转换成你想要的。//后面讲
String(字符串)
概述 多个字符链接的一串数据. 可以看做成一个字符数组;
特点
字面值字符串“abc"可以看做一个字符串对象
字符串一但创建就无法改变 但引用变量可以改变
String的构造方法
String():空构造
String(byte[] bytes):把字节数组转成字符串
String(byte[] bytes,int index,int length):字节数组的一部分转成字符串(index:表示的是从第几个索引开始, length表示的是长度)
String(char[] value):字符数组转成字符串
String(char[] value,int index,int count):字符数组的一部分转成字符串
String(String original):字符串常量值转成字符串
判断功能
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 int length(): 获取字符串的长度。
public char charAt(int index): 获取指定索引位置的字符
public int indexOf(int ch): 返回指定字符在此字符串中第一次出现处的索引。如果没有,返回为-1.
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): 从指定位置开始到指定位置结束截取字符串。
转换功能:
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 String replace(char old,char new)将指定字符进行互换
public String replace(String old,String new)将指定字符串进行互换
去除字符串两空格
public String trim()去除两端空格
按字典顺序比较两个字符串
public int compareTo(String str)
public int compareToIgnoreCase(String str)
案例 1,模拟登录,给三次机会,并提示还有几次
字符串的遍历 charAt(int index)和length();
统计不同类型字符个数 需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数
相关问题
String s = new String(“hello”)和String s = “hello”;的区别
String s = new String(“hello”) 两个对象,一个是方法区常量池,一个在堆内存
String s = “hello”; 一个对象,一个是方法区常量池
==和equals()的区别
== 比较基本数据,也比较引用数据的地址值
equals() 比较引用数据,默认比较地址值 String中比较的是字符串内容是否相同
构造方法:ppublic Scanner(InputStream source)
Scanner sc=new Scanner(System.in);
System 类 in 成员变量(静态)
public static final InputStream in;标准输出流
hasNextxxx()和nextxxx的方法
hasNextxxx():xxx可以是int byte double float..判断录入的数据是不是xx类型
nextxxx():xxx可以是int byte double float.. 获取xx数据
问题及解决方法
int nextInt();
String nextLine();
先获取Int,再获取String。会出现问题(只能输入int,String没机会输入)
原因:输入int完毕后,敲了回车“\t\n”,不属于int,就被String接收
而所有的数据输入默认以"\t\n"结束,so String的值为空
解决:重新创建Scanner对象or所有数据都用字符串,然后转换成你想要的。//后面讲
String(字符串)
概述 多个字符链接的一串数据. 可以看做成一个字符数组;
特点
字面值字符串“abc"可以看做一个字符串对象
字符串一但创建就无法改变 但引用变量可以改变
String的构造方法
String():空构造
String(byte[] bytes):把字节数组转成字符串
String(byte[] bytes,int index,int length):字节数组的一部分转成字符串(index:表示的是从第几个索引开始, length表示的是长度)
String(char[] value):字符数组转成字符串
String(char[] value,int index,int count):字符数组的一部分转成字符串
String(String original):字符串常量值转成字符串
判断功能
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 int length(): 获取字符串的长度。
public char charAt(int index): 获取指定索引位置的字符
public int indexOf(int ch): 返回指定字符在此字符串中第一次出现处的索引。如果没有,返回为-1.
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): 从指定位置开始到指定位置结束截取字符串。
转换功能:
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 String replace(char old,char new)将指定字符进行互换
public String replace(String old,String new)将指定字符串进行互换
去除字符串两空格
public String trim()去除两端空格
按字典顺序比较两个字符串
public int compareTo(String str)
public int compareToIgnoreCase(String str)
案例 1,模拟登录,给三次机会,并提示还有几次
字符串的遍历 charAt(int index)和length();
统计不同类型字符个数 需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数
相关问题
String s = new String(“hello”)和String s = “hello”;的区别
String s = new String(“hello”) 两个对象,一个是方法区常量池,一个在堆内存
String s = “hello”; 一个对象,一个是方法区常量池
==和equals()的区别
== 比较基本数据,也比较引用数据的地址值
equals() 比较引用数据,默认比较地址值 String中比较的是字符串内容是否相同