String类中的一些方法
equals()、equalsIgnoreCase()、contains()、startsWith()、endsWith()、isEmpty()
package com.qianfeng.string_01;
/*
* String类中的判断功能
* public boolean equals(Object anObject):比较两个字符串的内容是否相同
* public boolean equalsIgnoreCase(String anotherString):比较两个字符串的值是相同,忽略大小写
* public boolean contains(String str):判断指定大字符串是否包含指定的子串
* public boolean startsWith(String prefix):判断字符串是否以指定的前缀开头
* public boolean endsWith(String suffix):判断字符串是否以指定的后缀结尾
* public boolean isEmpty() :判断字符串对象是否为空
*
*
* String s = "" ; //空字符串 ,有内容,不过内容空
* String s = null ;//空对象
* 空字符串和空对象是两个概念!
*
* */
public class StringDemo {
public static void main(String[] args) {
//定义三个字符串
String s1 = "HelloWorld" ;
String s2 = "helloworld" ;
String s3 = "helloworld" ;
//public boolean equals(Object anObject):比较两个字符串的内容是否相同
System.out.println("equals():"+s1.equals(s2));//false
System.out.println("equals():"+s1.equals(s3));//false
System.out.println("equals():"+s2.equals(s3));//true
System.out.println("-----------------------");
//public boolean equalsIgnoreCase(String anotherString):比较两个字符串的值是相同,忽略大小写
System.out.println("equals():"+s1.equalsIgnoreCase(s2));//true
System.out.println("equals():"+s1.equalsIgnoreCase(s3));//true
System.out.println("equals():"+s2.equalsIgnoreCase(s3));//true
System.out.println("-----------------------");
//public boolean contains(Char s):判断指定字符串是否包含指定的字符
System.out.println("contains():"+s1.contains("ll"));
System.out.println("contains():"+s1.contains("owo"));
System.out.println("contains():"+s1.contains("ak47"));
System.out.println("-----------------------");
//public boolean startsWith(String prefix)
System.out.println("startWith():"+s2.startsWith("hel"));
System.out.println("startWith():"+s2.startsWith("java"));
System.out.println("-----------------------");
// public boolean isEmpty() 如果为空就是true (值是空的)
System.out.println("isEmpety():"+s1.isEmpty());//false
s1 = null ;
//加入判断
if(s1!=null) {
System.out.println(s1.isEmpty());
}else {
System.out.println("当前字符串null了");
}
//java.lang.NullPointerException
//System.out.println(s1.isEmpty());
s2 = "" ;
System.out.println(s2.isEmpty());
}
}
题目:模拟输密码
模拟用户登录操作,给3次机会,并且提示还有几次机会。
package com.qianfeng.string_01;
import java.util.Scanner;
/*
* 练习
* 模拟用户登录操作,给3次机会,并且提示还有几次机会
* 用户名
* 密码
* 分析:
* 1)假设 存在一个用户名和一个密码
* String username = "admin" ;
* String password = "admin" ;
*
* 循环3次:x:0,1,2
* 2)键盘录入 两个数据String
* name
* pwd
*
* 3)如果username和录入 的name一致并且 password和pwd一致
* 登录成功
*
* 如果有一个不成功,就登录失败
* 给出提示:2-x ==0
* 提示:"请速联系管理员"
* 如果没有取到最大次数
* 提示:您还有2-x次机会
*
*
* 开发中:接口 UserDao(Data Accesss Object:数据访问对象)
* User类
* 字段:id,name,age,gender
* 后台数据库中User表
* id name age gender
* */
public class StringTest {
public static void main(String[] args) {
//1)定义已经存在的用户名和密码
String username = "admin" ;
String password = "admin" ;
//2)键盘录入用户名和密码 给3次机会
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(name.equals(username) && pwd.equals(password)) {
//登录成功
System.out.println("恭喜您,登录成功...");
break ;
}else {
//如果有一个不一致,登录失败
//提示还有几次机会
//x:2,1,0
//如果当前的x取值已经是0了,没有机会,请速联系管理员
if((2-x)==0) {
System.out.println("账户已被冻结,请速联系客户人员...");
}else {
//换一种提示
System.out.println("账户或者密码错误,您还有"+(2-x)+"次机会...");
}
}
}
}
}
length()、concat()、charAt()、indexOf()、substring()
package com.qianfeng.string_02;
/*
* String类的获取功能
* public int length () :返回此字符串的长度。 (重点)
public String concat (String str) :将指定的字符串连接到该字符串的末尾。
public char charAt (int index) :返回指定索引处的 char值。(重点)
public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。 (重点)
public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符
串结尾。(重点)
public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到
endIndex截取字符串。含beginIndex,不含endIndex。
* */
public class StringDemo {
public static void main(String[] args) {
//定义一个字符串
String str = "helloworld" ;
//public int length () :返回此字符串的长度。
System.out.println("length():"+str.length());
//拼接功能:contact public String concat (String str) :将指定的字符串连接到该字符串的末尾。
System.out.println("contact():"+str.concat("javaee"));
String s1 = "java" ;
System.out.println(str+s1); //拼接符号
System.out.println("-----------------------");
//public char charAt (int index) :返回指定索引处的 char值。(重点)
//String的原码对字符串实现---->将字符串内容--->字符数组
System.out.println("charAt():"+str.charAt(4)); //o
System.out.println("-----------------------");
//public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
System.out.println("indexOf():"+str.indexOf("l")); //2
//public String substring (int beginIndex) :从开始位置默认截取到结尾,返回截取后的字符串
System.out.println("substring():"+str.substring(3));//loworld
//public String substring (int beginIndex, int endIndex) :从指定位置开始截取到指定位置结束
//注意:不包含结束位置(但是包含前面这个位置)
System.out.println("substring():"+str.substring(5, 10));//worl
}
}
题目:判断一个字符串中的大小写字母与数字
package com.qianfeng.string_02;
import java.util.Scanner;
/*
* 给定义一个字符串,统计字符串中大写字母字符,小写字母字符,数字字符有多少个(不考虑其他字符)
*
* 举例:
* Hello123World
*
*分析:
* 0)定义三个变量进行统计
* 1)改进:使用键盘录入字符串
* 2)可以将字符串进行遍历
* 获取每一个字符 char ch = s.charAt(x) ;
*
* 3)如何判断是大写字母,小写字母字符,数字字符
* 'a' -- 97
* '0' -- 48
* 'A' -- 65
*
* 上面这种方式,字符对应ASCII码表的值推的,麻烦
*
* ch字符如果在'a'-'z' :小写字母字符 统计变量++
* ch在'A' -'Z'之间,大写字母字符
* ch在'0' -'9',数字字符
* 4)在将所有统计变量输出即可!
* */
public class StringTest2 {
public static void main(String[] args) {
//定义统计变量
int bigCount = 0 ;
int smallCount = 0 ;
int numberCount = 0;
//创建键盘录入对象
Scanner sc = new Scanner(System.in) ;
//提示并录入数据
System.out.println("请输入一个字符串数据:");
//nextLine()/next()
String s = sc.nextLine() ;
//遍历字符串
for(int x = 0 ; x < s.length() ; x++) {
//获取每一个字符
char ch = s.charAt(x) ;
/*
* ch字符如果在'a'-'z' :小写字母字符 统计变量++
* ch在'A' -'Z'之间,大写字母字符
* ch在'0' -'9',数字字符
*
* */
if(ch >='a' && ch <='z') {
//小写字母字符
smallCount ++ ;
}else if(ch >='A' && ch<='Z') {
bigCount++ ;
}else if(ch >='0' && ch<='9') {
numberCount ++;
}
}
System.out.println("大写字母字符共有:"+bigCount+"个");
System.out.println("小写字母字符共有:"+smallCount+"个");
System.out.println("数字字符共有:"+numberCount+"个");
}
}
toUpperCase、toLowerCase
package com.qianfeng.string_03;
/*
* String类的转换功能
* public char[] toCharArray()将此字符串转换为一个新的字符数组。 (重点:使用最多)
* public byte[] getBytes():将字符串转换成字节数组
* public String toUpperCase():将字符串本身转换大写字符串
* public String toLowerCase():将字符串本身转换小写
* public static String valueOf(任意类型数):可以将任意类型的数据转换字符串 (重点)
*
* */
public class StringDemo {
public static void main(String[] args) {
//定义一个字符串
String s = "helloJava" ;//---->字符数组
// public char[] toCharArray()将此字符串转换为一个新的字符数组。
char[] chs = s.toCharArray() ;
//遍历字符数组
for(int x = 0 ; x < chs.length ; x++) {
System.out.print(chs[x]+" ");
}
System.out.println("----------------------");
//public byte[] getBytes():将字符串转换成字节数组
byte[] bys = s.getBytes() ;
for(int x = 0 ; x < bys.length ;x ++) {
System.out.print(bys[x]+" ");
}
System.out.println("----------------------");
// public String toUpperCase():将字符串本身转换大写字符串
// public String toLowerCase():将字符串本身转换小写
System.out.println("toUpperCase():"+s.toUpperCase());
System.out.println("toLowerCase():"+s.toLowerCase());
//public static String valueOf(任意类型数):可以将任意类型的数据转换字符串
int i = 100 ;
//String s3 = "" ;
//s3 + i ;
String s2 = String.valueOf(i) ; //万能方法
System.out.println(s2);//"100"
}
}
compareTo()、trim()、replace()
package com.qianfeng.string_04;
/*
* String其他功能:
*
* public String replace(char oldChar,char newChar):替换功能
* public String trim():去除两端空格
*
* /*
* 看原码
* public int compareTo(String anotherString)按字典顺序比较两个字符串
* s1="hello"
* s2="hel"
* 结果是:2
*
* */
public class StringDemo {
public static void main(String[] args) {
String s = "helloworld" ;
//public String replace(char oldChar,char newChar):替换功能
String s2 = s.replace('l', 'k') ;
System.out.println("s2:"+s2);
System.out.println("----------");
// public String trim():去除两端空格
String s3 = " hello world " ;
System.out.println("s3:"+s3+"---");
String s4 = s3.trim() ;
System.out.println("s4:"+s4+"---");
System.out.println("----------");
//public int compareTo(String anotherString)按字典顺序比较两个字符串
String s5 = "hello" ;
String s6 = "hello" ;
String s7 = "abc" ;
String s8 = "hel" ;
System.out.println(s5.compareTo(s6)); //0
System.out.println(s5.compareTo(s7)); //7
System.out.println(s5.compareTo(s8)); //2
}
}
StringBuffer
package com.qianfeng.stringbuffer_01;
/*
* StringBuffer:
* 线程安全的可变字符序列.
*线程安全?(多线程中讲)
* 意味着 同步
* 举例:银行的网站,政府部门,医院网站
* 同步性---->执行效率低
*
* 线程不安全:
* 举例:论坛网站
* 不同步---->执行效率高
*
* StingBuilder:一个可变的字符序列。此类提供一个与 StringBuffer 兼容的 API,但不保证同步(线程不安全,效率高)
* 注意:单线程程序中,StringBuilder通常去替代StringBuffer
*
* 面试题:三者之间的区别
* StringBuffer和String,StringBuilder ?
* 前者:字符串缓冲区(存储的都是字符串),可变的字符序列,线程安全的!
* 后者:字符串是常量,一旦被赋值,其值不能被改变!
* StingBuilder:单线程程序中,StringBuilder通常去替代StringBuffer,线程不安全!
*
* 构造方法:
* public StringBuffer() :空参构造,字符串缓冲区没有字符内容
public StringBuffer(int capacity):构造缓冲区对象,设置初始了初始容量
public StringBuffer(String str):构造缓冲区对象,存储一个str字符串
两个获取功能:
public int length():获取缓冲中字符长度
public int capacity()返回当前容量
*
* */
public class StringBufferDemo {
public static void main(String[] args) {
//public StringBuffer() :空参构造,字符串缓冲区没有字符内容
StringBuffer sb = new StringBuffer() ;
System.out.println(sb.length());//0
System.out.println(sb.capacity());//16:初始容量
System.out.println("--------------------------");
// public StringBuffer(int capacity):构造缓冲区对象,设置初始了初始容量
StringBuffer sb2 = new StringBuffer(50) ;
System.out.println(sb2.length());
System.out.println(sb2.capacity());//设置容量为50
System.out.println("--------------------------");
//public StringBuffer(String str):构造缓冲区对象,存储一个str字符串
StringBuffer sb3 = new StringBuffer("hello") ;
System.out.println(sb3.length());
System.out.println(sb3.capacity());//16+5=21
//StringBuffer sb4 ="hello" ; //String
//String s = "hello" ;
//StringBuffer sb4 = s ;
}
}
append()、insert()
package com.qianfeng.stringbuffer_01;
/*
* 添加相关的功能:
* public StringBuffer append(String str):在缓冲区中不断追加字符串内容,返回值是它本身(重点功能:使用居多)
public StringBuffer insert(int offset,String str):在指定的位置上插入指定的字符串
*
* */
public class StringBufferDemo2 {
public static void main(String[] args) {
//创建一个字符串缓冲区对象
StringBuffer sb = new StringBuffer() ;
System.out.println("sb:"+sb);
//public StringBuffer append(String str)
//StringBuffer sb2 = sb.append("hello") ;
//StringBuffer sb3 = sb2.append("world") ;
//System.out.println("sb3:"+sb3);//helloworld :StringBuffer类型
/*
* sb.append(true); sb.append(100) ; sb.append('a') ; sb.append("javaee") ;
* sb.append(3.1415926) ;
*/
//链式编程
//sb.append(true).append('a').append("hello").append(3.14).append(10);
sb.append("hello");
sb.append("world");
sb.append("java");
System.out.println("sb:"+sb);
System.out.println("-----------------------");
//public StringBuffer insert(int offset,String str):在指定的位置上插入指定的字符串
System.out.println("insert():"+sb.insert(5, "Andorid")) ;
}
}
deleteCharAt(int index)、delete(int start,int end)
package com.qianfeng.stringbuffer_01;
/*
* StringBuffer的删除功能:
* public StringBuffer deleteCharAt(int index):删除指定索引处的字符,返回字符串缓冲区本身(使用居多)
public StringBuffer delete(int start,int end):从指定位置开始到指定位置结束,删除内容,返回值字符串缓冲区本身
* */
public class StringBufferDemo3 {
public static void main(String[] args) {
//创建字符串缓冲区对象
StringBuffer sb = new StringBuffer() ;
//追加内容
sb.append("hello").append("Java") ;
//public StringBuffer deleteCharAt(int index)
//需求:删除字符串缓冲区的内容: 'e'
//System.out.println("deleteCharAt():"+sb.deleteCharAt(1));
//需求:删除缓冲区的第一个'l'字符
//System.out.println("deleteCharAt():"+sb.deleteCharAt(1));
//public StringBuffer delete(int start,int end)
System.out.println("delete():"+sb.delete(0,5));//Java
}
}