char | charAt(int index) 返回指定索引处的 char 值。 |
例:
public class Test {
public static void main(String[] args) {
String s = "asdasdadasd";
for (int i = 0; i < s.length(); i++) {
System.out.print(s.charAt(i) + " ");
}
}
}
输出结果:a s d a s d a d a s d
int | compareTo(String anotherString) 按字典顺序比较两个字符串。 |
public int compareTo(String anotherString)
-
按字典顺序比较两个字符串。该比较基于字符串中各个字符的 Unicode 值。按字典顺序将此
String
对象表示的字符序列与参数字符串所表示的字符序列进行比较。如果按字典顺序此String
对象位于参数字符串之前,则比较结果为一个负整数。如果按字典顺序此String
对象位于参数字符串之后,则比较结果为一个正整数。如果这两个字符串相等,则结果为 0;compareTo
只在方法equals(Object)
返回true
时才返回0
。
例:public class Test {
public static void main(String[] args) {
String s = "a", ss = "A;
int a = s.compareTo(ss);
System.out.println(a);
}
}
输出结果:32
int | compareToIgnoreCase(String str) 按字典顺序比较两个字符串,不考虑大小写。 |
例: public class Test {
public static void main(String[] args) {
String s = "A", ss = "a";
System.out.println(s.compareToIgnoreCase(ss));
}
}
输出结果:0
String | concat(String str) 将指定字符串连接到此字符串的结尾。 |
例:public class Test {
public static void main(String[] args) {
String s = "abc", ss = "cde";
System.out.println(s.concat(ss));
}
}
输出结果:abccde
boolean | contains(CharSequence s) 当且仅当此字符串包含指定的 char 值序列时,返回 true。 |
例:public class Test {
public static void main(String[] args) {
String s = "abc";
String ss = "ab";
System.out.println(s.contains(ss));
}
}
输出结果:true
boolean | contentEquals(CharSequence cs) 将此字符串与指定的 CharSequence 比较。 |
将此字符串与指定的 CharSequence
比较。当且仅当此 String
与指定序列表示相同的 char 值序列时,结果才为 true
。
例:public class Test {
public static void main(String[] args) {
String s = "abc";
String s1 = "ab";
String s2="abc";
System.out.println(s.contentEquals(s1));
System.out.println(s.contentEquals(s2));
}
}
输出结果:false
true
boolean | equalsIgnoreCase(String anotherString) 将此 String 与另一个 String 比较,不考虑大小写。 |
public boolean equalsIgnoreCase(String anotherString)
-
将此
String
与另一个String
比较,不考虑大小写。如果两个字符串的长度相同,并且其中的相应字符都相等(忽略大小写),则认为这两个字符串是相等的。
例:public class Test {
public static void main(String[] args) {
String s = "abc";
String s1 = "ABC";
String s2="abc";
System.out.println(s.equalsIgnoreCase(s1));
System.out.println(s.equalsIgnoreCase(s2));
}
}
输出结果:true
true
static String | copyValueOf(char[] data, int offset, int count) 返回指定数组中表示该字符序列的 String。 |
public static String copyValueOf(char[] data, int offset, int count)
-
返回指定数组中表示该字符序列的 String。
-
-
-
参数:
-
data
- 字符数组。 -
offset
- 子数组的初始偏移量。 -
count
- 子数组的长度。
返回:
-
一个
String
,它包含字符数组的指定子数组的字符
-
例:public class Test {
public static void main(String[] args) {
char[] a = { 'a', 'b', 'c', 'd' };
String s = String.copyValueOf(a, 1, 2);
System.out.println(s);
}
}
输出结果:bc
static String | copyValueOf(char[] data) 返回指定数组中表示该字符序列的 String。 |
copyValueOf
public static String copyValueOf(char[] data)
-
返回指定数组中表示该字符序列的 String。
-
-
-
参数:
-
data
- 字符数组。
返回:
-
一个
String
,它包含字符数组的字符。
-
例:public class Test {
public static void main(String[] args) {
char[] a = { 'a', 'b', 'c', 'd' };
String s = String.copyValueOf(a);
System.out.println(s);
}
}
输出结果:abcd
| |
boolean | equals(Object anObject) 将此字符串与指定的对象比较。 |
boolean | equalsIgnoreCase(String anotherString) 将此 String 与另一个 String 比较,不考虑大小写。 |
int
hashCode()
返回此字符串的哈希码。 int
indexOf(int ch)
返回指定字符在此字符串中第一次出现处的索引。 int
indexOf(int ch, int fromIndex)
返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。 int
indexOf(String str)
返回指定子字符串在此字符串中第一次出现处的索引。 int
indexOf(String str, int fromIndex)
返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
String[] | split(String regex) 根据给定正则表达式的匹配拆分此字符串。 |
例:public class Test {
public static void main(String[] args) throws Exception {
String s = "a,b,c,d,e";
String[] s1 = s.split(",");
for (int i = 0; i < s1.length; i++) {
System.out.print(s1[i] + " ");
}
}
}
输出结果:a b c d e
String | substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串。 |
String
substring(int beginIndex, int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。
substring
public String substring(int beginIndex)
- 返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾。
例:public class Test {
public static void main(String[] args) throws Exception {
String s = "abcdefghijk";
String s1 = s.substring(5);
System.out.println(s1);
}
}
输出结果:fghijk
String
substring(int beginIndex, int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。
String
substring(int beginIndex, int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。
char[] | toCharArray() 将此字符串转换为一个新的字符数组 |
例:public class Test {
public static void main(String[] args) throws Exception {
String s = "abcdefghijk";
char[] a=s.toCharArray();
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}
}
输出结果:a b c d e f g h i j k
String | toLowerCase() 使用默认语言环境的规则将此 String 中的所有字符都转换为小写。 |
String | toUpperCase() 使用默认语言环境的规则将此 String 中的所有字符都转换为大写。 |
static String | valueOf(boolean b) 返回 boolean 参数的字符串表示形式。 |
例:public class Test {
public static void main(String[] args) throws Exception {
boolean boo = true;
System.out.println(String.valueOf(boo));
}
}
输出结果:true
static String | valueOf(char c) 返回 char 参数的字符串表示形式。 |
static String | valueOf(char[] data) 返回 char 数组参数的字符串表示形式。 |
static String | valueOf(char[] data, int offset, int count) 返回 char 数组参数的特定子数组的字符串表示形式。 |
static String | valueOf(double d) 返回 double 参数的字符串表示形式。 |
static String | valueOf(float f) 返回 float 参数的字符串表示形式。 |
static String | valueOf(int i) 返回 int 参数的字符串表示形式。 |
static String | valueOf(long l) 返回 long 参数的字符串表示形式。 |
static String | valueOf(Object obj) 返回 Object 参数的字符串表示形式。 |