常见方法
-charAt( int index):返回字符串指定位置的字符
public class CharAtExample {
public static void main(String[] args) {
String greeting = "Hello"; // 定义一个字符串
int index = 1; // 选择一个索引,例如1,表示第二个字符
// 使用charAt方法获取指定索引的字符
char character = greeting.charAt(index);
// 打印结果
System.out.println("The character at index " + index + " is: " + character);
}
}
The character at index 1 is: e
-indexOf(String s):返回指定字符串第一次出现的位置
public class IndexOfExample {
public static void main(String[] args) {
String text = "Java is fun and Java is also great!";
String searchString = "is";
// 寻找子串在文本中第一次出现的位置
int index = text.indexOf(searchString);
// 打印结果
if (index != -1) {
System.out.println("The string \"" + searchString + "\" first appears at index: " + index);
} else {
System.out.println("The string \"" + searchString + "\" does not appear in the text.");
}
}
}
The string "is" first appears at index: 4
-startsWith(String s):测试字符串是否以指定前缀开始
public class StartsWithExample {
public static void main(String[] args) {
String text = "Hello, World!";
String prefix = "Hello";
// 检查字符串是否以指定的前缀开始
boolean startsWithPrefix = text.startsWith(prefix);
// 打印结果
if (startsWithPrefix) {
System.out.println("The string starts with the prefix: \"" + prefix + "\"");
} else {
System.out.println("The string does not start with the prefix: \"" + prefix + "\"");
}
}
}
输出:
The string starts with the prefix: "Hello"
-endsWith(String s):测试字符串是否以指定后缀开始
public class EndsWithExample {
public static void main(String[] args) {
String text = "Java is fun!";
String suffix = "fun";
// 检查字符串是否以指定的后缀结束
boolean endsWithSuffix = text.endsWith(suffix);
// 打印结果
if (endsWithSuffix) {
System.out.println("The string ends with the suffix: \"" + suffix + "\"");
} else {
System.out.println("The string does not end with the suffix: \"" + suffix + "\"");
}
}
}
//它会输出:
The string ends with the suffix: "fun"
-subString(int index):返回字符串的子字符串
public class SubstringExample {
public static void main(String[] args) {
String text = "Java Programming Language";
int index = 5; // 从索引5开始截取子字符串
// 使用subString方法获取从指定索引到字符串末尾的子字符串
String sub = text.substring(index);
// 打印结果
System.out.println("The substring starting from index " + index + " is: \"" + sub + "\"");
}
}
//输出:
The substring starting from index 5 is: "Programming Language"
-replace(char a,char b):替换字符串的指定字符
public class ReplaceExample {
public static void main(String[] args) {
String originalString = "Hello World!";
char toReplace = 'o'; // 要替换的字符
char replacement = '0'; // 替换后的字符
// 使用replace方法替换字符
String replacedString = originalString.replace(toReplace, replacement);
// 打印原始字符串和替换后的字符串
System.out.println("Original string: " + originalString);
System.out.println("Replaced string: " + replacedString);
}
}
//输出:
Original string: Hello World!
Replaced string: Hell0 W0rld!
-trim():去掉字符串的前后空格
public class TrimExample {
public static void main(String[] args) {
String originalString = " Hello, World! ";
// 使用trim方法去掉字符串前后的空格
String trimmedString = originalString.trim();
// 打印原始字符串和去掉空格后的字符串
System.out.println("Original string: '" + originalString + "'");
System.out.println("Trimmed string: '" + trimmedString + "'");
}
}
输出:
Original string: ' Hello, World! '
Trimmed string: 'Hello, World!'
-concat(String str):连接两个字符串
public class ConcatExample {
public static void main(String[] args) {
String baseString = "Hello, ";
String附加String = "World!";
// 使用concat方法连接两个字符串
String concatenatedString = baseString.concat(附加String);
// 打印连接后的字符串
System.out.println("Concatenated string: " + concatenatedString);
}
}
输出:
Concatenated string: Hello, World!
-split(String regex):给定正则表达式的匹配来拆分字符串
public class SplitExample {
public static void main(String[] args) {
String text = "apple,orange,banana,grape";
String regex = ","; // 正则表达式,用于按逗号拆分字符串
// 使用split方法根据正则表达式拆分字符串
String[] fruits = text.split(regex);
// 打印拆分后的字符串数组
System.out.println("Split strings:");
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}
输出:
Split strings:
apple
orange
banana
grape
标签:index,String,常见,println,字符串,方法,public,string
From: https://www.cnblogs.com/jmy3/p/18367907