Reputation: 35
I want to make a program how it returns an ArrayList with each substring (from a String) in order.
static boolean tropChangements(String branche, int ocurrTaches) {
ArrayList dispositif = new ArrayList();
String pub = "[public]";
String priv = "[private]";
String brancheDecoup;
int ptVirg = branche.indexOf(" ; ");
for(int i = 0; i<ocurrTaches; i++) {
brancheDecoup = [...] //use ptVirg
if (brancheDecoup.contains(pub)) {
dispositif.add("public");
} else if (branche.contains(priv)) {
dispositif.add("private");
}
}
//[...] It's OK here
}
So, I want in "BrancheDecoup" only the first occurence of my substring and the next one, next one etc. (The end of String is with a ".")
My String, for example : EcrireNomEtudiant[public] ; EntrerMDP[private] ; AvecQui[private] ; ChoisirJour[perso] ; VerifDisponibilites[system] ; AfficherRecupilatif[private] ; EnvoyerLaDemande[system].
And I want this in result -->
In ArrayList : "public", "private", "private", "perso", "private"
Thank you very much in advance.
Best regards, Vincent.
Upvotes: 1
Views: 54
Reputation: 15254
You can first split the string and then use regex to match the pattern
Pattern p = Pattern.compile("\\[(.*?)]");
String str = "EcrireNomEtudiant[public] ; EntrerMDP[private] ; AvecQui[private] ; ChoisirJour[perso] ; VerifDisponibilites[system] ; AfficherRecupilatif[private] ; EnvoyerLaDemande[system]";
Function<String, String> finder = s -> {
Matcher m = p.matcher(s);
return m.find() ? m.group(1) : null;
};
List<String> list = Arrays.stream(str.split(";"))
.map(finder)
.collect(Collectors.toList());
System.out.println(list);
Upvotes: 4
Reputation: 1253
Consider Apache Commons Lang lib usage, which makes the above (@sidgate) code even more clear.
So, complete listing will be:
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang.StringUtils;
public class Example {
static String sample =
"EcrireNomEtudiant[public] ; EntrerMDP[private] ; AvecQui[private] ; ChoisirJour[perso] ; VerifDisponibilites[system] ; AfficherRecupilatif[private] ; EnvoyerLaDemande[system]";
public static void main(String[] args) {
tropChangements(sample);
}
static void tropChangements(String branche) {
List<String> list = Arrays
.stream(branche.split(";"))
.map(String::trim)
.map(s -> StringUtils.substringBetween(s, "[", "]"))
.collect(Collectors.toList());
System.out.println(list);
}
}
Upvotes: 2
Reputation: 9658
Not sure i understand fully what you're trying to do, but this would give you the output you're expecting:
List<String> dispositif = new ArrayList<>();
Pattern re = Pattern.compile("\\[(public|private|perso)\\]");
Matcher matcher = re.matcher(branche);
while (matcher.find()) {
dispositif.add(matcher.group(1));
}
Upvotes: 3
Reputation: 1478
Try this:
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
String sample = "EcrireNomEtudiant[public] ; EntrerMDP[private] ; AvecQui[private] ; ChoisirJour[perso] ; VerifDisponibilites[system] ; AfficherRecupilatif[private] ; EnvoyerLaDemande[system].";
tropChangements(sample);
}
static void tropChangements(String branche) {
Set<String> dispositif = new HashSet<String>(); // use Set so that there will be no duplicates
String[] branches = branche.split(";"); // use split method to split string by ";"
for (String s : branches) { // iterate string
int indexOfOpen = s.indexOf("["); // get the index of "["
int indexOfClose = s.lastIndexOf("]"); // get the index of "]"
if (indexOfOpen > 0 && indexOfClose > 0) { // check if both "[" and "]" is found
String tmp = s.substring(indexOfOpen + 1, indexOfClose); // get the needed string
dispositif.add(tmp); //add it to the set
}
}
System.out.println(dispositif); // print the set
}
}
Result : [perso, private, system, public]
Upvotes: 1