·equals方法
用于比较两个字符串的值是否相等,相等返回True,否则返回False。
public class Main {
public static void main(String[] args) {
String s = "12345678";
String s1 = "12345678";
System.out.println(s.equals(s1)); //返回True
String s2 = "12345678";
String s3 = "87654321";
System.out.println(s2.equals(s3)); //返回False
}
}
运行结果:true
false
·substring方法
用于截取字符串
public class Main {
public static void main(String[] args) {
String str = "12345678";
//从下标为1的位置开始截取至字符串结束
String ret1 = str.substring(1);
System.out.println(ret1);
//从下标为2的位置开始截取至下标为6的位置
String ret2 = str.substring(2, 7);
System.out.println(ret2);
}
}
运行结果:2345678
34567
·chartAt方法
返回指定下标位置的字符,可用于遍历字符串。
public class Main {
public static void main(String[] args) {
String str = "12345678";
for(int i = 0;i < str.length();i++) {
char c = str.charAt(i);
System.out.print(c);
}
}
}
运行结果:12345678
·comparaTo()方法
当比较对象是一个 Byte, Double, Integer, Float, Long 或 Short 类型的参数时比较的是值,当比较对象是字符串类型时,比较的是字符串长度。
1.整数比较
public class Main {
public static void main(String[] args) {
Integer num1 = 12;
Integer num2 = 10;
int result = num1.compareTo(num2);
System.out.println(result);
}
}
运行结果:1
2.字符串比较
public class Main {
public static void main(String[] args) {
String str1 = "apple";
String str2 = "banana";
int result = str1.compareTo(str2);
System.out.println(result);
}
}
输出结果:-1
·indexOf()方法
从原字符串中查找目标字符串,返回第一次出现的位置
public class Main {
public static void main(String[] args) {
String str = "Hello, World! Hello again.";
// 查找字符 'o' 第一次出现的位置
int indexOfO = str.indexOf('o');
System.out.println(indexOfO);
// 查找子字符串 "World" 第一次出现的位置
int indexOfWorld = str.indexOf("World");
System.out.println(indexOfWorld);
}
}
运行结果:4
7
·lastIndexOf()方法
从原字符串中查找目标字符串,返回最后一次出现的位置
public class Main {
public static void main(String[] args) {
String str = "Hello, World! Hello again.";
// 查找字符 'o' 最后一次出现的位置
int lastIndexOfO = str.lastIndexOf('o');
System.out.println(lastIndexOfO);
// 查找子字符串 "Hello" 最后一次出现的位置
int lastIndexOfHello = str.lastIndexOf("Hello");
System.out.println(lastIndexOfHello);
}
}
运行结果:18
14
·contains()方法
用于检查一个字符串是否包含指定的子字符串
public class Main {
public static void main(String[] args) {
String str = "Hello, World!";
boolean containsHello = str.contains("Hello");
boolean containsJava = str.contains("Java");
System.out.println(containsHello); // 输出:true
System.out.println(containsJava); // 输出:false
}
}
运行结果:true
false
·startswith()方法
判断原字符串是否以指定字符串开始
public class Main {
public static void main(String[] args) {
String str = "Hello, World!";
boolean startsWithHello = str.startsWith("Hello");
boolean startsWithJava = str.startsWith("Hi");
System.out.println("开始字符是否为'Hello'? " + startsWithHello); // 输出:true
System.out.println("开始字符是否为'Hi'? " + startsWithJava); // 输出:false
}
}
运行结果:开始字符是否为'Hello'? true
开始字符是否为'Hi'? false
·endswith()方法
判断原字符串是否以指定字符串结尾
public class Main {
public static void main(String[] args) {
String str = "Hello, World!";
boolean endsWith1 = str.endsWith("World!");
boolean endsWith2 = str.endsWith("Java!");
// 输出:true
System.out.println("结束字符是否为'World!'? " + endsWith1);
// 输出:false
System.out.println("结束字符是否为'Java!'? " + endsWith2);
}
}
运行结果:结束字符是否为'World!'? true
结束字符是否为'Java!'? false
·replace()方法
用于替换字符串中所有指定的字符或子串为另一个字符或子串。这个方法不会改变原始字符串,而是返回一个新的字符串,其中所有指定的字符或子串都被替换掉了。
1.替换字符
public class Main {
public static void main(String[] args) {
String str = "Hello, World!";
String replaceStr = str.replace('o', 'a');
System.out.println("原始字符串: " + str);
System.out.println("替换后的字符串: " + replaceStr);
}
}
运行结果:原始字符串: Hello, World!
替换后的字符串: Hella, Warld!
2.替换子串
public class Main {
public static void main(String[] args) {
String str = "Hello, World!";
String replaceStr = str.replace("World", "Java");
System.out.println("原始字符串: " + str);
System.out.println("替换后的字符串: " + replaceStr);
}
}
运行结果:原始字符串: Hello, World!
替换后的字符串: Hello, Java!
·trim()方法
去除首尾空白字符
public class Main {
public static void main(String[] args) {
String str = " Hello, World! ";
String trimstr = str.trim();
System.out.println("原始字符串: \"" + str + "\"");
System.out.println("修剪后的字符串: \"" + trimstr + "\"");
}
}
运行结果:原始字符串: " Hello, World! "
修剪后的字符串: "Hello, World!"
·toUpperCase()方法
将字符串小写字符转化为大写
public class Main {
public static void main(String[] args) {
String str = "Hello, World!";
String Str = str.toUpperCase();
System.out.println(Str);
}
}
运行结果: HELLO, WORLD!
·toLowerCase()方法
将字符串大写字符转化为小写
public class Main {
public static void main(String[] args) {
String Str = "Hello, World!";
String str = Str.toLowerCase();
System.out.println(str);
}
}
运行结果:hello, world!