1、Scanner
概述:简单的文本扫描器
next()String(直接获取内容,不介绍回车键)
nextLine String(获取输入的返回值)
nextByte() byte
nextShort()short
nextInt()int
nextLong()Long
nextFloat()float
nextDouble()double
nextBoolean() boolean
Scanner的构造方法原理
Scanner(InputStream source)
system类下有一个静态字段
public static final InputStream in;标准输入流,对应键输入
一般方法:
hasNextXxx()判断是否由下一个输入项,其中Xxx是类型,如果判断是否包含下一个字符串,可以省略Xxx。
nextXxx()获取下一个输入项,Scanner使用空格和回车作为分隔符
Scanner获取数据问题及解决方案(掌握)
常用方法:
public int nextInt();获取一个int类型的值
public String nextLine();获取一个String类型的值
nextLine式键盘录入整数方法,当我们输入10时,底层会得到10\r\n其中,\r\n代表回车,nextLine判断是否结束,收到\r\n就会停止执行
问题(连续输出不同类型字符时,会出现错误接收回车键为输入内容)
解决方法:
1.创建多个Scanner对象,进行特点数据接收
弊端:空间浪费,创建了多个对象
2,。键盘录入的都是字符串,都要nextLine方法,后面再进行其他类型转换
String类讲解
String 字符串(定义不可改变,可覆盖)
StringBuffer(字符池,可以改变大小)
StringBuiledr(字符池,可以改变大小)
1,字符串字面值“abc”也可以看成一个字符池对象
2.字符串是常量,一旦被赋值,就不可被改变
String类构造方法(掌握)
public tringS();空构造方法
public String(byte【】bytes);把字节数组转换为字符串
public String(byte【】bytes ,int index,int length);把字节数组的一部分换成字符串
public String(char【】value);把字符数组转换成字符串
public String(char【】value ,int index,int length);把字符数组的一部分换成字符串
public String(String original);把字符常量转化为字符串
面试题:判断定义String类型是否相等
String s1="abc"
String s2="abc"
s1==s1(true)
s1.equals(s2)(true)
==比较的是地址在堆中abc会存储在常量池中
equals只比较值。
String s1=new String("abc");
String s2="abc"
s1==s1(false)
s1.equals(s2)(true)
new是新建对象,所以地址不同
String s1="a“+“b”+“c”’
String s2="abc"
s1==s1(true)
s1.equals(s2)(true)
常量优化机制会把"a“+“b”+“c”默认为“abc”
String s1="ab"
String s2="abc"
String s3=s1+“c”(默认为s3=new String(s1+“c”))
s1==s1(false)
s1.equals(s2)(true)
String的判断功能
boolean equals(object obj);比较字符串内容,区分大小写
boolean equalslgnorecase();比较字符串是否相同,不区分大小写
boolean contains(String str);判断字符串是否包含某个字符串
boolean startWith(String str);判断字符串是否以某个自定字符开头
boolean endsWith(String str);判断字符串是否以某个自定字符结尾
boolean isEmpty();判断是否为空
“ ”和null区别
‘’是字符串常量,同时也是一个String类的对象,可以调用String类中的方法
null是空常量,buneng调用任何方法
String获取功能
public class Demo4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1="adabsminminmin";
System.out.println("获取字符串长度");
System.out.println(s1.length());
System.out.println("i在字符串中的位置");
System.out.println(s1.indexOf("i"));
System.out.println("字符串中下标为2的字符");
System.out.println(s1.charAt(2));
System.out.println("返回字符串在mi在s1中的位置");
System.out.println(s1.indexOf("mi"));
System.out.println("返回字符在i在s1中第8个位置开始后的位置");
System.out.println(s1.indexOf("i",8));
System.out.println("返回字符串在mi在s1中第8个位置开始后的位置");
System.out.println(s1.indexOf("mi",8));
System.out.println("返回从下标2字符开始后的字符串");
System.out.println(s1.substring(2));
System.out.println("返回从下标1-3字符开始的字符串");
System.out.println(s1.substring(1,3));
}
}
lsatIndexOf(----)从后往前 ,与indexOf()功能相反。
String的转换功能
byte【】 getBytes();把字符串转换为字节数组
char【】toCharArray();把字符串转换为字符数组。
static String valueOf(char【】chs);把字符数组转换为字符串
static String valueOf(int i);把int类型数据转换为字符串
注意:String类的valueOf方法可以把任意类型的数据转换为字符串。
了解内容
String toLowerCase();把字符串转成小写
String toUpperCase();把字符串转换为小写
String concat(String str);把字符串拼接
package com.demo.cn;
import java.util.Arrays;
public class Demo3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="1234567";
byte a[]=str.getBytes();
String str1="qwerty";
int i=12345;
char c[]=str1.toCharArray();
String str2=String.valueOf(i);
String str3=String.valueOf(c);
byte x[]=str2.getBytes();
System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(c));
System.out.println(str2);
System.out.println(str3);
System.out.println(Arrays.toString(x));
}
}
结果: