0

这是我的代码。

public static String hBlanks(String a, String b){

    StringBuilder blanks = new StringBuilder();

    int j;
    for(int x = 0; x < a.length(); x++){
      blanks.append('-');
    }
    System.out.println(blanks);
    String strBlanks = blanks.toString();

    for(int i = 0; i < a.length(); i++){
      j = 0;
      while(j < b.length()){
        boolean check = a.contains(b.charAt(j));

//我不断收到布尔检查错误 = a.contains(b.charAt(j)); 线。它说:“java.lang.String 中的包含(java.lang.CharSequence)不能应用于(char)”

        if(check == true){
          strBlanks = blanks.replace('-', "" + a.charAt(i));

//我在 str = strBlanks.replace('-', "" + a.charAt(i)); 线。那个说“找不到符号符号:方法替换(char,java.lang.String)位置:类java.lang.String”

        }else{
          j++;
        }
      }
     return strBlanks;
    }
}
4

2 回答 2

0

To get rid of your compilation issues, Change strBlanks.replace('-', "" + a.charAt(i)); to

strBlanks.replace('-', a.charAt(1));

Also change a.contains(b.charAt(j)); to

a.contains("" + b.charAt(1));
于 2013-04-03T01:18:17.893 回答
0

How about just one line, instead of all that code:

public static String wordBlanks(String a, CharSequence b){
    return a.replaceAll("[^" + b + "]", "-");
}

This works by creating a regex that matches every character not in b and using that to replace ever occurrence in a with a dash.

于 2013-04-03T01:19:41.410 回答