简体   繁体   中英

Splitting based on delimiter and string

String CompanyData = "{ChargeCompany1Cnt:0,ChargeCompany2Cnt:73,ChargeCompany3Cnt:44,BalanceCompany3Cnt:0,ChargeCompany4Flag:green,BalanceCompany2Flag:green,BalanceCompany1Cnt:0,ChargeCompany3Flag:red,ChargeCompany1Flag:green,BalanceCompany4Flag:green,BalanceCompany1Flag:green,BalanceCompany2Cnt:0,BalanceCompany4Cnt:0,BalanceCompany3Flag:green,ChargeCompany2Flag:red,ChargeCompany4Cnt:6}";

CompanyData is my string I am splitting the data like below. There is no issue with this code, but if the order is changed in the string splitting is breaking. how to split this string and assign to another string by its name(like splitting based on ChargeCompany1Cnt, ChargeCompany2Cnt). i have used cut and sed commands in UNIX to do this, right now converting my Shell script into JAVA. So sorry if it's a basic question

String ChargeCompany1Cnt=CompanyData.split(,)[0].replace("{","");
String ChargeCompany2Cnt=CompanyData.split(,)[1];
String ChargeCompany3Cnt=CompanyData.split(,)[2];
String BalanceCompany3Cnt=CompanyData.split(,)[3];
String ChargeCompany1Flag=CompanyData.split(,)[8];

Basically I need to find String like ChargeCompany2Cnt,ChargeCompany1Flag in CompanyData and print ChargeCompany2Cnt:73 ChargeCompany1Flag:green

Please note if this is JSON object you can parse it easily with ObjectMapper of Jacson. you can use the below code for manual parsing

    String CompanyData = "{ChargeCompany1Cnt:0,ChargeCompany2Cnt:73,ChargeCompany3Cnt:44,BalanceCompany3Cnt:0,ChargeCompany4Flag:green,BalanceCompany2Flag:green,BalanceCompany1Cnt:0,ChargeCompany3Flag:red,ChargeCompany1Flag:green,BalanceCompany4Flag:green,BalanceCompany1Flag:green,BalanceCompany2Cnt:0,BalanceCompany4Cnt:0,BalanceCompany3Flag:green,ChargeCompany2Flag:red,ChargeCompany4Cnt:6}";
    HashMap<String,String> mymap = new HashMap<String,String>();
    for ( String s: CompanyData.split("[?,{}]")) {
        if (!s.equals(""))
        mymap.put(s.split(":")[0],s.split(":")[1]); }

        for (HashMap.Entry<String, String> entry : mymap.entrySet()) {
    String key = entry.getKey().toString();;
    String value = entry.getValue();
    System.out.println( key + " =  " + value );

Your question isn't too clear, but perhaps this snippet will point you in the right direction:

List<String> companyCount = new ArrayList<>();
String[] companies = CompanyData.substring(1, -1).split(",");
for (String companyCnt : companies) {
    companyCount.add(companyCnt);
}

Incidentally, you can probably perform this whole operation without use of cut(1) as well.

Depending on how you intend to use the variables you could alternatively create a set of key-value pairs instead of explicitly declaring each variable. Then you could split the names out (ie split each element further on : ) and use them as keys without needing to know which is which.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM