肇庆市有那家做网站的,学生成绩管理系统 网站建设,cdn,咨询律师免费解答在Java中#xff0c;你可以使用Collections.sort()方法对字符串中的字符进行排序。这个方法会按照字母顺序对字符进行排序。
以下是一个例子#xff1a;
import java.util.Arrays;
import java.util.Collections;
public class Main { public static void main(…在Java中你可以使用Collections.sort()方法对字符串中的字符进行排序。这个方法会按照字母顺序对字符进行排序。
以下是一个例子
import java.util.Arrays;
import java.util.Collections;
public class Main { public static void main(String[] args) { String str hello world; char[] c str.toCharArray();Arrays.sort(c);String sortedStr new String(c); System.out.println(sortedStr); }
}
字符串的常用函数
1、charAt(int index) 返回指定索引位置的字符。
String str Hello, World!;
char charAt2 str.charAt(2); // 返回字符 l
2、length() 返回字符串的长度。
String str Hello, World!;
int length str.length(); // 返回13因为字符串 Hello, World! 有13个字符
3、substring(int beginIndex, int endIndex) 返回从beginIndex到endIndex不含endIndex之间的子字符串。
String str Hello, World!;
String substring str.substring(0, 5); // 返回从索引0到4不包括5的子字符串 Hello
4、indexOf(String substring) 返回子字符串第一次出现的索引如果未找到则返回-1。
String str Hello, World!;
int indexOfWorld str.indexOf(World); // 返回7因为 World 第一次出现在索引7的位置
5、lastIndexOf(String substring) 返回子字符串最后一次出现的索引如果未找到则返回-1也可以传入单个字符。
String str Hello, World!;
int lastIndexOfWorld str.lastIndexOf(World); // 返回12因为 World 最后出现在索引12的位置
6、equals(Object anObject) 比较两个字符串的内容是否相同。
String str1 Hello;
String str2 Hello;
boolean isEqual str1.equals(str2); // 返回true因为两个字符串的内容相同
7、equalsIgnoreCase(String anotherString) 忽略大小写比较两个字符串的内容是否相同。
String str1 Hello;
String str2 HELLO;
boolean isEqualIgnoreCase str1.equalsIgnoreCase(str2); // 返回true因为忽略了大小写两个字符串的内容相同
8、startsWith(String prefix) 测试字符串是否以指定的前缀开始。
String str Hello, World!;
boolean isStartsWith str.startsWith(Hello); // 返回true因为字符串Hello, World!以Hello开始
9、endsWith(String suffix) 测试字符串是否以指定的后缀结束。
String str Hello, World!;
boolean isEndsWith str.endsWith(World!); // 返回true因为字符串Hello, World!以World!结束
10、replace(char oldChar, char newChar) 替换字符串中的所有指定字符。
String str Hello, World!;
String replacedStr str.replace(o, a); // 将所有的o替换为a
System.out.println(Replaced string: replacedStr); // 输出: Hella, Warld!
11、toLowerCase() 将所有字符转换为小写。
String str Hello World!;
String lowerCaseStr str.toLowerCase(); // 将所有字符转换为小写
System.out.println(lowerCaseStr); // 输出: hello world!
12、toUpperCase() 将所有字符转换为大写。
String str Hello World!;
String upperCaseStr str.toUpperCase(); // 将所有字符转换为大写
System.out.println(upperCaseStr); // 输出: HELLO WORLD!
13、trim() 去除字符串首尾的空白字符。
String str Hello World! ;
String trimmedStr str.trim(); // 去除字符串首尾的空白字符
System.out.println(trimmedStr); // 输出: Hello World!
14、replaceFirst(String regex, String replacement) 替换第一次出现的指定字符串。或使用给定的正则表达式替换此字符串中第一次出现的匹配内容。
String str Hello World!;
String replacedStr str.replaceFirst(o, a); // 替换第一次出现的o为a
System.out.println(replacedStr); // 输出: Hella World!String s Hello World;
String replaced s.replaceFirst([a-z], a); // replaced 将变成 Haaa Waa
15、replaceAll(String regex, String replacement) 替换所有出现的指定字符串。或使用给定的正则表达式替换此字符串中所有出现的匹配内容。
String str Hello World!;
String replacedStr str.replaceAll(o, a); // 替换所有出现的o为a
System.out.println(replacedStr); // 输出: Hella Warld!String s Hello World;
String replaced s.replaceAll([a-z], a); // replaced 将变成 Haaaaa Waaaaa
16、split(String regex) 根据给定的正则表达式来分割字符串。
String str Hello, World!;
String[] parts str.split(, ); // 根据逗号和空格分割字符串
System.out.println(Arrays.toString(parts)); // 输出: [Hello, World!]
17、将字符串转换为一个字符数组
String str Hello, World!;
char[] charArray str.toCharArray();
18、concat(String str)将指定字符串连接到此字符串的末尾。
String s1 Hello;
String s2 World;
s1 s1.concat(s2); // s1 现在变成了 Hello World
19、indexOfFirstNonSpace()返回第一个非空格字符的索引如果没有则返回-1。
String s Hello World ;
int index s.indexOfFirstNonSpace(); // index 将变成 3因为第一个非空格字符是 H其索引是 3
20、indexOfLastNonSpace()返回最后一个非空格字符的索引如果没有则返回-1。
String s Hello World ;
int index s.indexOfLastNonSpace(); // index 将变成 7因为最后一个非空格字符是 W其索引是 7
21、matches(String regex)测试此字符串是否匹配指定的正则表达式。
String s 12345;
boolean matches s.matches([0-9]); // matches 将变成 true因为 s 只包含数字字符
22、contains(CharSequence s)测试此字符串是否包含指定的子序列。
String s Hello World;
boolean contains s.contains(World); // contains 将变成 true因为 s 包含 World 子序列