重新开始第5天
String
String s1 = "hello world";
byte[] bytes = {97,98,99};
String s2 = new String(bytes);//将bytes数组转换成字符串
String s3 = new String(bytes,1,2);
//字节数组,数组元素下标的起始位置,长度
System.out.println(s3);
方法
public class ReplayTest {
public static void main(String[] args) {
//String类中常用方法
char c = "中国人".charAt(1);
System.out.println(c);//国
//int compareTo(String anotherString) 比较
int result = "abc".compareTo("abc");
System.out.println(result);//0(等于0)前后一致
int result2 = "abcd".compareTo("abce");
System.out.println(result2);//-1(小于0)前小后大
int result3 = "abce".compareTo("abcd");
System.out.println(result3);//1(大于0)前大后小
System.out.println("xyz".compareTo("yxz"));//-1
//boolean contains(CharSequence s) //判断前面的字符串中是否包含后面的子字符串
System.out.println("HelloWorld.java".contains(".java"));//true
//包含为true不包含为false
//boolean endsWith(String suffix)
//判断当前字符串是否以某个字符串结尾
System.out.println("tese.txt".endsWith(".java"));//false
System.out.println("tese.txt".endsWith(".txt"));//true
//boolean equals(Object anObject)
//比较两个字符串是否相等
System.out.println("abe".equals("abc"));//true
//boolean equalsIgnoreCase(String anotherString)
//判断两个字符串是否相等,并且忽略大小写
System.out.println("AbC".equalsIgnoreCase("abc"));//true
//byte[] getBytes()
//将字符串对象转换成字节数组
byte[] bytes = "abc".getBytes();
for (int i = 0; i < bytes.length ; i++) {
System.out.println(bytes[i]);//97,98,99
}
//int indexOf(String str)
//判断某个字符串在当前字符串中第一次出现的索引(下标)
System.out.println("abcdsafdsafd".indexOf("d"));//3
//boolean isEmpty()
//判断某个字符串是否为空
String s = "a";
System.out.println(s.isEmpty());//false
//int length()
//字符串长度
//判断数组长度是length属性,判断段字符串长度是length()方法
System.out.println("accd".length());//4
//int lastIndexOf(String str)
//判断某个字符串在当前字符串中最后一次出现的索引(下标)
System.out.println("fjdafldbnaognoeiw".lastIndexOf("bn"));//7
//String replace(CharSequence target, CharSequence replacement)
