1.1 String类概述
通过JDK提供的API,查看String类的说明
- "abc"是String类的一个实例,或者成为String类的一个对象
- 字符串字面值"abc"也可以看成是一个字符串对象
- 字符串是常量,一旦被赋值,就不能被改变
- 字符串本质是一个字符数组
1.2 String类的构造方法
-
String(String original):
- 把字符串数据封装成字符串对象
-
String(char[] value):
- 把字符数组的数据封装成字符串对象
-
String(char[] value, int index, int count):
- 把字符数组的一部分数据封装成字符串对象
1.2.1 常用构造方法演示
package com.itheima_01;
/*
* String:代表字符串类。
* 由多个字符组成的一串数据。
* 字符串的本质就是一个字符数组。
*
* 构造方法:
* String(String original):把字符串数据封装成字符串对象
* String(char[] value):把字符数组的数据封装成字符串对象
* String(char[] value, int index, int count):把字符数组的一部分数据封装成字符串对象
*
* public String toString():返回此对象本身(它已经是一个字符串!)。
*/
public class StringDemo {
public static void main(String[] args) {
//String(String original):把字符串数据封装成字符串对象
String s1 = new String("hello");
System.out.println(s1);
System.out.println("----------");
//String(char[] value):把字符数组的数据封装成字符串对象
char[] value = {'h','e','l','l','o'};
String s2 = new String(value);
System.out.println(s2);
System.out.println("----------");
//String(char[] value, int index, int count):把字符数组的一部分数据封装成字符串对象
//String s3 = new String(value,0,value.length);
String s3 = new String(value,0,3);
System.out.println(s3);
System.out.println("----------");
//最常用的
String s4 = "hello";
System.out.println(s4);
}
}
1.2.2 创建字符串对象两种方式的区别
String01.png
package com.itheima_02;
/*
* String类创建对象的特点:
* A:通过构造方法创建对象
* B:通过直接赋值的方式创建对象
* 这两种方式的创建是有区别的。
* 通过构造方法创建的字符串对象是在堆内存。
* 通过直接赋值的方式创建的字符串对象是在方法区的常量池。
*/
public class StringDemo {
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = "hello";
String s4 = "hello";
System.out.println(s1 == s2);//false
System.out.println(s1 == s3);//false
System.out.println(s3 == s4);//true
}
}
1.3String 的练习之模拟用户登录
- boolean equals(Object obj):
- 比较字符串的内容是否相同
- boolean equalsIgnoreCase(String str):
- 比较字符串的内容是否相同,忽略大小写
1.3.1 模拟用户登录演示
需求:模拟登录,给三次机会,并提示还有几次
1.3.1.1案例代码
package com.itheima_03;
import java.util.Scanner;
/*
* 需求:模拟登录,给三次机会,并提示还有几次
* 分析:
* A:定义两个字符串对象,用于存储已经存在的用户名和密码
* B:键盘录入用户名和密码
* C:拿键盘录入的用户名和密码去跟已经存在的用户名和密码进行比较
* 如果内容相同,就提示登录成功
* 如果内容不同,就提示登录失败,并提示还有几次机会
* public boolean equals(Object anObject):比较字符串的内容,严格区分大小写(用户名和密码)
* public boolean equalsIgnoreCase(String anotherString):比较字符串的内容,不考虑大小写(验证码)
*/
public class StringTest {
public static void main(String[] args) {
//定义两个字符串对象,用于存储已经存在的用户名和密码
String username = "admin";
String password = "admin";
for(int x=0; x<3; x++) {
//键盘录入用户名和密码
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String name = sc.nextLine();
System.out.println("请输入密码:");
String pwd = sc.nextLine();
//拿键盘录入的用户名和密码去跟已经存在的用户名和密码进行比较
if(username.equals(name) && password.equals(pwd)) {
System.out.println("登录成功");
break;
}else {
if((2-x) == 0){
System.out.println("你的帐号被锁定,请与管理员联系");
}else {
System.out.println("登录失败,你还有"+(2-x)+"次机会");
}
}
}
}
}
1.4 String类的获取功能
- public char charAt(int index):返回指定索引处的值
- public int length():返回字符串中的字符个数,字符串的长度
1.4.1 遍历字符串的演示
package com.itheima_03;
/*
* 需求:遍历字符串(获取字符串中的每一个字符)
*/
public class StringTest2 {
public static void main(String[] args) {
//要遍历字符串,你首先得有一个字符串
String s = "abcde";
//思考:如何获取字符串中的每一个字符
//假如让我们来提供方法,我们应该提供一个根据索引返回指定位置的字符的方法
//返回值:char
//形式参数:int index
//public char charAt(int index):返回指定索引处的值
//原始做法
System.out.println(s.charAt(0));
System.out.println(s.charAt(1));
System.out.println(s.charAt(2));
System.out.println(s.charAt(3));
System.out.println(s.charAt(4));
System.out.println("-------------------");
//用for循环改进
for(int x=0; x<5; x++) {
System.out.println(s.charAt(x));
}
System.out.println("-------------------");
//目前for循环中的数据5是我们数出来的,那么字符串有没有提供一个方法,用于获取字符串中字符的个数呢?
//public int length():返回字符串中的字符个数,字符串的长度
for(int x=0; x<s.length(); x++) {
System.out.println(s.charAt(x));
}
}
}
1.5对字符串的拼接案例
package com.itheima_03;
/*
* 需求:把数组中的数据按照指定个格式拼接成一个字符串
* 举例:int[] arr = {1,2,3};
* 输出结果:[1, 2, 3]
*
* 分析:
* A:定义一个int类型的数组
* B:写方法实现把数组中的元素按照指定的格式拼接成一个字符串
* C:调用方法
* D:输出结果
*/
public class StringTest3 {
public static void main(String[] args) {
//定义一个int类型的数组
int[] arr = {1,2,3};
//写方法实现把数组中的元素按照指定的格式拼接成一个字符串
//调用方法
String result = arrayToString(arr);
//输出结果
System.out.println("result:"+result);
}
/*
* 两个明确:
* 返回值类型:String
* 参数列表:int[] arr
*/
public static String arrayToString(int[] arr) {
String s = "";
//[1, 2, 3]
s+="[";
for(int x=0; x<arr.length; x++) {
if(x==arr.length-1) {
s += arr[x];
}else {
s += arr[x];
s += ", ";
}
}
s+="]";
return s;
}
}
1.6 String 类的反转案例
package com.itheima_03;
import java.util.Scanner;
/*
* 需求:字符串反转
* 举例:键盘录入”abc”
* 输出结果:”cba”
*
* 分析:
* A:键盘录入字符串数据
* B:写方法实现字符串数据的反转
* 把字符串倒着遍历,在把每一个得到的字符拼接成一个字符串
* C:调用方法
* D:输出结果
*/
public class StringTest4 {
public static void main(String[] args) {
//键盘录入字符串数据
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String s = sc.nextLine();
//写方法实现字符串数据的反转
//调用方法
String result = reverse(s);
//输出结果
System.out.println("result:"+result);
}
/*
* 两个明确:
* 返回值类型:String
* 参数列表:String s
*/
public static String reverse(String s) {
//把字符串倒着遍历,在把每一个得到的字符拼接成一个字符串
String ss = "";
for(int x=s.length()-1; x>=0; x--) {
ss += s.charAt(x);
}
return ss;
}
}








网友评论