public class StringTest1 { public static void main(String[] args) { String str = new String("My name is "); String name = " Wit"; str += "Edgar"; str = str + name; System.out.println(str); System.out.println(str.length()); System.out.println(str.equals("my name is edgar" + " wit")); System.out.println(str.equalsIgnoreCase("my name " + "is edgar wit")); System.out.println(str.toUpperCase()); System.out.println(str.toLowerCase()); System.out.println(str.toString()); } } |
输出结果:
My name is Edgar Wit
20
false
true
MY NAME IS EDGAR WIT
my name is edgar wit
My name is Edgar Wit
JDK文档:
int | length() |
toLowerCase() | |
toLowerCase(Locale locale) | |
toString() | |
toUpperCase() | |
toUpperCase(Locale locale) | |
boolean | |
boolean | equalsIgnoreCase(String anotherString) |
boolean |
codePointCount(),charAt(),codePointAt(),codePointBefore()方法
public class StringTest2 { public static void main(String[] args) { String str = "Edgar"; int m = str.length(); int n = str.codePointCount(0, str.length()); System.out.println(m + " " + n);
char ch1 = str.charAt(2); char ch2 = str.charAt(4); System.out.println(ch1 + " " + ch2);
int i = str.codePointAt(3); System.out.println(i);
int j = str.codePointBefore(4); System.out.println(j); } } |
输出结果:
5 5
g r
97
97
JDK文档:
char | charAt(int index) |
int | codePointAt(int index) |
int | codePointBefore(int index) |
int | codePointCount(int beginIndex, int endIndex) |
compareTo(),compareToIgnoreCace()方法
public class StringTest3 { public static void main(String[] args) { String str1 = "Edgar"; String str2 = "Edgar"; String str3 = "edgar";
System.out.println(str1.compareTo(str2)); System.out.println(str1.compareTo(str3));
System.out.println(str1.compareToIgnoreCase(str3)); } } |
输出结果:
0
-32
0
JDK文档:
int | |
int | compareToIgnoreCase(String str) |
concat(),contains(),copyValeOf()方法
public class StringTest4 { public static void main(String[] args) { String str1 = "abcdefg"; System.out.println(str1);
str1 = str1.concat("abcd"); System.out.println(str1);
boolean bool1 = str1.contains("abcd"); boolean bool2 = str1.contains("hijk"); System.out.println(bool1 + " " + bool2);
char[] ch = {'a','b','c'}; System.out.println(str1.copyValueOf(ch)); } } |
输出结果:
abcdefg
abcdefgabcd
true false
abc
JDK文档:
boolean | contains(CharSequence s) |
static String | copyValueOf(char[] data) |
static String | copyValueOf(char[] data, int offset, int count) |
public class StringTest5 { public static void main(String[] args) { String str1 = "abcabcdeabcdefg";
System.out.println(str1.indexOf('a')); System.out.println(str1.indexOf('a', 2));
System.out.println(str1.indexOf("bc")); System.out.println(str1.indexOf("bc", 6));
System.out.println(str1.lastIndexOf('a')); System.out.println(str1.lastIndexOf('a', 6));
System.out.println(str1.lastIndexOf("bc")); System.out.println(str1.lastIndexOf("bc", 4)); } } |
int | indexOf(int ch) |
int | indexOf(int ch, int fromIndex) |
int | |
int | indexOf(String str, int fromIndex) |
int | lastIndexOf(int ch) |
int | lastIndexOf(int ch, int fromIndex) |
int | lastIndexOf(String str) |
int | lastIndexOf(String str, int fromIndex) |
public class StringTest6 { public static void main(String[] args) { String str1 = "Edgar is a student"; str1 = str1.replace('a', 'A');
System.out.println(str1); System.out.println(str1.startsWith("E")); System.out.println(str1.startsWith("E", 1)); System.out.println(str1.endsWith("ent"));
String str2 = str1.substring(3); String str3 = str1.substring(5, 11); System.out.println(str2); System.out.println(str3);
String str4= " Edgar "; System.out.println(str4); str4 = str4.trim(); System.out.println(str4); } } |
输出结果:
EdgAr is A student
true
false
true
Ar is A student
is A
Edgar
Edgar
JDK文档:
boolean | |
boolean | startsWith(String prefix) |
boolean | startsWith(String prefix, int toffset) |
substring(int beginIndex) | |
substring(int beginIndex, int endIndex) | |
trim() |