Android spinner在逗号后获取文本

时间:2014-06-23 08:39:12

标签: android string spinner country

在我的android应用程序中,我使用微调器来选择数据..我为要在微调器中显示的字符串创建了字符串数组。我把所有细节都放在了strings文件夹中。一旦用户选择了项目,我希望所选文本显示在编辑文本中。

例如:spinner用于选择国家代码,假设用户选择了USA 那么所选文本将是这样的

美利坚合众国,+ 001

我不需要拍摄所有文字并以编辑文字显示。我只需要逗号之后的文本,即+001。那么有没有办法让逗号后面的文本

Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();

我知道这将显示所有文字我只想要在逗号后删除的文字

5 个答案:

答案 0 :(得分:1)

String seperated[] = spinner.getSelectedItem().toString().split(",");
text = seperated[1];

这将仅返回“+001”。

答案 1 :(得分:1)

您可以在逗号上分割文字:

String text = spinner.getSelectedItem().toString();
String[] splited_text = text.split(",");
text = splited_text[1];

答案 2 :(得分:1)

假设文本名称为text。用这个:

String[] temp = text.split(",")
String code  = temp[1]; //+001 the code after , temp[0] contains the rest

答案 3 :(得分:1)

String code = text.substring(text.indexOf(','));

答案 4 :(得分:0)

这是在最后一个逗号之后获取字符串的方法:

        String founded;
        Pattern p = Pattern.compile(".*,\\s*(.*)");
        String dd = (String) "Your string, really, really2";
        Matcher m = p.matcher(dd);

        if (m.find()) {
            founded = m.group(1); //returns "really2"
        }