仅替换第一个特定事件

时间:2017-03-28 16:00:10

标签: java string replace

我想替换仅在括号中的空格。我的代码更改了字符串中的所有空格。我不知道为什么。我写了这样的代码。

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    String line;
    line = scanner.nextLine();
    for (int i=0; i<line.length(); i++){
        if (line.charAt(i)=='('){
            while (line.charAt(i)!=')'){
                i++;
                if(line.charAt(i)==' '){
                    line=line.replaceFirst(" ", "-space-");
                }
            }
        }
    }
    System.out.println(line);
}

3 个答案:

答案 0 :(得分:1)

试试这个:

public class TestMain {

    public static void main(String[] args) {
        System.out.println("Enter something");
        Scanner scanner = new Scanner(System.in);
        String line;
        line = scanner.nextLine();

        for (int i=0; i<line.length(); i++) {
            if (line.charAt(i)=='(') {
                while (line.charAt(i)!=')') {
                    if(line.charAt(i)==' ') {
                        line=line.replaceFirst(" ", "-space-");
                        break; //leave while
                    }
                    i++;
                }
            }
            break; //leave for loop
        }
        System.out.println(line);
    }
}

检查输出enter image description here

答案 1 :(得分:1)

刚刚更换了一行:只更改了parantheses中的空格。

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    String line;
    line = scanner.nextLine();
    for (int i=0; i<line.length(); i++){
        if (line.charAt(i)=='('){
            while (line.charAt(i)!=')'){
                i++;
                if(line.charAt(i)==' '){
                    // changed
                    line = line.substring(0, i) + "-space-" + line.substring(i+1, line.length());
                }
            }
        }
    }
    System.out.println(line);     
    scanner.close();
}

答案 2 :(得分:1)

你的逻辑有问题,你在行中使用了replaceFirst方法,&#34;用给定的替换替换了该字符串的第一个子字符串,与给定的正则表达式匹配。&#34;而不是仅消耗空格并插入&#39; -space&#39;。

最好按建议使用正则表达式,但如果你想这样做,试试这个:

SELECT date1, date2, 
1 + extract(year from age(to_timestamp(date1::text,'YYYYMM'),to_timestamp(date2::text,'YYYYMM'))) * 12 
+ extract(month from age(to_timestamp(date1::text,'YYYYMM'),to_timestamp(date2::text,'YYYYMM'))) AS MonthsBetweenInclusive,
extract(year from age(to_timestamp(date1::text,'YYYYMM'),to_timestamp(date2::text,'YYYYMM'))) * 12 
+ extract(month from age(to_timestamp(date1::text,'YYYYMM'),to_timestamp(date2::text,'YYYYMM'))) AS MonthsBetweenExclusive
FROM datetable;

使用regexp,您可以执行以下操作:

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    String line;
    line = scanner.nextLine();
    int begin;
    for (int i = 0; i < line.length(); i++) {
        if (line.charAt(i) == '(') {
            i++;
            begin = i;
            while (line.charAt(i) == ' ') {
                i++;
            }
            if (line.charAt(i) == ')') {
                line = line.substring(0, begin) + 
                        "-space-" + line.substring(i, line.length());
            }
        }
    }
    System.out.println(line);
}

这会给你输出:

  public static void main(String[] args)
  {
    String line = "asdfsadf (       )   (   adsf)  asdf()asdfasdf( )  (  asdf  (     )";
    String line1 = line.replaceFirst("(\\(\\s+\\))", "(-space-)");
    String line2 = line.replaceAll("(\\(\\s+\\))", "(-space-)");

    System.out.println(line);
    System.out.println(line1);
    System.out.println(line2);
  }

根据您的需要,您可以对其进行修改。

相关问题