嗨,大家好,我試圖刪除使用循環的空白。繼承人我想出迄今使用循環刪除字符串中的空格
import java.util.Scanner;
public class Q2 {
public static void main(String[] args) {
String input = "";
char noSpace = ' ';
Scanner scan = new Scanner(System.in);
input = scan.nextLine();
System.out.println(input);
for (int i = 0; i < input.length(); i++) { //search from right to left
for (int j = input.length(); j != -1; j--) { //search from left to right
if (input.charAt(i) == noSpace) { //if there is a space move position of i and j
i++;
j--;
}
}
System.out.println(input);
我還是很新的Java中,任何建議將是巨大的感謝!
什麼是你的問題只是運行的例子嗎? – maba
通過使用Character.isWhitespace()簡化... http://docs.oracle.com/javase/6/docs/api/java/lang/Character.html#isWhitespace(char) – Adam
個人而言,我會考慮使用[regex](http://docs.oracle.com/javase/tutorial/essential/regex/intro.html)。示例:'sText = sText.replaceAll(「\\ s +」,「」);' – paulsm4