11.18 Daily

The properties of Java strings get the properties of the length of the string: length() usage: string.length() experiment: public class day15_09 { public static void main(String[]args){ String a=“hello”; /write one String, named a, value is hello System.out.println(a.length()); /output string length a.length() } } 123456 Result: Determine whether the string is the same attribute: equals() usage :String a=“content”; a.equals(“content”); return true experiment: public class day15_09 { public static void main(String[]args){ String a=“bbb”; / write a string and name it Is a, the value is bbb System.out.println(a.equals("bbb")); / compare the value of a and equals("bbb") to see if the value in equals ("bbb") is the same, the same returns the boolean value true and outputs System. out.println(a.equals("ccc")); / compare the value of a and equals("ccc") to see if the value in equals ("ccc") is the same, if it is not the same, return the boolean value false and output } } 1234567 result: compare equal, not Case-sensitive attributes: equalsIgnoreCase() Usage: String a=“AbCd”; a.equals(“abcd”); return true Experiment: public class day15_09 {













public static void main(String[]args){ String a="AbCd"; /Write a string, named a, and the value is AbCd

     /比较a的值和equals("bbb")里的值是不是相同,相同返回布尔值true并输出
System.out.println("验证码:"+a.equalsIgnoreCase("abcd"));

}
1
2
3
}
The connection of 12345678 strings will generate new strings. The first type: concat attribute usage: "hello".concat("java"); Generate hellojava experiment: public class day15_09 { public static void main(String []args){ String a="hello"; /Write a string, named a, the value is hello String b="java"; /Write a string, named a, the value is java System.out.println (a.concat(b)); /Use the concat attribute to connect the value of a and b to generate hellojava and output System.out.println("hello".concat("java")); /Use the concat attribute to make hello and java are connected to generate hellojava and output } } 12345678 Result: the second type: + sign is more common usage: "string a" + "string b" experiment: public class day15_09 { public static void main(String[]args ){ System.out.println("hello"+"java"); /Use the + sign to connect hello and java, generate hellojava and output it }}











1234 Result: In the obtained string, the index position of the content is found from the left: indexOf attribute Usage: String con="hello"; con.indexOf("l"); Return 2 experiment: public class day15_09 { public static void main( String[]args){ String con="hellojava"; /Write a string, named con, the value is hellojava System.out.println(con.indexOf("l")); /Find characters in the value of con String l, count from left to right, subscript at the second digit, output 2 System.out.println(con.indexOf("p")); /Find the string p in the value of con, and count from left to right , P is not found, output -1



/得出结论:索引(位置)是从0开始计算,找不到会返回-1

}
1
2
}
123456789 Results: Find from the end: lastIndexOf attribute Usage: String con="hello"; con.lastIndexOf("l"); Return 3 experiments: public class day15_09 { public static void main(String[]args) { String con="hellojava"; /Write a string, named con, the value is hellojava System.out.println(con.lastIndexOf("l")); /Find the string l from the end in the value of con , Subscript at the 3rd position, output 3


/得出结论:用lastIndexOf,从末尾开始找,返回的是从左往右数的下标

}
1
2
}
The interception attribute of 12345678 string: substring() Usage: String con="helloJava"; con.substring(3); Return to lojava test: public class day15_09 { public static void main(String[]args){ String con="hellojava"; /Write a string, named con, and the value is hellojava System.out.println(con.substring(3)); /Use the substring attribute to intercept the string con, starting from the third digit, and Keep the 3rd position, output lojava System.out.println(con.substring(3,6)); / Use the substring attribute to intercept the string con, start from the 3rd position, and intercept to the 6th position, keep the 3rd position but Do not keep the sixth position, output loj } } 1234567 Result: the method of removing spaces from the string: trim attribute usage: string. trim() experiment: public class day15_09 { public static void main(String[]args){ /write one String, named con, value is hellojava; String con=" hello";









System.out.println(con);              /输出con
System.out.println(con.trim());       /用trim属性去掉con值里的空格,再输出

}

Guess you like

Origin blog.csdn.net/zzxin1216/article/details/109800004