如何在 java eclipse 控制台中左对齐文本。例如,行必须是 50 个字符,并且单词也不应该分开。我尝试了上面的代码,但我认为它不起作用。我怎样才能做到这一点?
public void Left(String str){
if(str.length()>50){
int cursor = 0;
int k = 0;
for (int i = 0; i < str.length(); i++,cursor++) {
if(cursor%50 == 0){
//if(str.charAt(cursor)!=' ')
for (int index = i; index > 0; index--) {
if(str.charAt(index) == 32){
String left = str.substring(0, index);
String right = str.substring(index+1, str.length());
str = left+"\n"+right;
int lineLength = str.split("\n")[k].length();
int d = 50-lineLength;
while(d-- != 0){
String left2 = str.substring(k*50, (k*50)+lineLength);
String right2 = str.substring(lineLength++, str.length());
str = left2+" "+right2;
}
k++;
break;
}
}
}
}
}
System.out.println(str);
}