当前位置: 动力学知识库 > 问答 > 编程问答 >

string - Android spinner get text after comma

问题描述:

In my android application I am using spinner for selecting data.. and I created string array for strings that to be displayed in spinner. I put all the details in strings folder. I wanted the selected text t be displayed in edit text once the user selected item..

For example : spinner is used to select country codes suppose user selected USA

then the selected text will be like this

United States of America,+001

I don't need t take all the text and display it in edit text. I need only the text after comma, that is +001. So is there any way to get the text after the comma only

Spinner spinner = (Spinner)findViewById(R.id.spinner);

String text = spinner.getSelectedItem().toString();

I know this will display all text I want only text that dislpaying after comma

网友答案:

You can split your text on the comma:

String text = spinner.getSelectedItem().toString();
String[] splited_text = text.split(",");
text = splited_text[1];
网友答案:
String seperated[] = spinner.getSelectedItem().toString().split(",");
text = seperated[1];

This will return only "+001".

网友答案:

Suppose the text is in a string name text. use this:

String[] temp = text.split(",")
String code  = temp[1]; //+001 the code after , temp[0] contains the rest
网友答案:
String code = text.substring(text.indexOf(','));
分享给朋友:
您可能感兴趣的文章:
随机阅读: