Javaの - 特定の方法で、切断された文字列

Iothin:

私は(ファイルから取られた)文字列を持っています:

コンピュータ:Intelのグラフィックカード:NVIDIAの、

マウス:Razerの、色:白など

「」および「:」私は間の言葉を取る必要があります。

私はその方法でこれをやっているとき

Scanner sc = new Scanner(new File(path));
    String str = sc.nextLine();

    ArrayList<String> list = new ArrayList<String>();

    while (sc.hasNextLine()) {

        for (int i = 0; i < str.length(); i++) {
            list.add(str.substring(str.indexOf(":"), str.indexOf(",")));
        }
        System.out.println("test");

        sc.nextLine();

    }

私は取っている「:インテル」。私は、同じ行と次の行からそれらの単語からより多くの単語を取る方法がわかりません。

アービンド・クマールのAvinash:

ファイルの内容を想定すると、test.txt次のとおりです。

Computer: intel, graphic card: Nvidia
Mouse: razer, color: white

次のプログラムの意志

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

class Coddersclub {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner sc = new Scanner(new File("test.txt"));
        ArrayList<String> list = new ArrayList<String>();
        String str = "";
        while (sc.hasNextLine()) {
            str = sc.nextLine();
            String[] specs = str.split(",");
            for (String item : specs) {
                list.add(item.substring(item.indexOf(":") + 1).trim());
            }
        }
        System.out.println(list);
    }
}

出力:

[intel, Nvidia, razer, white]

注:あなたがのようにリストを探している場合は[: intel, : Nvidia, : razer, : white]、交換してくださいlist.add(item.substring(item.indexOf(":") + 1).trim());list.add(item.substring(item.indexOf(":")).trim());

あなたが何か他のものを探しているなら、コメントにお気軽に。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=4463&siteId=1