题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.nextLine(); sort(str); } public static void sort(String str){ ArrayList<String> list=new ArrayList<>(); for (String s : str.split("")) { if(s.matches("[a-z]||[A-Z]")){ list.add(s); } } Collections.sort(list,String::compareToIgnoreCase); StringBuilder result = new StringBuilder(); for (int i = 0, j = 0; i < str.length(); i++) { if (Character.isLetter(str.charAt(i))) { result.append(list.get(j++)); } else { result.append(str.charAt(i)); } } System.out.println(result); } }