【问题标题】:Get first and last character (number) from String android从String android获取第一个和最后一个字符(数字)
【发布时间】:2015-07-09 13:41:28
【问题描述】:

我正在尝试从字符串中获取第一个和最后一个字符(数字),但我不知道该怎么做。

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);

    }
}

【问题讨论】:

  • 是否需要获取字符串的第一个和最后一个字符(必须是数字)?
  • 到目前为止你尝试了什么?您的问题包含很多挫败感,但甚至没有一次尝试。 Android SDK中没有LeftRight方法,是你自己写的吗?你为什么不给我们看这些sn-ps?
  • 我用左右函数来说明我想要什么...谢谢回复

标签: android get numbers character entity-framework-migrations


【解决方案1】:
txt1.setText(existingAddress.subString(0,1), TextView.BufferType.EDITABLE);
txt2.setText(existingAddress.subString(existingAddress.length()-1),  TextView.BufferType.EDITABLE);

【讨论】:

  • 也许您可以添加一些信息,为什么这会解决问题?
  • 简单而有效的感谢 Mohammed 的回复与良好的问候
【解决方案2】:
String first=existingAddress.substring(0, 1);
String last=existingAddress.substring(existingAddress.length()-1,existingAddress.length());

【讨论】:

  • 感谢您以美丽的问候回复
【解决方案3】:
**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);
}

}

【讨论】:

  • 非常感谢,非常感谢您的回复
  • 感谢您以美丽的问候回复
猜你喜欢
  • 2021-06-23
  • 2019-09-10
  • 1970-01-01
  • 2020-10-04
  • 2011-04-11
  • 1970-01-01
  • 1970-01-01
  • 2016-11-20
相关资源
最近更新 更多