0

大家好,我正在尝试专门使用循环删除空格。这是我到目前为止的想法

    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还是很陌生,任何建议都会非常感谢!

4

6 回答 6

4

试试这个:

public class RemoveWhiteSpace {

    public static void main(String[] args) {

        String str = "Hello World... Hai...             How are you?     .";
        for(Character c : str.toCharArray()) {
            if(!Character.isWhitespace(c)) // Check if not white space print the char
                System.out.print(c);
        }
    }
}
于 2012-09-04T04:55:50.940 回答
1

以及主题组合...

StringBuilder result = new StringBuilder(64);
String str = "sample test";
for (Character c : str.toCharArray()) {
    if (!Character.isWhitespace(c)) {
        result.append(c);
    }
}

System.out.println(result.toString()); // toString is not required, but I've had to many people assume that StringBuilder is a String
System.out.println(str.replace(" ", ""));
System.out.println("Double  spaced".replace(" ", ""));

基本上,没有什么新东西,只是其他人所说的可运行的例子......

于 2012-09-04T05:02:44.550 回答
1

为什么不使用正则表达式?replaceAll("\\s","")删除所有空格。您还可以删除其他不可见的符号,例如 \tab 等。

查看docs.oracle.com了解更多信息

于 2012-09-04T04:46:14.500 回答
1
import java.util.Scanner;
public class Iterations{
public static void main(String[] args){
    Scanner kb = new Scanner(System.in);
    System.out.print("Enter a sentence: ");
    String s = kb.nextLine();
    String temp = "";
    for (int i = 0; i < s.length(); i++){    //This loops separates the string into each character
        String c = s.substring(i, i+1);              
        if (c.equals(" ")){
            System.out.print(c.trim());      //If the individual character is space then remove it with trim()
        } else {
            temp = temp + c;                 //Adds the string up into single sentence
        }
    }
    System.out.println(temp);               //Print it to have a nice line of string
}

}

我也是 java 新手,碰巧遇到了只用一些方法和循环删除空格的问题。这就是我的解决方案,请随时尝试。

于 2019-04-21T04:01:52.363 回答
0

通过保持两个循环,您实际上已经走得太远了,您只能在一个循环中完成:

public static void main(String[] args) {
    String input = "";
    char space = ' ';

    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 char by char
        if (input.charAt(i)!= space) { // if the char is not space print it. 
            System.out.print(input.charAt(i));
        }
    }
} 
于 2012-09-04T05:02:08.133 回答
0
public class sample {

public static void main(String[] args) {

    String input = "sample test";
    char noSpace = ' ';
    System.out.println("String original:"+input);

    for (int i = 0; i < input.length(); i++) { //search from right to left
        if (input.charAt(i) != noSpace) { //if there is a space move position of i and j
            System.out.print(input.charAt(i));
        }
     }

    }
   }
于 2012-09-04T04:55:08.457 回答