String类

Scanner的概述: JDK5以后用于获取用户的键盘输入
Scanner的构造方法原理
Scanner(InputStream source)
System类下有一个静态的字段:
public static final InputStream in; 标准的输入流,对应着键盘录入。

基本格式

**hasNextXxx()**  判断下一个是否是某种类型的元素,其中Xxx可以是Int,Double等。
			  如果需要判断是否包含下一个字符串,则可以省略Xxx
**nextXxx()**  获取下一个输入项。Xxx的含义和上个方法中的Xxx相同

String类的构造方法

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的方法中转就行了。而字符串一旦被创建就不能改变 因为字符串的值是在方法区的常量池中划分空间 和分配地址值的。

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(): 判断字符串的内容是否为空串""。

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): 返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
public String substring(int start): 从指定位置开始截取字符串,默认到末尾。
public String substring(int start,int end): 从指定位置开始到指定位置结束截取字符串。
这其中就比较重要了,它们几个可以随时调用来查看字符串中的字符以及数字并且提取出来,而后面的街区功能可以截取我们想要的字符串。

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): 把字符串拼接。
public String trim() 去除两端空格
public int compareTo(String str) 会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果
public String replace(char old,char new) 将指定字符进行互换
public String replace(String old,String new) 将指定字符串进行互换

例题

A:案例演示: 需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
public static void main(String[] args) {
//统计字符串中大写字母,小写字母,数字的个数
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 + “个”);
}
B:案例演示: 需求:模拟登录,给三次机会,并提示还有几次。
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) + “次机会”);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值