一、Scanner(用于接收键盘录入数据)
(一)概述和构造方法原理
Scanner(InputStream source)
//System类下有一个静态的字段:
public static final InputStream in; //标准的输入流,对应着键盘录入
(二)常见对象
hasNextXxx() 判断下一个是否是某种类型的元素,其中Xxx可以是Int,Double等。
如果需要判断是否包含下一个字符串,则可以省略Xxx
nextXxx() 获取下一个输入项。Xxx的含义和上个方法中的Xxx相同
Scanner scanner = new Scanner(in);
//nextXXXX 系列
int i = scanner.nextInt();
scanner.nextDouble();
scanner.nextLong();
System.out.println("请输入一段字符串");
String s = scanner.nextLine(); //此方法用来录入用户输入的字符串
String s = scanner.next();
System.out.println(s);
// nextLine 和 next 的区别? nextLine方法录入的文本可以包含空格 next方法,遇到第一个空格,空格后面的文本就不录入了
//注意
//先录入整数,再用nextLine()录入字符串时有个小bug,字符串就无法录入了,解决方法,可以在录入字符串时,再创建一个新的Scanner对象
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数");
int i = sc.nextInt();
sc = new Scanner(System.in);
System.out.println("请输入一个字符串");
String s = sc.nextLine();
System.out.println(s);
二、String类的概述和构造
(一)
字符串:字符串是由多个字符组成的一串数据(字符序列);字符串可以看成是字符数组
(二)特点
//定义一个字符串
String s = "hello";
s = "world" + "java";
System.out.println(s);
// 字符串是常量;它们的值在创建之后不能更改。
// String()
// 初始化一个新创建的 String 对象,使其表示一个空字符序列。
// "" 空字符串
String s = new String(); //"" 创建了一个空字符串对象
//字符串也用索引,从0开始
String s2 = new String("法法师打发舒服点");
int length = s2.length(); //获取字符串的长度
System.out.println(length);
//String 类重写了toString方法,打印的是字符串的内容
System.out.println(s2);
(三)常见构造方法
public String():空构造
public String(byte[] bytes):把字节数组转成字符串
public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串(index:表示的是从第几个索引开始, length表示的是长度)
public String(char[] value):把字符数组转成字符串
public String(char[] value,int index,int count):把字符数组的一部分转成字符串
public String(String original):把字符串常量值转成字符串
(四)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 ():判断字符串的内容是否为空串 ""。
boolean b = "abc".equals("abC"); //区分大小写的比较
System.out.println(b);
System.out.println("abc".equalsIgnoreCase("ABC")); //不区分大小写的比较
boolean b1 = "七月的风,八月的雨".contains("八月的雨");
System.out.println(b1);
System.out.println("七月的风,八月的雨".startsWith("七月的风"));
System.out.println("七月的风,八月的雨".endsWith("八月的雨"));
String s="";//定义一个空串
boolean empty = s.isEmpty();
System.out.println(empty);
if(s.length()==0){
System.out.println("是空串");
}else{
System.out.println("不是空串");
}
(五)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): 返回指定字符串在此字符串中从指定位置后第一次出现处的索引。可以顺带提一下lastIndexOf系列
public String substring(int start): 从指定位置开始截取字符串,默认到末尾。
public String substring(int start,int end): 从指定位置开始到指定位置结束截取字符串。
举例:
需求:模拟登录, 给三次机会, 并提示还有几次。
public class MyTest {
public static void main(String[] args) {
String name = "张三";
String password = "123456";
//判断用户输入的跟我们定义的一样不
for (int i = 1; i <= 3; i++) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的用户名");
String uName = scanner.nextLine();
System.out.println("请输入你的密码");
String pPwd = scanner.nextLine();
if (name.equals(uName) && password.equals(pPwd)) {
System.out.println("登录成功");
break;
} else {
if (i == 3) {
System.out.println("你的账户以被锁定,请明天再来");
} else {
System.out.println("用户名或密码错误,请重写输入。你还是" + (3 - i) + "次机会");
}
}
}
}
}
(六)字符串的遍历
String s="猴子在玩吃鸡";
//遍历字符串
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
System.out.println(c);
}
(七)统计不同类型字符个数
举例:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
//统计字符串中大写字母,小写字母,数字的个数
String s = "AbcdeFdfdgDKief10019FDFD002323343";
//定义统计变量
int xiao = 0;
int da = 0;
int num = 0;
for (int i = 0; i < s.length(); i++) {
//截取每一个字符
char ch = s.charAt(i);
if (ch >= 'a' && ch <= 'z') {
xiao++;
} else if (ch >= 'A' && ch <= 'Z') {
da++;
} else if (ch >= '0' && ch <= '9') {
num++;
}
}
System.out.println("小写字母有" + xiao + "个");
System.out.println("大写字母有" + da + "个");
System.out.println("数字有" + num + "个");
(八)String类的转换功能
//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):把字符串拼接。
//把字节数组装成字符串 通过String的构造方法
//把字符串准成字节数组
byte[] bytes = "我爱你".getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
String s = new String(bytes);
System.out.println(s);
//把字符数组转换成字符串,通过String的构造
//把字符串,转换成字符数组
String s1="我爱你";
char[] chars = s1.toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.println(chars[i]);
}
System.out.println(new String(chars,0,2));
案例:需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
public class MyTest {
public static void main(String[] args) {
String s = "aaAbdfesefAAfefefe";
//链式编程
s = s.substring(0, 1).toUpperCase().concat(s.substring(1).toLowerCase());
System.out.println(s);
}
}
(九)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)跟上面一样 只是忽略大小写的比较
String s = "abc".replace('a', 'A');
System.out.println(s);
String s1 = "奥巴马是美国总统".replace("奥巴马", "***");
System.out.println(s1);
String s2 = "奥巴马是美国总统特朗普也是美国总统".replace("奥巴马", "*").replace("特朗普", "*");
System.out.println(s2);
//去掉字符串中的前后空格
String trim = " aaaa ".trim();
System.out.println(trim);