1 concat
作用是将指定字符串连接到此字符串的结尾。
示例代码如下:
public class Concat {
public static void main(String[] args) {
String str1=("Hello");
String str2=("World");
String str=str1.concat(str2);
System.out.println(str);
}
}
2 split
作用是根据给定正则表达式的匹配拆分此字符串。
该方法的作用就像是使用给定的表达式和限制参数 0 来调用两参数 split 方法。因此,所得数组中不包括结尾空字符串。
示例代码如下:
public class Split {
public static void main(String[] args) {
String str="a:b:c:d:e";
String[] strArr=str.split(":");
for(int i=0;i<strArr.length;i++){
System.out.print(strArr[i]+"\t");
}
}
}
3 toLowerCase
作用是使用给定 Locale
的规则将String
中的所有字符都转换为小写。
示例代码如下:
public class ToLowerCase {
public static void main(String[] args) {
String s="ABCD";
String s1=s.toLowerCase();
System.out.println(s1);
}
}
4 toCharArray
作用是将此字符串转换为一个新的字符数组。
一个新分配的字符数组,它的长度是此字符串的长度,它的内容被初始化为包含此字符串表示的字符序列。
示例代码如下:
public class ToCharArray {
public static void main(String[] args) {
String s="abcd";
char[] c=s.toCharArray();
for(int i=0;i<s.length();i++){
System.out.println(c[i]);
}
}
}
5 equals
将此字符串与指定的对象比较。当且仅当该参数不为 null
,并且是与此对象表示相同字符序列的 String
对象时,结果才为 true
。
示例代码如下:
public class Equals {
public static void main(String[] args) {
String s1="abcde";
String s2="DFGER";
boolean b=s1.equals(s2);
System.out.println(b);
}
}
6 length
示例代码如下:
public class Length {
public static void main(String[] args) {
String s="abcd";
System.out.println(s.length());
}
}
7 substring
返回一个新字符串,它是此字符串的一个子字符串。
示例代码:
public class Substring {
public static void main(String[] args) {
String s="HelloWorld";
String s1=s.substring(1,6);
System.out.println(s1);
}
}
8 replace
返回一个新的字符串,它是通过用 newChar
替换此字符串中出现的所有 oldChar
得到的。
示例代码:
public class Replace {
public static void main(String[] args) {
String s="abcdefg";
String s1=s.replace('f', 'd');
System.out.println(s1);
}
}
9 valueOf
返回 char
数组参数的字符串表示形式。
示例代码:
public class ValueOf {
public static void main(String[] args) {
char[] c={'a','b','c','d'};
String s1=String.valueOf(c);
System.out.println(s1);
}
}
10 regionMatches
测试两个字符串区域是否相等。
将此 String 对象的一个子字符串与参数 other 的一个子字符串进行比较。如果这两个子字符串表示相同的字符序列,则结果为 true。
示例代码如下:
public class RegionMatches {
public static void main(String[] args) {
String s1="abcde";
String s2="DFGEa";
boolean b=s1.regionMatches(1,s2,3,2);
System.out.println(b);
}
private static boolean regionMatches(int i, String s2, int j, int k) {
return false;
}
}