我是一个机器人noobie。我想要做的是使这个String成为一个ArrayList。这个完成了。当我打印它(与tv.setText),结果是我需要的,但在这个,如果我有正确的下面我找不到“1”。 我想要的结果是将文本存储在另一个ArrayList中的noumbers之间但是要去那里我必须能够从ArrayList中读取字符串。
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"
}
}
}
}
答案 0 :(得分:0)
首先,只需一个提示,从string
到char[]
即可使用
char[] chars = myString.toCharArray();
因为将char array
保存为string ArrayList
但现在问题。你有你的字符串,你想在数字之间打印文本。
目前还不清楚你的目标是什么,但试试吧。
我会假设您使用了char [],因为它好10倍且更容易
//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);
您也可以使用字符串的ArrayList,只需将'
替换为"
希望有所帮助