String 类+Excel版
String 类+Excel版
1) 概述:
Java.lang包下的,用final修饰
字符串是一个常量,也是一个字符串对象.
字符串一旦初始化,其值就不能发生改变.
字符串的底层其实是一个字符数组.
String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现
String类是Java.lang包下的类不用导包.
-
public class Demo1_String {
-
public static void main(String[] args) {
-
String str = "abc"; //"abc"可以看做一个字符串对象
-
str = "def";//当把"def"赋值给str,原来的"abc"就变成了垃圾
-
"abc"代表对象,把地址值赋给str, "def"也是对象,把地址值又赋给了str
-
System.out.println(str);//打印的结果不是"类名@字符串地址",而是"abc",说明String重写了toString()方法
-
-
}
-
-
}
2)构造方法(创建String类对象,参数用来赋值)
public String();
public String(String str);
public String(char[] chs);
public String(char[] chs,int offSet,int count); //offSet代表开始的索引,count代表要拿几个
public String(byte[] bys);
public String(byte[] bys,int offSet,int count); //offSet代表开始的索引,count代表要拿几个
public String(StringBuffer sb);
public String(StringBuilder sb);
-
public class Demo2_StringCon {
-
public static void main(String[] args) {
-
String s1 = new String();
-
System.out.println(s1);//(空串什么都没有)
-
-
byte[] arr1 = {97,98,99};
-
String s2 = new String(arr1); //解码,将计算机读的懂的转换成我们读的懂(用的是当前项目默认的码表)(平台默认字符集)
-
System.out.println(s2);
-
-
byte[] arr2 = {97,98,99,100,101,102};
-
String s3 = new String(arr2,2,3); //将arr2字节数组从2索引开始转换3个
-
System.out.println(s3);
-
-
char[] arr3 = {'a','b','c','d','e'}; //将字符数组转换成字符串
-
String s4 = new String(arr3);
-
System.out.println(s4);
-
-
String s5 = new String(arr3,1,3); //将arr3字符数组,从1索引开始转换3个
-
System.out.println(s5);
-
-
String s6 = new String("kailing");
-
System.out.println(s6);
-
}
-
-
}
3)一般常用成员方法(总结Excel版)-需要Excel版的留言给我
4)String类的判断功能
A:String类的判断功能
-
boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
-
boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
-
boolean contains(String str):判断大字符串中是否包含小字符串
-
boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
-
boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
-
boolean isEmpty():判断字符串是否为空。
""和null的区别
""是空字符串,同时也是String类的对象,可以调用String类的方法
Null是空常量,不能调用任何方法,否则会出现空指针异常
案例:
-
public class Demo4_StringMethod {
-
public static void main(String[] args) {
-
//demo1();
-
//demo2();
-
String s1 = "kailing";
-
String s2 = "";
-
String s3 = null;没有记录任何字符串对象
-
-
System.out.println(s1.isEmpty());
-
System.out.println(s2.isEmpty());
-
System.out.println(s3.isEmpty()); //java.lang.NullPointerException
-
}
-
-
private static void demo2() {
-
String s1 = "我爱kailing,哈哈";
-
String s2 = "kailing";
-
String s3 = "baima";
-
String s4 = "我爱";
-
String s5 = "哈哈";
-
-
System.out.println(s1.contains(s2)); //判断是否包含传入的字符串
-
System.out.println(s1.contains(s3));
-
-
System.out.println("------------------");
-
System.out.println(s1.startsWith(s4)); //判断是否以传入的字符串开头
-
System.out.println(s1.startsWith(s5));
-
-
System.out.println("------------------");
-
System.out.println(s1.endsWith(s4)); //判断是否以传入的字符串结尾
-
System.out.println(s1.endsWith(s5));
-
}
-
-
private static void demo1() {
-
String s1 = "kailing";
-
String s2 = "kailing";
-
String s3 = "Kailing";
-
-
System.out.println(s1.equals(s2)); //true
-
System.out.println(s2.equals(s3)); //false
-
-
System.out.println("---------------");
-
-
System.out.println(s1.equalsIgnoreCase(s2));
-
System.out.println(s1.equalsIgnoreCase(s3)); //不区分大小写
-
}
-
-
}
模拟用户登录
需求:模拟登录,给三次机会,并提示还有几次。
用户名和密码都是admin
案例:
-
import java.util.Scanner;
-
public class Test1 {
-
/
-
-
需求:模拟登录,给三次机会,并提示还有几次。
-
用户名和密码都是admin
-
分析:
-
1,模拟登录,需要键盘录入,Scanner
-
2,给三次机会,需要循环,for
-
3,并提示有几次,需要判断,if
-
/
-
public static void main(String[] args) {
-
Scanner sc = new Scanner(System.in); //创建键盘录入对象
-
-
for(int i = 0; i < 3; i++) {
-
System.out.println("请输入用户名:");
-
String userName = sc.nextLine(); //将键盘录入的用户名存储在userName中
-
System.out.println("请输入密码:");
-
String password = sc.nextLine(); //将键盘录入的密码存储在password中
-
-
//如果是字符串常量和字符串变量比较,通常都是字符串常量调用方法,将变量当作参数传递,防止空指针异常
-
if("admin".equals(userName) && "admin".equals(password)) {
-
System.out.println("欢迎" + userName + "登录");
-
break; //跳出循环
-
}else {
-
if(i == 2) {
-
System.out.println("您的错误次数已到,请明天再来吧");
-
}else {
-
System.out.println("录入错误,您还有" + (2-i) + "次机会");
-
}
-
}
-
-
}
-
}
-
-
}
实现方式二:
-
/
-
-
需求:模拟登录,给三次机会,并提示还有几次。
-
用户名和密码都是admin
-
@author JX
-
-
/
-
public class ScannerTest {
-
public static void main(String[] args) {
-
Scanner sc = new Scanner(System.in);
-
int count = 3;
-
while(true) {
-
System.out.println("请输入用户名:");
-
String userName = sc.nextLine();
-
System.out.println("请输入密码:");
-
String password = sc.nextLine();
-
if("admin".equals(userName)&&"admin".equals(password)) {
-
System.out.println("欢迎光临:"+userName);
-
break;
-
} else {
-
if(--count==0) {
-
System.out.println("登录次数已到,请明天再来吧!");
-
break;
-
} else{
-
System.out.println("您还有"+(count)+"机会!");
-
}
-
}
-
}
-
}
-
}
5)String类的获取功能
A:String类的获取功能
-
int length():获取字符串的长度。
-
char charAt(int index):获取指定索引位置的字符
-
int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
-
int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
-
int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
-
int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
-
lastIndexOf
-
String substring(int start):从指定位置开始截取字符串,默认到末尾。
-
String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。
案例:
-
ublic class Demo5_StringMethod {
-
public static void main(String[] args) {
-
//demo1();
-
//demo2();
-
//demo3();
-
//demo4();
-
String s = "woaikailing";
-
s.substring(4); //subString会产生一个新额字符串,需要将新的字符串记录
-
System.out.println(s);//打印的是原来的s
-
}
-
-
private static void demo4() {
-
String s1 = "kailingwudi";
-
String s2 = s1.substring(5);//默认到末尾
-
System.out.println(s2);
-
-
String s3 = s1.substring(0, 5); //包含头,不包含尾,左闭右开
-
System.out.println(s3);
-
}
-
-
private static void demo3() {
-
String s1 = "woaikailing";
-
int index1 = s1.indexOf('a', 3); //从指定位置开始向后找
-
System.out.println(index1);
-
-
int index2 = s1.lastIndexOf('a'); //从后向前找,第一次出现的字符
-
System.out.println(index2);
-
-
int index3 = s1.lastIndexOf('a', 7); //从指定位置向前找
-
System.out.println(index3);
-
}
-
-
private static void demo2() {
-
String s1 = "kailing";
-
int index = s1.indexOf('e'); ????????????//参数接收的是int类型的,传递char类型的会自动提升
-
System.out.println(index);
-
-
int index2 = s1.indexOf('z'); //如果不存在返回就是-1
-
System.out.println(index2);
-
-
int index3 = s1.indexOf("ma"); //获取字符串中第一个字符出现的位置
-
System.out.println(index3);
-
-
int index4 = s1.indexOf("ia");
-
System.out.println(index4);
-
}
-
-
private static void demo1() {
-
//int[] arr = {11,22,33};
-
//System.out.println(arr.length); //数组中的length是属性
-
String s1 = "kailing";
-
System.out.println(s1.length()); //length()是一个方法,获取的是每一个字符的个数
-
String s2 = "你要减肥,造吗?";
-
System.out.println(s2.length());
-
-
char c = s2.charAt(5); //根据索引获取对应位置的字符
-
System.out.println(c);
-
char c2 = s2.charAt(10); //StringIndexOutOfBoundsException字符串索引越界异常
-
System.out.println(c2);
-
}
-
-
}
字符串的遍历
需求:遍历字符串
案例:
-
public class Test2 {
-
/
-
-
需求:遍历字符串
-
/
-
public static void main(String[] args) {
-
String s = "kailing";
-
-
for(int i = 0; i < s.length(); i++) { //通过for循环获取到字符串中每个字符的索引
-
/char c = s.charAt(i);
-
System.out.println(c);/
-
System.out.println(s.charAt(i)); //通过索引获取每一个字符
-
}
-
}
-
-
}
统计不同类型字符个数
需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数,其他字符出现的次数。
ABCDEabcd123456!@#$%^
案例:
-
public class Test3 {
-
/
-
分析:字符串是有字符组成的,而字符的值都是有范围的,通过范围来判断是否包含该字符
-
如果包含就让计数器变量自增
-
/
-
public static void main(String[] args) {
-
String s = "ABCDEabcd123456!@#$%^";
-
int big = 0;
-
int small = 0;
-
int num = 0;
-
int other = 0;
-
//1,获取每一个字符,通过for循环遍历
-
for(int i = 0; i < s.length(); i++) {
-
char c = s.charAt(i); //通过索引获取每一个字符
-
//2,判断字符是否在这个范围内
-
if(c >= 'A' && c <= 'Z') {
-
big++; //如果满足是大写字母,就让其对应的变量自增
-
}else if(c >= 'a' && c <= 'z') {
-
small++;
-
}else if(c >= '0' && c <= '9') {
-
num++;
-
}else {
-
other++;
-
}
-
}
-
-
//3,打印每一个计数器的结果
-
System.out.println(s + "中大写字母有:" + big + "个,小写字母有:" + small + "个,数字字符:"
-
+ num + "个,其他字符:" + other + "个");
-
}
-
-
}
6)String类的转换功能
A: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):把字符串拼接。
案例:
-
import com.itkailing.bean.Person;
-
public class Demo6_StringMethod {
-
public static void main(String[] args) {
-
//demo1();
-
//demo2();
-
//demo3();
-
String s1 = "kailing";
-
String s2 = "chengxuYUAN";
-
String s3 = s1.toLowerCase();
-
String s4 = s2.toUpperCase();
-
-
System.out.println(s3);
-
System.out.println(s4);
-
-
System.out.println(s3 + s4); //用+拼接字符串更强大,可以用字符串与任意类型相加
-
System.out.println(s3.concat(s4)); //concat方法调用的和传入的都必须是字符串
-
}
-
-
private static void demo3() {
-
char[] arr = {'a','b','c'};
-
String s = String.valueOf(arr); //底层是由String类的构造方法完成的(底层是new对象来做的)
-
valueOf(arr),是静态方法直接类名点调用即可,
-
System.out.println(s);
-
-
String s2 = String.valueOf(100); //将100转换为字符串
-
System.out.println(s2 + 100);
-
-
Person p1 = new Person("张三", 23);
-
System.out.println(p1);
-
String s3 = String.valueOf(p1); //调用的是对象的toString方法
-
System.out.println(s3);
-
}
-
-
private static void demo2() {
-
String s = "kailing";
-
char[] arr = s.toCharArray(); //将字符串转换为字符数组
-
-
for (int i = 0; i < arr.length; i++) {
-
System.out.print(arr[i] + "");
-
}
-
}
-
-
private static void demo1() {
-
String s1 = "abc";
-
byte[] arr = s1.getBytes();
-
for (int i = 0; i < arr.length; i++) {
-
//System.out.print(arr[i] + " ");
-
}
-
-
String s2 = "你好你好";
-
byte[] arr2 = s2.getBytes(); //通过gbk码表将字符串转换成字节数组
-
for (int i = 0; i < arr2.length; i++) { //编码:把我们看的懂转换为计算机看的懂得
-
//System.out.print(arr2[i] + " "); //gbk码表一个中文代表两个字节
-
} //gbk码表特点,中文的第一个字节肯定是负数
-
-
String s3 = "琲";
-
byte[] arr3 = s3.getBytes();
-
for (int i = 0; i < arr3.length; i++) {
-
System.out.print(arr3[i] + "");
-
}
-
}
-
-
}
按要求转换字符
需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
案例:
-
public class Test4 {
-
/
-
-
需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
-
链式编程:只要保证每次调用完方法返回的是对象,就可以继续调用
-
/
-
public static void main(String[] args) {
-
String s = "woaiKailingniaima";
-
String s2 = s.substring(0, 1).toUpperCase().concat(s.substring(1).toLowerCase());
-
System.out.println(s2);
-
}
-
-
}
把数组转成字符串
需求:把数组中的数据按照指定个格式拼接成一个字符串
举例:
int[] arr = {1,2,3};
输出结果:
"[1, 2, 3]"
案例:
-
public class Test5 {
-
-
/
-
分析:
-
1,需要定义一个字符串"["
-
2,遍历数组获取每一个元素
-
3,用字符串与数组中的元素进行拼接
-
/
-
public static void main(String[] args) {
-
int[] arr = {1,2,3};
-
String s = "["; //定义一个字符串用来与数组中元素拼接
-
-
for (int i = 0; i < arr.length; i++) { //{1,2,3}
-
if(i == arr.length - 1) {
-
s = s + arr[i] + "]"; //[1, 2, 3]
-
}else {
-
s = s + arr[i] + ", "; //[1, 2,
-
}
-
}
-
-
System.out.println(s);
-
}
-
}
7)String类的其他功能
A:String的替换功能及案例演示
-
String replace(char old,char new)
-
String replace(String old,String new)
B:String的去除字符串两空格及案例演示
-
String trim()
C:String的按字典顺序比较两个字符串及案例演示
-
int compareTo(String str) 该比较基于字符串中各个字符的 Unicode 值
-
如果这两个字符串不同,那么它们要么在某个索引处的字符不同(该索引对二者均为有效索引),要么长度不同,
-
1. 如果它们在一个或多个索引位置上的字符不同,假设 k 是这类索引的最小值;则在位置 k 上具有较小值的那个字符串(使用 < 运算符确定),其字典顺序在其他字符串之前。在这种情况下,compareTo 返回这两个字符串在位置 k 处两个char 值的差,即值:this.charAt(k)-anotherString.charAt(k)
-
2.如果没有字符不同的索引位置,则较短字符串的字典顺序在较长字符串之前。在这种情况下,compareTo 返回这两个字符串长度的差,即值:this.length()-anotherString.length()
-
(返回时int数,大就是正数,相等0,小于负数)(第一个一样就去比较第二个,) 按照码表值比较(char的转换都是Unicode,)
-
int compareToIgnoreCase(String str)(了解)
案例:
-
public class Demo7_StringMethod {
-
public static void main(String[] args) {
-
//demo1();
-
//demo2();
-
demo3();
-
}
-
-
private static void demo3() {
-
String s1 = "a";
-
String s2 = "aaaa";
-
-
int num = s1.compareTo(s2); //按照码表值比较
-
System.out.println(num);
-
-
String s3 = "黑";
-
String s4 = "马";
-
int num2 = s3.compareTo(s4);
-
System.out.println('黑' + 0); //查找的是unicode码表值
-
System.out.println('马' + 0);
-
System.out.println(num2);
-
-
String s5 = "kailing";
-
String s6 = "KAILING";
-
int num3 = s5.compareTo(s6);
-
System.out.println(num3);
-
-
int num4 = s5.compareToIgnoreCase(s6);
-
System.out.println(num4);
-
}
-
-
private static void demo2() {
-
String s = " hei ma ";
-
String s2 = s.trim();
-
System.out.println(s2);
-
}
-
-
private static void demo1() {
-
String s = "kailing";
-
String s2 = s.replace('i', 'o'); //用o替换i
-
System.out.println(s2);
-
-
String s3 = s.replace('z', 'o'); //z不存在,保留原字符不改变
-
System.out.println(s3);
-
-
String s4 = s.replace("ei", "ao");
-
System.out.println(s4);
-
}
-
-
}
字符串反转
需求:把字符串反转
举例:键盘录入"abc"
输出结果:"cba"
案例:
-
import java.util.Scanner;
-
public class Test6 {
-
/
-
分析:
-
1,通过键盘录入获取字符串Scanner
-
2,将字符串转换成字符数组
-
3,倒着遍历字符数组,并再次拼接成字符串
-
4,打印
-
/
-
public static void main(String[] args) {
-
Scanner sc = new Scanner(System.in); //创建键盘录入对象
-
System.out.println("请输入一个字符串:");
-
String line = sc.nextLine(); //将键盘录入的字符串存储在line中
-
-
char[] arr = line.toCharArray(); //将字符串转换为字符数组
-
-
String s = "";
-
for(int i = arr.length-1; i >= 0; i--) { //倒着遍历字符数组
-
s = s + arr[i]; //拼接成字符串
-
}
-
-
System.out.println(s);
-
}
-
-
}
在大串中查找小串出现的次数
统计大串中小串出现的次数
案例:
-
public class Test7 {
-
public static void main(String[] args) {
-
//定义大串
-
String max = "woaikailing,kailingbutongyubaima,wulunkailinghaishibaima,zhaodaogongzuojiushihaoma";
-
//定义小串
-
String min = "kailing";
-
-
//定义计数器变量
-
int count = 0;
-
//定义索引
-
int index = 0;
-
//定义循环,判断小串是否在大串中出现
-
while((index = max.indexOf(min)) != -1) {
-
count++; //计数器自增
-
max = max.substring(index + min.length());
-
}
-
System.out.println(count);
-
}
-
-
}
-
public class Test7 {
-
public static void main(String[] args) {
-
//定义大串
-
String max = "woaikailing,kailingbutongyubaima,wulunkailinghaishibaima,zhaodaogongzuojiushihaoma";
-
//定义小串
-
String min = "kailing";
-
-
//定义计数器变量
-
int count = 0;
-
//定义索引
-
int index = 0;
-
//定义循环,判断小串是否在大串中出现
-
while(max.indexOf(min,index)!= -1) {
-
count++; //计数器自增
-
index = max.indexOf(min,index)+min.length();
-
}
-
-
System.out.println(count);
-
}
-
}