1

私は大きな会話をしています。文字列の間に文字列として処理しています。多くの空白があり、単語以外の文字も見えない可能性があります。以下は文字列の例です。

public static void main(String[] args) {
  String str = " TWD day count Spot                              6-Sep / 2-Sep 2016 1W7d                        13-Sep / 9-Sep 2016 1M30d                      6-Oct / 4-Oct 2016 2M62d                      7-Nov / 3-Nov 2016 3M91d                      6-Dec / 2-Dec 2016 6M181d                    6-Mar / 2-Mar 2017 9M273d                    6-Jun / 2-Jun 2017 12M365d                  6-Sep / 4-Sep 2017 18M546d                  6-Mar / 2-Mar 2018 24M730d                  6-Sep / 4-Sep 2018";
  str = str.toString().replaceAll(" +", "");
  System.out.println("str="+str.toString().trim().replaceAll(" ", ""));
}

、、、、、、関数などtrim()の空白を削除するために多くの文字列関数をreplaceAll(" ","")試しました。多くの関数を試しましたが、期待どおりに動作しませんでした。replaceAll("\\s","")replaceAll(" +","")replaceAll("\\s\\u00a0","")stringUtils.normalize()

私は以下のような出力を期待しています:

"String str = " TWD 日カウント スポット 2016 年 9 月 6 日 / 9 月 2 日 1W7d 2016 年 9 月 13 日 / 9 月 9 日 1M30d 2016 年 10 月 6 日 / 10 月 4 日 2M62d 2016 年 11 月 7 日 / 11 月 3 日- 2016 年 12 月 6M181d 2017 年 3 月 6 日 / 3 月 2 日 9M273d "

長い白い重複スペースの代わりに 1 つのスペースのみ。

助けてください。

以下のような答えが見つかりました。

System.out.println("str="+str.replaceAll("(?U)\\s+", " "));
4

2 に答える 2

0
public static void main(String []args){
    String str = " TWD day count Spot                              6-Sep / 2-Sep 2016 1W7d                        13-Sep / 9-Sep 2016 1M30d                      6-Oct / 4-Oct 2016 2M62d                      7-Nov / 3-Nov 2016 3M91d                      6-Dec / 2-Dec 2016 6M181d                    6-Mar / 2-Mar 2017 9M273d                    6-Jun / 2-Jun 2017 12M365d                  6-Sep / 4-Sep 2017 18M546d                  6-Mar / 2-Mar 2018 24M730d                  6-Sep / 4-Sep 2018";
    str = str.replaceAll("\\s+", " ");
    System.out.println(str);
}

出力:

TWD 日数 スポット 2016 年 9 月 6 日 / 9 月 2 日 1W7d 2016 年 9 月 13 日 / 9 月 9 日 1M30d 2016 年 10 月 6 日 / 10 月 4 日-2017 年 3 月 / 2 月 2 日 9M273d 2017 年 6 月 6 日 / 6 月 2 日 12M365d 2017 年 9 月 6 日 / 9 月 4 日
于 2017-01-06T02:01:41.317 に答える