1.String上的操作:
public static void main(String[] args) {
test01();//length() String中字符的个数。
test02();//charAt() 取的String中该索引位置上的字符。
test03();//equals() 将此字符串与指定的对象比较,区分大小写。 equalsIgnoreCase()不区分大小写。
test04();/*compareTo(Object o) 把这个字符串和另一个对象比较。
compareTo(String anotherString) 按字典顺序比较两个字符串。
比较对应字符的大小(ASCII码顺序),如果参数字符串等于此字符串,则返回值 0;
如果此字符串小于字符串参数,则返回一个小于 0 的值;
如果此字符串大于字符串参数,则返回一个大于 0 的值。*/
test05();//contains() 判断字符串中是否包含指定的字符串或字符。如果包含指定的字符串或字符返回true,否则返回false。
test06();//contentEquals 将此字符串与指定的 StringBuffer 比较。如字符串与指定 StringBuffer 表示相同的字符序列,则返回 true;否则返回 false。
test07();//indexOf() 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回-1。
test08();//substring() 返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾。
test09();//concat() 将指定的字符串参数连接到字符串上。
test10();//replace() 返回替换字符后的新String对象。如果没有替换,则返回原始的String对象。
test11();//toLowerCase() 将此String中的所有字符都转换为小写。toUpperCase() 将此String中的所有字符都转换为大写。
test12();//trim() 删除字符串的头尾空白符。
test13();//intern() 返回字符串对象的规范化表示形式。
test14();//isEmpty() 判断字符串是否为空。
}
public static void test01() {
String str = "abc";
int len = str.length();
System.out.println(len);
}
public static void test02() {
String str = "abc";
char result = str.charAt(0);
System.out.println(result);
}
public static void test03() {
String str = "abc";
String str1 = str;
String str2 = "ABC";
System.out.println(str.equals(str1));
System.out.println(str.equals(str2));
System.out.println(str.equalsIgnoreCase(str2));
}
public static void test04() {
String str = "abc";
String str1 = "abc123";
String str2 = str;
System.out.println(str.compareTo(str1));
System.out.println(str.compareTo(str2));
System.out.println(str1.compareTo(str));
}
public static void test05() {
String str = "abc";
System.out.println(str.contains("a"));
System.out.println(str.contains("1"));
}
public static void test06() {
String str = "abc";
String str1 = "abc1";
StringBuffer str2 = new StringBuffer("abc");
System.out.println(str.contentEquals(str2));
System.out.println(str1.contentEquals(str2));
}
public static void test07() {
String str = "abc";
System.out.println(str.indexOf("d"));
}
public static void test08() {
String str = "hello world";
System.out.println(str.substring(6));
}
public static void test09() {
String str = "你好世界:";
str = str.concat("hello world");
System.out.println(str);
}
public static void test10() {
String str = "abc";
System.out.println(str.replace('a', 'A'));
}
public static void test11() {
String str = "HELLO";
String str1 = "world";
System.out.println(str.toLowerCase());
System.out.println(str1.toUpperCase());
}
public static void test12() {
String str = " hello ";
System.out.println(str.trim());
}
public static void test13() {
String str = "hello";
System.out.println(str.intern());
}
public static void test14() {
String str = "abc";
String str1 = "";
String str2 = " ";
System.out.println(str.isEmpty());
System.out.println(str1.isEmpty());
System.out.println(str2.isEmpty());
}
2.格式化输出:printf():%:表示格式说明的起始符号,不可缺少。 -:有-表示左对齐输出。0:有0表示指定空位填0。%d:按整型数据的实际长度输出。%f:用来输出浮点数。Formatter类:java.util.Formatter用来处理所有新的格式化功能。
public static void main(String[] args) {
Formatter formatter = new Formatter();
formatter.format("%d", 123);
String str = formatter.toString();
System.out.println(str);
}
1万+

被折叠的 条评论
为什么被折叠?



