看成员方法(没有static关键字修饰,只能由对象调用):
(1)方法名称:
(2)返回值类型:
(3)参数列表:
API的使用步骤:
1.查看类 java.util.Scanner :该类需要import导入后使用。
2.查看构造方法
public Scanner(InputStream source) :
构造一个新的 Scanner ,它生成的值是从指定的输入流扫描的。
3.查看成员方法
public String next() :
获取键盘录入的字符串(不能包含空白字符),遇到空白字符/回车换行就认为录入结束
public String nextLine() :
获取键盘录入的一行字符串(可以包含空白字符),遇到回车换行就认为录入结束
String类: 用来描述的字符串的,字符串是常量
String类的对象的体现形式:
1.所有双引号""引起来的内容,都是String类的对象,都可以调用String类中定义的方法
2.和前面一样,使用new关键字创建String类的对象
##### String类的构造方法
String str = "abc";
相当于: char data[] = {'a', 'b', 'c'};
String str = new String(data);
public String(): 创建一个空白字符串对象,不含有任何内容,等价于""
String s1 = new String();
System.out.println("Hello"+s1+"World");
char[] chs = {'a','b','c'};
String s2 = new String(chs);
System.out.println(s2);
byte[] bs = {97,98,99};
String s3 = new String(bs);
System.out.println(s3);
用户登录案例
需求:
已知用户名和密码,请用程序实现模拟用户登录。总共给三次机会,登录之后,给出相应的提示
如果用户名和密码都正确: 提示"登录成功"
如果用户名或者密码不正确: 提示"登录失败",同时提示"剩余xx次机会,请珍惜"
public class Demo04Login {
public static void main(String[] args) {
String rUser = "abc", rPass = "123";
Scanner sc = new Scanner(System.in);
for (int times = 1; times <= 3; times++) {
System.out.println("请输入用户名: ");
String inUser = sc.next();
System.out.println("请输入密码: ");
String inPass = sc.next();
if ((inUser.equals(rUser)) && (inPass.equals(rPass))) {
System.out.println("登录成功");
break;
} else {
System.out.println("用户名或者密码不正确,登录失败");
if (times == 3) {
System.out.println("您的免费试用3次的机会已经用完,拜拜.....");
} else {
System.out.println("剩余" + (3 - times) + "次机会,请珍惜");
}
}
}
}
}
获取功能的方法
- public int length () :返回此字符串的长度。
举例:s.length() 获取s中的字符的数量
- public String concat (String str) :将指定的字符串连接到该字符串的末尾。
举例:s1.concat(s2) 把s2连接到s1的末尾 等价于 s1 += s2
- public char charAt (int index) :返回指定索引处的 char值。
举例:s1.charAt(5) 获取s1中索引为5的字符
- public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
举例:s1.indexOf(s2) 查找s2在s1中第一次出现的位置,如果不存在,返回-1
- public String substring (int beginIndex) :
返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。
举例:s1.substring(5) 截取s1字符串从索引5开始一直到最后的内容
- public String substring (int beginIndex, int endIndex) :
返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。
举例:s1.substring(5,10) 截取s1字符串从索引5开始一直到索引10之间的内容(包含5,不包含10)
String s1 = "Hello World";
System.out.println(s1.length());
System.out.println("".length());
需求: 键盘录入一个字符串,使用程序实现在控制台遍历该字符串
public class Demo06BianLi {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
System.out.println(ch);
}
}
}
##### 拼接字符串
定义一个方法,把int数组中的数据按照指定格式拼接成一个字符串返回,
调用该方法, 并在控制台输出结果。
例如 数组为 int[] arr = {1,2,3}; 执行方法后的输出结果为
:[1, 2, 3]
三要素:
1.方法名称: myToString
2.是否有未知数据参加运算: int[] array
3.是否产生结果数据: String
*/
public class Demo07PinJie {
public static void main(String[] args) {
int[] array = {1, 2, 3};
String s = myToString(array);
System.out.println(s);
}
public static String myToString(int[] array) {
String s = "[";
for (int i = 0; i < array.length; i++) {
s = s + array[i];
if (i != array.length - 1) {
s = s + ", ";
}
}
s = s + "]";
return s;
}
}
public class Demo04StringConvert {
public static void main(String[] args) {
String s1 = "abc";
byte[] bs = s1.getBytes();
for (int i = 0; i < bs.length; i++) {
System.out.print(bs[i]+" ");
}
System.out.println("----------------");
String s2 = "我靠,你他妈有病,你个二货,你个傻叉....";
String s3 = s2.replace("靠", "*");
System.out.println(s3);
String s4 = s3.replace("他妈有病", "****");
System.out.println(s4);
String s5 = s4.replace("二货", "**");
System.out.println(s5);
String s6 = s5.replace("傻叉", "傻不拉几的");
System.out.println(s6);
}
}
统计字符次数 需求: 判断根据ch中的不同类型的字符,给对应的计数器的值增加1
char ch = str.charAt(i);
smallCount++;
创建字符串对象两种方式的区别(理解)
1.直接new的方式: 字符串变量中直接保存的对内存空间的地址值
String s1 = new String(chs);
2.直接""的方式: 字符串变量中直接保存的常量池中字符串常量对象的地址值
String s3 = "abc";
==equals的使用:
1.基本类型: 比较的是具体的数据值是否相同
String类的切割方法
String s1 = "aaa,bbb,ccc,ddd";
String[] "a,b,c,d".split(",")