目录
5、掌握StringBuffer以及StringBuilder的区别
目标:
1、掌握String的基本概念
2、掌握String的的基本用法
3、掌握StringBuffer类
4、掌握StringBuilder类
5、掌握StringBuffer以及StringBuilder的区别
---1、String的基本概念
’string类位于java.lang包中,具有丰富的方法,是最常用的类之一。
使用string储存字符串:
String s = "Hello World";
String s = new String();
String s = new String("Hello World");
---2、String的的基本用法
1、注册新用户,要求密码长度不能小于6位
分析: String类提供了length()方法,确定字符串的长度
public class Register {
public static void main(String[ ] args) {
Scanner input = new Scanner(System.in);
String uname,pwd;
System.out.print("请输入用户名: ");
uname=input.next();
System.out.print("请输入密码: ");
pwd=input.next();
if( pwd.length()>=6 ){//判断代码长度
System.out.print("注册成功! ");
}else{
System.out.print("密码长度不能小于6位!");
}
}
}
2、注册成功后,实现登录验证。用户名为“TOM”,密码为“1234567”
public class Login {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String uname,pwd;
System.out.print("请输入用户名: ");
uname=input.next();
System.out.print("请输入密码: ");
pwd=input.next();
if( uname.equals("TOM") && pwd.equals("1234567") ){//比较用户名和密码是否正确
System.out.print("登录成功! ");
}else{
System.out.print("用户名或密码不匹配,登录失败!");
}
}
}
分析: String类提供了equals( )方法,比较存储在两个字符串对象的内容是否一致
equals()方法比较原理
equals():检查组成字符串内容的字符是否完全一致
“==”方法比较原理
==:判断两个字符串在内存中的首地址,即判断是否是同一个字符串对象
// 判断是否是 ""
System.out.println(s1.isEmpty());
// 判断是否包含 contains
System.out.println(s1.contains("ca"));
// 截取
System.out.println(s1.substring(2));//ca.mp4
System.out.println(s1.substring(2, 4));//ca
// 切割 split
System.out.println(Arrays.toString(s2.split(",")));
// 大写 小写
s1.toUpperCase();
s1.toLowerCase();
// 去除左右的空格
// s1.trim();
// 替换 replace
System.out.println(s1.replace("a", "f"));//fbcf.mp4
// 长度 length
if (s1.length() == 0) { System.out.println("请输入密码"); } else if (s1.length() <
6 || s1.length() > 16) { System.out.println("不好意思 密码必须在6-16之间"); }
// lastIndexOf 查找出现的位置
System.out.println(s1.lastIndexOf("a"));//3
// indexOf 查找出现的位置
System.out.println(s1.indexOf("a"));//0
System.out.println(s1.indexOf("a",2));//3
System.out.println(s1.indexOf("x"));//-1
// 是否是视频格式 endsWith
System.out.println(s1.endsWith(".mp4"));
// 取字符串的第一个
System.out.println(s1.charAt(0));//'a'
// equals 比较是否相等
// 判断谁大
compareTo
compareToIgnoreCase 忽略大小写
int result=s1.compareTo(s2); if(result>0) { System.out.println("s1"); }else
if(result==0){ System.out.println("一样大"); }else { System.out.println("s2"); }
// 判断该字符中存在多少个f
String temp = "weursfasdfhasfjweorigdshg" + "dafhsafjsafjafoewruwrqruqw9" +
"rhfidsfhasfdcjzvbvsafnakwef" + "afkhasfhafeiawruwahfasfhads"
+"fasfasfasfasfwerwqarhfafafa";
System.out.println(temp.split("f").length-1);
//替换f为""
String newTemp = temp.replace("f", "");
System.out.println(temp.length()-newTemp.length());
int count = 0;
int idx = temp.indexOf("f");
while (idx != -1) {
count++;
// 从刚刚那个位置上+1然后继续找
idx = temp.indexOf("f", idx + 1);
}
System.out.println(count);
// 为了解决String常见的问题:
// String的值无法改变,拼接效率问题
// StringBuffer: 同步,线程安全,速度慢
// StringBuilder: 不同步,线程不安全,速度快
// 在这两个类中都有一个方法叫做append追加
StringBuffer buffer=new StringBuffer();
buffer.append("abc");
//判断一个字符串是否是回文
//回文字符串是一个正读和反读都一样的字符串
String str="上海自来水来自海上";
for (int i = 0; i < str.length()/2; i++) {
if(str.charAt(i)!=str.charAt(str.length()-1-i)) {
System.out.println("不是回文");
}
}
StringBuffer b1=new StringBuffer("上海自来水来自海上");
//反转 reverse
StringBuffer b2 = b1.reverse();
System.out.println(b1.equals(b2));
**/
希望能够给各位博友带来一点帮助,其中如有错误还请各位指点,谢谢!