从 String android 中获取第一个和最后一个字符(数字)
Get first and last character (number) from String android
我正在尝试从字符串中获取第一个和最后一个字符(数字),但我不知道该怎么做。
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);
}
}
txt1.setText(existingAddress.subString(0,1), TextView.BufferType.EDITABLE);
txt2.setText(existingAddress.subString(existingAddress.length()-1), TextView.BufferType.EDITABLE);
String first=existingAddress.substring(0, 1);
String last=existingAddress.substring(existingAddress.length()-1,existingAddress.length());
**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);
}
}
我正在尝试从字符串中获取第一个和最后一个字符(数字),但我不知道该怎么做。
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);
}
}
txt1.setText(existingAddress.subString(0,1), TextView.BufferType.EDITABLE);
txt2.setText(existingAddress.subString(existingAddress.length()-1), TextView.BufferType.EDITABLE);
String first=existingAddress.substring(0, 1);
String last=existingAddress.substring(existingAddress.length()-1,existingAddress.length());
**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);
}
}