如果我有一个String数组,如下所示。
String[] name = {"aval", "H ---- ", "Singh", "Sawh", "Raman", " ---- ", "parminder", "kaur", "jaspreet", "asneet", " ---- "};
我想将所有内容从“ H ----”复制到“ ----”。那应该是“ SinghSawhRaman”(不包括“ H ----”和“ ----”)。
像这样:
String keywords = "";
int j = 0;
for(int i = 0; i < name.length; i++){
if(name[i].contains("H ---- ")){
for(j = i; j < 5; j++){
// There problem is j < 5. I want to give a condition when name[i].contains(" ---- ")
// but not sure how should I be able to do it.
keywords = keywords + name[j];
}
有人可以告诉我最好的方法吗?
最佳答案
for(j = i; !name[j].contains(" ---- "); j++){
keywords = keywords + name[j];
}
这对您应该有用,您可以将
for
循环的中断条件设置为任何布尔表达式。编辑:条件为false时,需要在其中的
!
作为循环中断。关于java - 运行for循环,直到JAVA中的字符串条件为止,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21101153/