javatrim()
public class Main {
public static void main(String[] args) {
// 生成一个包含1000个随机字符串的数组
String[] strings = generateRandomStrings(1000);
// 对数组中的字符串进行trim操作
String[] trimmedStrings = trimStrings(strings);
// 输出结果
for (int i = 0; i < strings.length; i++) {
System.out.println("Original String: " + strings[i]);
System.out.println("Trimmed String: " + trimmedStrings[i]);
System.out.println();
}
}
// 生成包含指定数量随机字符串的数组
public static String[] generateRandomStrings(int count) {
String[] strings = new String[count];
for (int i = 0; i < count; i++) {
strings[i] = generateRandomString();
}
return strings;
}
// 生成一个随机字符串
public static String generateRandomString() {
StringBuilder sb = new StringBuilder();
int length = (int) (Math.random() * 100); // 生成随机字符串的长度
for (int i = 0; i < length; i++) {
char c = (char) (Math.random() * 26 + 'a'); // 生成小写字母
sb.append(c);
}
return sb.toString();
}
// 对字符串数组中的每个字符串进行trim操作
public static String[] trimStrings(String[] strings) {
String[] trimmedStrings = new String[strings.length];
for (int i = 0; i < strings.length; i++) {
trimmedStrings[i] = strings[i].trim();
}
return trimmedStrings;
}
}