我们如何使用Java将字符串的所有空格移到最前面?
Input string = "move these spaces to beginning"
Output string = " movethesespacestobeginning"
最佳答案
试试这个:
String input = "move these spaces to beginning";
int count = input.length() - input.replace(" ", "").length();
String output = input.replace(" ", "");
for (int i=0; i<count; i++) output = " " + output;
System.out.print(output);