Reputation: 273
If I have a String array as follows.
String[] name = {"aval", "H ---- ", "Singh", "Sawh", "Raman", " ---- ", "parminder", "kaur", "jaspreet", "asneet", " ---- "};
I want to copy all the contents from between "H ---- " to " ---- ". That is it should be "SinghSawhRaman" (excluding "H ---- " and " ---- ".
Something like this:
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];
}
Can someone please show me the best way to do this?
Upvotes: 1
Views: 2544
Reputation: 158
String[] name = {"aval", "H ---- ", "Singh", "Sawh", "Raman", " ---- ", "parminder", "kaur", "jaspreet", "asneet", " ---- "};
String key ="";
boolean flag = false;
for(String s : name){
if(s.equals("H ---- ")){
flag = true;
}
if(s.equals(" ---- ")){
flag = false;
}
if(flag && !s.equals("H ---- ")){
key+=s;
}
}
System.out.println(key);
Upvotes: 0
Reputation: 19635
Simply break out of the outer loop once your condition has been reached:
boolean capture = false;
for (String n : name) {
if (n.contains("H ---- ")) {
capture = true;
} else if (n.contains(" ---- ")) {
break;
} else if (capture) {
keywords = keywords + n;
}
}
If there are multiple occurrences of H ----
and ----
, then don't break
out of the loop, but simply set capture = false
instead of break
.
With two nested loops, you will be reading any elements that make up part of a name twice, which is inefficient. Unless it is possible to have names with nested H ----
, you are better off removing the inner loop.
Upvotes: 0
Reputation: 21047
String keywords = "";
boolean append = false;
for(j = 0; j <= name.length; j++) {
if(name[j].contains("H----")
append = true;
if(append)
keywords += name[j];
if(name[j].contains("----") {
append = false;
break;
}
}
Notice that, if you move the if(name[j].contains("H----")
block below the if(append)
block, you will exclude the H---
from the keywords
string;
Upvotes: 0
Reputation: 2703
for(j = i; !name[j].contains(" ---- "); j++){
keywords = keywords + name[j];
}
That should work for you, you can set the break condition of a for
loop to be any boolean expression.
Edit: Need the !
in there as loop breaks when condition is false.
Upvotes: 3
Reputation: 201467
I would do it like so
private static String copyFrom(String[] in, String begin, String end) {
StringBuilder sb = new StringBuilder();
boolean start = false;
if (in != null) {
for (String s : in) {
if (!start) {
start = s.equals(begin);
} else {
start = !s.equals(end);
if (start) {
sb.append(s);
}
}
}
}
return sb.toString();
}
public static void main(String[] args) {
String[] name = {"aval", "H ---- ", "Singh", "Sawh",
"Raman", " ---- ", "parminder", "kaur",
"jaspreet", "asneet", " ---- "};
String keywords = copyFrom(name, "H ---- ", " ---- ");
System.out.println(keywords);
}
Outputs
SinghSawhRaman
Upvotes: 0
Reputation: 16067
Any reason that you use contains
instead of equals
?
If using equals
is also fine for you, you could use a List
:
String[] name = {"aval", "H ---- ", "Singh", "Sawh", "Raman", " ---- ", "parminder", "kaur", "jaspreet", "asneet", " ---- "};
List<String> s = Arrays.asList(name);
StringBuilder sb = new StringBuilder();
for(int i = s.indexOf("H ---- ")+1; i < s.indexOf(" ---- "); i++)
sb.append(s.get(i));
System.out.println(sb);
Upvotes: 0