从String android获取第一个和最后一个字符(数字)

时间:2015-07-09 13:41:28

标签: android get numbers character ef-migrations

我想从String中获取第一个和最后一个字符(数字),但我不知道该怎么做。

public void getadrrss (){
    SharedPreferences  sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    String Key = getString(R.string.address);
    String existingAddress = sharedPreferences.getString(Key, null);
    if (existingAddress ==null) {
        existingAddress=("11");
    }
    if (existingAddress !=null){
        EditText txt1 = (EditText)findViewById(R.id.editText1);
        EditText txt2 = (EditText)findViewById(R.id.editText2);
        EditText txt11 = (EditText)findViewById(R.id.editText11);
        txt11.setText((existingAddress), TextView.BufferType.EDITABLE);

 //**The problem is here**

        //GET first and last character (number) FROM existingAddress 

        txt1.setText(Left(existingAddress, 1), TextView.BufferType.EDITABLE);
        txt2.setText(Right(existingAddress,1), TextView.BufferType.EDITABLE);

    }
}

3 个答案:

答案 0 :(得分:1)

txt1.setText(existingAddress.subString(0,1), TextView.BufferType.EDITABLE);
txt2.setText(existingAddress.subString(existingAddress.length()-1),  TextView.BufferType.EDITABLE);

答案 1 :(得分:0)

String first=existingAddress.substring(0, 1);
String last=existingAddress.substring(existingAddress.length()-1,existingAddress.length());

答案 2 :(得分:0)

**Try this code**

public class MainActivity extends Activity
{

private TextView txtView;
private char str1,str2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtView = (TextView) findViewById(R.id.txtView);

    String str = "Hello Friends";
    str1 = str.charAt(0);
    str2 = str.charAt(str.length()-1);

    txtView.setText(""+str1+""+str2);
}

}
相关问题