使用循环从字符串中删除空格

时间:2012-09-04 04:36:09

标签: java

大家好我正在尝试使用循环删除空格。这是我到目前为止所提出的内容

    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的新手,任何建议都会非常感谢!

6 个答案:

答案 0 :(得分: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);
        }
    }
}

答案 1 :(得分:1)

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

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

答案 2 :(得分: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(" ", ""));

基本上,没有什么新的,只是每个人都谈到的可运行的例子......

答案 3 :(得分: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还是陌生的,碰巧会遇到一些问题,这些问题仅使用一些方法和循环来删除空格。这就是我的解决方案,请随时尝试。

答案 4 :(得分: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));
        }
     }

    }
   }

答案 5 :(得分: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));
        }
    }
}