1. ⭐️String ⭐️
📚概述:
String类位于java.lang包下,是Java语言的核心类,提供了字符串的比较、查找、截取、大小写转换等操作;Java语言为“+”连接符(字符串连接符)以及对象转换为字符串提供了特殊的支持,字符串对象可以使用“+”连接其他对象。
📚遍历的方法
String有两种遍历方式:
第一种charAt()方法;
第二种是先转化为字符数组,再挨个遍历.
经常使用的方法:
charAt(int i);//返回索引i处的字符
length();//返回此字符串的长度
isEmpty();//判空 当length()为0时返回true
📒第一种charAt()方法;
public class String对象的遍历 {
public static void main(String[] args) {
String s = "123456";
for(int i = 0; i < s.length(); i++)
System.out.println(s.charAt(i)+" ");// 1 2 3 4 5 6
}
}
📒 第二种是先转化为字符数组,再挨个遍历.
public class String对象的遍历 {
public static void main(String[] args) {
String s1 = "123456";
char[] s2=s1.toCharArray();//注意此处
for(int i=0;i<s2.length;i++){
System.out.print(s2[i]+" ");// 1 2 3 4 5 6
}
}
}
📚比较的方法
compareTo(String anotherString)//按字典顺序比较两个字符串
compareToIgnoreCase(String anotherString)//按字典顺序且不区分大小写比较两个字符串
equals(String anotherString)//判断两个字符串是否相等,相等返回true否则返回false
equalsIgnoreCase(String str)//同上,不区分大小写。
//判断字符串内容是否相同要用 equals 方法而不能直接用 == 。谨记
String s = "abcde";
String s1 = "Abcde";
int f = s.compareTo(s1);
int f1 = s1.compareToIgnoreCase(s);
Boolean f2 = s.equals(s1);
Boolean f3 = s.equalsIgnoreCase(s1);