2016-10-21 6 views
-4

例えば、 "int bot = 235;"のように、テキストファイルの行から、 "bot"と "235"だけを抽出してJavaのHashMapに格納したいとします。テキストファイルの行から "="演算子の右辺値と左辺値のみを抽出するにはどうすればよいですか?

+1

はあなたが必要となります、いくつかの文字列解析魔法、そして実際に**試みる**。 – MordechayS

+0

何を試しましたか? '' String''として利用できる行はありますか? '' String''オブジェクトが提供するメソッドが何であるかを見てみましょう。いくつかの努力をしてください。 – f1sh

+0

あなたの好きな検索エンジンを使用し、 "正規表現"を探します。彼らはあなたのニーズに合うかもしれません – Loopo

答えて

1

次のような文字列関数splitを、使用することができます。

String[] s = string.split("="); 
String s1 = string[0]; // "int bot " 
String s2 = parts[1]; // " 235;" 
+2

'' 's2''にはセミコロンも最後にあります。 – f1sh

+0

@ f1sh true、edited。 –

2

あなたは正規表現を使用できます。社内のデータ構造を:

String detail = "int bot = 235"; 
    String pattern = "(\\w+) = (\\w+)"; 
    Pattern r = Pattern.compile(pattern); 
    Matcher m = r.matcher(detail); 
    HashMap<String, String> result = new HashMap<>(); 
    while (m.find()) { 
     result.put(m.group(1), m.group(2)); 
    } 
    System.out.println(result); 

{bot=235} 
関連する問題