Android 将字符串拆分为ArryList后<;字符串>;我无法从任何单元格读取文本
我是一个机器人noobie。我想做的是把这个字符串变成ArrayList。这已经完成了。当我打印它时(使用tv.setText),结果就是我所需要的,但是如果我在下面有,我就找不到“1”。 我想要得到的结果是将文本存储在另一个ArrayList中的Noumber之间,但要到达那里,我必须能够从ArrayList读取字符串Android 将字符串拆分为ArryList后<;字符串>;我无法从任何单元格读取文本,android,arraylist,split,Android,Arraylist,Split,我是一个机器人noobie。我想做的是把这个字符串变成ArrayList。这已经完成了。当我打印它时(使用tv.setText),结果就是我所需要的,但是如果我在下面有,我就找不到“1”。 我想要得到的结果是将文本存储在另一个ArrayList中的Noumber之间,但要到达那里,我必须能够从ArrayList读取字符串 public class MainActivity extends AppCompatActivity { String text = "1Hello12People22Pa
public class MainActivity extends AppCompatActivity {
String text = "1Hello12People22Paul22Jackie21Anna12Fofo2";
TextView tv;
List<String> chars = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.tv);
PrinThemNow();
}
public void PrinThemNow(){
chars = Arrays.asList(text.split(""));
tv.setText(toString().valueOf(chars));
for(int i=0;i<chars.size();i++){
if(toString().valueOf(chars.get(i)) == " 1"){
Toast.makeText(this,"I found One",Toast.LENGTH_LONG).show();
//This if is not working while the TV's text shows " 1"
}
}
}
}
public类MainActivity扩展了AppCompatActivity{
字符串text=“1Hello12People22Paul22Jackie21Anna12Fofo2”;
文本视图电视;
列表字符=新的ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findviewbyd(R.id.tv);
PrinThemNow();
}
public void PrinThemNow(){
chars=Arrays.asList(text.split(“”);
tv.setText(toString().valueOf(chars));
对于(inti=0;i首先,只是一个提示,从string
到char[]
您可以使用
char[] chars = myString.toCharArray();
因为将字符数组
保存为字符串数组列表
但现在问题来了。你有你的字符串,你想打印数字之间的文本
现在还不清楚你的目标是什么,但让我们试试看
我认为您使用了char[],因为它比char[]好10倍,更简单
案例1)要在“1”之间打印文本吗
案例2)您希望打印除数字以外的所有文本(可能会使用空格)
for(字符c:chars){
//检查它是否是一个数字
如果(c>='0'&&c
//lets loop the chars
bool firstOneFound = false;
int firstOccurrence = -1;
int secondOccurrence = -1;
int i = 0;
for(char c : chars){
//is it equals to 1?
if(c.equals('1')){
//check if we are already after the first 1
if(firstOneFound){
//if yes, we found the final one
secondOccurrence = i;
break;
}
else{
//this is the first occurrence
firstOccurrence = i;
firstOneFound = true;
}
}
i++;
}
if(firstOccurrence != -1 && secondOccurrence != -1){
String myFinalString = myString.subString(firstOccurrence, secondOccurrence);
}
for(char c : chars){
//check if it's a number
if(c >= '0' && c <= '9'){
//replace the number with anything else
c = ' '; //if you wanna have it as a space
}
}
//print the final string
String myFinalString = new String(chars);