A:常见构造方法
public String():空构造
public String(byte[] bytes):把字节数组转成字符串
public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串(index:表示的是从第几个索引开始, length表示的是长度)
public String(char[] value):把字符数组转成字符串
public String(char[] value,int index,int count):把字符数组的一部分转成字符串
public String(String original):把字符串常量值转成字符串
B:案例演示
演示String类的常见构造方法
public class MyTest {
public static void main(String[] args) {
String string = new String();
System.out.println(string);
String string1 = new String("dytct");
System.out.println(string1);
byte[] bytes = {100, 15, 55, 66, 103};
String s = new String(bytes);
System.out.println(s);
String s1 = new String(bytes, 0, 3);
System.out.println(s1);
char[] chars={'a','c','d','k','u','好','好','学','习'};
String s2 = new String(chars);
System.out.println(s2);
String s3 = new String(chars,5,4);
System.out.println(s3);
}
}
演示前先说一个字符串的方法:
public int length():返回此字符串的长度。
public class MyTest {
public static void main(String[] args) {
String s = new String("vsauycg");
int length=s.length();
System.out.println(length);
}
}
C:String的特点: 一旦被创建就不能改变 因为字符串的值是在方法区的常量池中划分空间 分配地址值的
D:==和equals()的区别
==是一个比较运算符,可以比较基本类型,也可以比较引用类型。
==比较的是基本类型,比的是两个值是否相等。
==比较引用类型,比的是两个地址值是否相等。
equals()是Object类中的一个方法,只能比较引用类型,默认比较的是两个对象的地址值是否相同
很多类会重写 equals()方法,会按照重写过后的方式去比较。
常见对象(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 MyTest {
public static void main(String[] args) {
boolean g = "gahgyu".equalsIgnoreCase("GAHGYU");
System.out.println(g);
boolean s = "好好学习,天天向上".contains("天天向上");
System.out.println(s);
boolean d = "赵一一".startsWith("赵");
System.out.println(d);
boolean b = "赵一一".endsWith("一");
System.out.println(b);
String l="";
System.out.println(l.isEmpty());
if (l.length()==0){
System.out.println("空串");
}
}
}
常见对象(模拟用户登录)
A:案例演示: 需求:模拟登录,给三次机会,并提示还有几次。
public class MyTest {
public static void main(String[] args) {
String username="赵一";
String password="123456";
for (int i = 1; i <=3; i++) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的用户名");
String name=scanner.nextLine();
System.out.println("请输入你的密码");
String pwd=scanner.nextLine();
if (username.equals(name)&&password.equals(pwd)){
System.out.println("登录成功");
break;
}else {
if ((3-i)==0) {
System.out.println("你的次数已用完");
}else {
System.out.println("用户名或密码输入错误,请重新输入");
System.out.println("你还剩余"+(3-i)+"次");
}
}
}
}
}
常见对象(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) {
System.out.println("biabci".length());
String s="一行白鹭上青天";
char c = s.charAt(s.length()-1);
System.out.println(c);
int q = "我还是从前那个少年,没有一丝丝改变".indexOf("是");
System.out.println(q);
int indexOf = "我还是从前那个少年,没有一丝丝改变".indexOf("少年");
System.out.println(indexOf);
String p="我还是从前那个少年,没有一丝丝改变";
int l = p.indexOf("丝", p.indexOf("丝") + 1);
System.out.println(l);
int k = p.lastIndexOf("有");
System.out.println(k);
String substring = "我还是从前那个少年,没有一丝丝改变".substring(3);
System.out.println(substring);
String substring1 = "我还是从前那个少年,没有一丝丝改变".substring(5, 9);
System.out.println(substring1);
}
}
常见对象(字符串的遍历)
A:案例演示: 需求:遍历字符串
public class MyTest {
public static void main(String[] args) {
String s="我还是从前那个少年,没有一丝丝改变";
for (int i = 0; i < s.length(); i++) {
char c=s.charAt(i);
System.out.println(c);
}
System.out.println("========================");
for (int i=s.length()-1;i>=0;i--){
char c=s.charAt(i);
System.out.println(c);
}
}
}
常见对象(统计不同类型字符个数)(掌握)
A:案例演示: 需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
public class MyTest {
public static void main(String[] args) {
String str="asknuyeyhaooioSAGUYGAUC7854xtrwlkun";
int x=0;
int d=0;
int num=0;
for (int i = 0; i < str.length(); i++) {
char ch=str.charAt(i);
if (ch>='a'&&ch<='z'){
x++;
}else if(ch>='A'&&ch<='Z'){
d++;
}else {
num++;
}
}
System.out.println("小写字母"+x);
System.out.println("大写字母"+d);
System.out.println("数字"+num);
}
}
常见对象(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类的转换功能
1.public class MyTest {
public static void main(String[] args) {
byte[] bytes = "asldkj".getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
String s = new String(bytes);
System.out.println(s);
byte[] bytes1 = "你好".getBytes();
System.out.println(bytes.length);
String b="好好学习,天天向上";
char[] chars = b.toCharArray();
System.out.println(Arrays.toString(chars));
String s1 = new String(chars);
System.out.println(s1);
String s2 = new String(chars, 0, s1.indexOf('天') + 1);
System.out.println(s2);
}
}
2. public class MyTest {
public static void main(String[] args) {
int num=100;
String str=num+"";
boolean b=true;
String str2=b+"";
String s = String.valueOf(200);
System.out.println(s);
String s1 = String.valueOf(new char[]{'a', 'c', 'h', 'u'});
System.out.println(s1);
String s2 = "ascd".toUpperCase();
System.out.println(s2);
String s3 = "SASOIOJ".toLowerCase();
System.out.println(s3);
String s4 = "dihiuhd".concat("okaloi");
System.out.println(s4);
}
}
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) 跟上面一样 只是忽略大小写的比较
常见对象(把数组转成字符串)
A:案例演示
需求:把数组中的数据按照指定个格式拼接成一个字符串
举例:
int[] arr = {1,2,3};
拼接结果:
"[1, 2, 3]"
public class MyTest {
public static void main(String[] args) {
int[] arr={1,2,3};
String str="[";
for (int i = 0; i < arr.length; i++) {
if (i==arr.length-1){
str+=arr[i]+"]";
}else {
str+=arr[i]+",";
}
}
System.out.println(str);
}
}
常见对象(字符串反转并断点查看)
A:案例演示
需求:把字符串反转
举例:键盘录入"abc"
反转结果:"cba"
public class MyTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一段字符串");
String s=scanner.nextLine();
String str="";
for (int i = s.length()-1; i >=0; i--) {
str+=s.charAt(i);
}
System.out.println(str);
}
}
常见对象(在大串中查找小串出现的次数思路图解)
A:画图演示
需求:统计大串中小串出现的次数
举例: "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun”中java出现了5次
public class MyTest {
public static void main(String[] args) {
String maxStr="woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";
int index=maxStr.indexOf("java");
int count=0;
while (index!=-1){
count++;
maxStr=maxStr.substring(index+4);
index=maxStr.indexOf("java");
}
System.out.println(count);
}
}