Java学习第十五天笔记
15.01_常见对象(String类的判断功能)(掌握)
A: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(): 判断字符串的内容是否为空串""。
B:案例演示: 案例演示String类的判断功能;
public class MyTest2 {
public static void main(String[] args) {
//判断两个字符串,字面上的内容是否相同,区分大小写
boolean b = "abc".equals("ABC");
System.out.println(b);
//判断两个字符串,字面上的内容是否相同,不区分大小写
boolean b1 = "abc".equalsIgnoreCase("ABC");
System.out.println(b1);
//判断一个字符串是不是空串 ""
boolean b2 = "".isEmpty();
System.out.println(b2);
if ("".length()==0) {
System.out.println("是空串");
}
//判断一个字符串是不是以这个开头
boolean b3 = "王语嫣".startsWith("王语");
System.out.println(b3);
//判断一个字符串是不是以这个结尾
boolean b4 = "段誉".endsWith("段誉");
System.out.println(b4);
//判断一个字符串,是不是包含一个子串
boolean b5 = "我想要带你去浪漫的土耳其".contains("浪漫");
System.out.println(b5);
}
}
15.02_常见对象(模拟用户登录)(掌握)
A:案例演示: 需求:模拟登录,给三次机会,并提示还有几次。
public class MyTest3 {
public static void main(String[] args) {
/*A:
案例演示:
需求:模拟登录, 给三次机会, 并提示还有几次。*/
//用户名和密码,已经从数据库查出来
String username = "张三";
String password = "123456";
//获取用户从键盘输入的用户名和密码,来模拟登陆
Scanner scanner = new Scanner(System.in);
for (int i = 1; i <= 3; i++) {
System.out.println("请输入用户名");
String uname = scanner.nextLine();
System.out.println("请输入密码");
String pwd = scanner.nextLine();
//把用户输入的数据和我们查出来的数据进行比对
if (username.equals(uname) && pwd.equals(password)) {
System.out.println("登陆成功,可以进行取款操作");
break;
} else {
if ((3 - i) == 0) {
System.out.println("登陆失败,次数用完,卡以被回收");
} else {
System.out.println("登陆失败,用户名或密码错误,请重新输入,你还有" + (3 - i) + "次机会");
}
}
}
}
}
15.03_常见对象(String类的获取功能)(掌握)
A: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): 从指定位置开始到指定位置结束截取字符串。
B:案例演示
案例演示String类的获取功能
public class MyTest {
public static void main(String[] args) {
// public int length ():获取字符串的长度。
//根据索引获取单个字符
String s = "abcdefbc我爱你我爱你";
//charAt(索引) 根据索引,来获取这个字符串中的某个字符
char ch = s.charAt(s.length()-1);
System.out.println(ch);
//查找该字符或字符串第一次出现的索引
int index = s.indexOf('我');
System.out.println(index);
//indexOf 从左往右 从开头找
int index2 = s.indexOf("bc");
System.out.println(index2);
//从指定索引处往后查找
int index1 = s.indexOf("bc", 2);
System.out.println(index1);
int index3= s.indexOf("bc", s.indexOf("bc")+1);
System.out.println(index3);
//调用indexOf() 如果没有找到 返回 -1 -1 经常用来代表没找到 我们经常用 -1来作为判断条件
int index4 = s.indexOf("呵呵");
System.out.println(index4);
System.out.println("======================================");
//从后往前找,该字符或字符串第一次出现的索引
String s2="像我这样的人,本该灿烂过一生我";
int index5 = s2.lastIndexOf("我",10);
System.out.println(index5);
//怎么判断一个字符在字符串中只出现过一次
//怎么判断这个坏字 在这个字符串中只出现过一次。
String s3 = "像我这样的人本该灿烂过一生坏我像我这样的人本该灿烂过一生我像我这样的人,本该灿烂过一生我";
System.out.println("================================================");
//截取字符串
String s4 ="我在人民广场吃着炸鸡而此时此刻你在哪里";
//从指定索引处,截取到末尾返回
String s1 = s4.substring(5);
System.out.println(s1);
//如果要截取某一段,可以指定两个索引
String s5 = s4.substring(0, 6); //含头不含尾
System.out.println(s5);
}
}
15.04_常见对象(字符串的遍历)(掌握)
A:案例演示: 需求:遍历字符串
public class MyTest2 {
public static void main(String[] args) {
String s4 = "我在人民广场吃着炸鸡而此时此刻你在哪里";
/* char c = s4.charAt(0);
char c2 = s4.charAt(1);*/
//遍历字符串
/* for(int i=0;i= 0; length--) {
char ch = s4.charAt(length);
System.out.println(ch);
}
}
}
15.05_常见对象(统计不同类型字符个数)(掌握)
A:案例演示: 需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
public class MyTest3 {
public static void main(String[] args) {
/* A:
案例演示:
需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)*/
String str = "asdfaASFfdcfdf144149855001asdfasAFdcdf";
//遍历,遍历途中,就得判断是什么字符,然后统计
//定义统计变量
int xiao = 0;
int da = 0;
int num = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.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("大写字符有" + da + "个");
System.out.println("小写字符有" + xiao + "个");
System.out.println("数字符有" + num + "个");
}
}
15.06_常见对象(String类的转换功能)(掌握)
A: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): 把字符串拼接。
B:案例演示
案例演示String类的转换功能
public class MyTest {
public static void main(String[] args) {
//把一个字符串转换成字节数组
String str = "abcd";
byte[] bytes = str.getBytes();
System.out.println(bytes.length);
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
System.out.println("==========================");
//把字节数组转换成字符串
String s = new String(bytes);
System.out.println(s);
System.out.println("========================");
String string = "我爱你";
//UTF-8 编码一个汉字占三个字节
byte[] bytes1 = string.getBytes();
System.out.println(bytes1.length);
System.out.println("========================");
for (int i = 0; i < bytes1.length; i++) {
System.out.println(bytes1[i]);
}
System.out.println("=============================");
String s1 = new String(bytes1, 0, 9);
System.out.println(s1);
}
}
15.07_常见对象(按要求转换字符)(掌握)
A:案例演示: 需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
public class MyTest5 {
public static void main(String[] args) {
/* A:
案例演示:
需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)*/
String str = "aBDEfdafaaafeDeFDf";
/* String index= str.substring(0, 1);
String s = index.toUpperCase();
String substring = str.substring(1);
substring=substring.toLowerCase();
String concat = s.concat(substring);
System.out.println(concat);*/
//链式编程,一行写完
String s = str.substring(0, 1).toUpperCase().concat(str.substring(1).toLowerCase());
System.out.println(s);
}
}
15.08_常见对象(String类的其他功能)(掌握)
A:String的替换功能及案例演示
public String replace(char old,char new) 将指定字符进行互换
public String replace(String old,String new) 将指定字符串进行互换
B:String的去除字符串两空格及案例演示
public String trim() 去除两端空格
C:String的按字典顺序比较两个字符串及案例演示
public int compareTo(String str) 会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果
如果前面几个字母一样会根据两个字符串的长度进行减法运算返回的就是这个减法的结果
如果连个字符串一摸一样 返回的就是0
public int compareToIgnoreCase(String str) 跟上面一样 只是忽略大小写的比较
public class MyTest {
public static void main(String[] args) {
String str="奥巴马和特朗普是美国总统";
//一次替换一个字符
String s = str.replace('奥', '*');
System.out.println(s);
//一次替换一个字符串
String str2= "奥巴马和特朗普是美国总统";
String s1 = str2.replace("奥巴马", "*").replace("特朗普","我爱你");
System.out.println(s1);
}
}
public class MyTest {
public static void main(String[] args) {
String str="奥巴马和特朗普是美国总统";
//一次替换一个字符
String s = str.replace('奥', '*');
System.out.println(s);
//一次替换一个字符串
String str2= "奥巴马和特朗普是美国总统";
String s1 = str2.replace("奥巴马", "*").replace("特朗普","我爱你");
System.out.println(s1);
}
}
public class MyTest {
public static void main(String[] args) {
String str="奥巴马和特朗普是美国总统";
//一次替换一个字符
String s = str.replace('奥', '*');
System.out.println(s);
//一次替换一个字符串
String str2= "奥巴马和特朗普是美国总统";
String s1 = str2.replace("奥巴马", "*").replace("特朗普","我爱你");
System.out.println(s1);
}
}
public class MyTest {
public static void main(String[] args) {
String str="奥巴马和特朗普是美国总统";
//一次替换一个字符
String s = str.replace('奥', '*');
System.out.println(s);
//一次替换一个字符串
String str2= "奥巴马和特朗普是美国总统";
String s1 = str2.replace("奥巴马", "*").replace("特朗普","我爱你");
System.out.println(s1);
}
}
public class MyTest {
public static void main(String[] args) {
String str="奥巴马和特朗普是美国总统";
//一次替换一个字符
String s = str.replace('奥', '*');
System.out.println(s);
//一次替换一个字符串
String str2= "奥巴马和特朗普是美国总统";
String s1 = str2.replace("奥巴马", "*").replace("特朗普","我爱你");
System.out.println(s1);
}
}
public class MyTest {
public static void main(String[] args) {
String str="奥巴马和特朗普是美国总统";
//一次替换一个字符
String s = str.replace('奥', '*');
System.out.println(s);
//一次替换一个字符串
String str2= "奥巴马和特朗普是美国总统";
String s1 = str2.replace("奥巴马", "*").replace("特朗普","我爱你");
System.out.println(s1);
}
}
public class MyTest {
public static void main(String[] args) {
String str="奥巴马和特朗普是美国总统";
//一次替换一个字符
String s = str.replace('奥', '*');
System.out.println(s);
//一次替换一个字符串
String str2= "奥巴马和特朗普是美国总统";
String s1 = str2.replace("奥巴马", "*").replace("特朗普","我爱你");
System.out.println(s1);
}
}
15.09_常见对象(把数组转成字符串)(掌握)
A:案例演示
需求:把数组中的数据按照指定个格式拼接成一个字符串
举例:
int[] arr = {1,2,3};
拼接结果:
"[1, 2, 3]"
15.10_常见对象(字符串反转并断点查看)(掌握)
A:案例演示
需求:把字符串反转
举例:键盘录入"abc"
反转结果:"cba"
15.11_常见对象(在大串中查找小串出现的次数思路图解)(掌握)
A:画图演示
需求:统计大串中小串出现的次数
举例: "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun”中java出现了5次
15.12_常见对象(在大串中查找小串出现的次数代码实现)(掌握)
A:案例演示 统计大串中小串出现的次数
15.13_常见对象(在大串中查找小串出现的次数代码优化并断点查看)(掌握)
A:代码优化
统计大串中小串出现的次数
String maxStr = "woaijavawozjavahendeaijavawohenajavaihenaijava";
String minStr = "java";
// 定义一个统计变量
int count = 0;
// 获取"java"在字符串中的索引
// int index = maxStr.indexOf(minStr);
// while (index != -1) {
// count++;
// // 找到"java" 截取
// maxStr = maxStr.substring(index + minStr.length());
// // 再获取索引
// index = maxStr.indexOf(minStr);
//
// }
int index;
while ((index = maxStr.indexOf(minStr)) != -1) {
count++;
// 找到"java" 截取
maxStr = maxStr.substring(index + minStr.length());
}
System.out.println(count);