在某些字符后分割字符串,并将其保存到Java中的数组中

问题描述 投票:0回答:1

我有以下字符串数组。

String [] MakesModels = {"BMW-Series 1, Series2", "Citroen-C1,C2", "Audi-A1, A2"};

从此字符串中,我需要取出作为(BMW, Citroen and Audi)的“ makes”以将其填充到JCombobox中。到目前为止,我已经做到了。

for (String m : MakesModels){
    MakeCMB.setModel(default new comboBoxModel(m.substring(0, (m.indexOf("-")))));
}

再次从此字符串中,我需要取出正在(Series 1, Series 2, etc...)的模型以在第二个JComboBox中填充它们。

split substring indexof
1个回答
0
投票

尝试,

for (String m : MakesModels){
            //Get maker 
           m.substring(0, (m.indexOf("-")));
           //Get models - array
          m.substring(m.indexOf("-")+1).split(",");            

        }
© www.soinside.com 2019 - 2024. All rights reserved.