@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txt1 = (TextView) findViewById(R.id.txt1);
TextView txt2 = (TextView) findViewById(R.id.txt2);
TextView txt3 = (TextView) findViewById(R.id.txt3);
TextView txt = (TextView) findViewById(R.id.txt4);
String myStringValue = "Android programming is fun.";
txt1.setText(myStringValue);
txt2.setText(myStringValue.charAt(0) + "");
}
我想在charAt()中选择多个字符。
答案 0 :(得分:0)
charAt()始终返回一个字符。如果您想要更多,则必须使用其他方法或为您的目的创建循环。
例如
String myStringValue = "Android programming is fun.";
StringBuilder textViewText = new StringBuilder();
for(int i = 0 ; i< myStringValue.length();i++){
if (i>=24 && i<=26){
textViewText.append(myStringValue.charAt(i));
}
}
Log.i("test",textViewText.toString());//print fun
或使用substring
方法
txt2.setText(myStringValue.substring(24,26) + "");//print fun