Well a simple solution can be to split the string around the dashes ("-"), then iterate over the split parts and match your keyword.
But you have to decide what to do when there are multiple matches or no matches at all.
The following code contains 2 basic implementations, one which collects the matches in a list and one which stops after the first match.
The first will return an empty list when there are no matches, the second will return null.
import java.util.ArrayList;
import java.util.List;
public class KeywordMatcher {
private static List<String> getKeywordMatches(String s, String keyword) {
List<String> ret = new ArrayList<>();
String[] parts = s.split("-");
for (String part : parts) {
if(part.contains(keyword))
ret.add(part);
}
return ret;
}
private static String getFirstKeywordMatch(String s, String keyword) {
String[] parts = s.split("-");
for (String part : parts) {
if(part.contains(keyword))
return part;
}
return null;
}
public static void main(String[] args) {
String s ="59USD-300kg-25mb_4G-48p/min(Incl. tax)-70y-2gb+1gb_Toffee-65USD-1km(mm/hr)";
System.out.println(getKeywordMatches(s, "USD")); // prints [59USD, 65USD]
System.out.println(getKeywordMatches(s, "min")); // prints [48p/min(Incl. tax)]
System.out.println(getFirstKeywordMatch(s, "USD")); // prints 59USD
System.out.println(getFirstKeywordMatch(s, "min")); // prints 48p/min(Incl. tax)
}
}
A more sophisticated approach involves searching your string for the next divider ("-") and the next keyword. Iterating the string until its end and keeping track of the relative position of dividers and keywords gets you to the same result in a more memory-efficient way (since you don't create any new object in memory unlike the "split" approach).
However the implementation can be quite cumbersome and difficult to read, so I suggest the one described above, unless you have specific performance requirements or the strings to be searched are MB-sized.
EDIT:
Having seen the other answer using regular expressions, as I commented below, it is unfortunately extremely inefficient.
Look at the following snippet to prove it:
private static String getFirstKeywordMatch(String s, String keyword) {
String[] parts = s.split("-");
for (String part : parts) {
if(part.contains(keyword))
return part;
}
return null;
}
private static String lookForKeyword(String message, String keyword) {
//System.out.println("Looking for keyword \"" + keyword + "\" in string \"" + message + "\"");
String pattern = "^.*?-?([^-]*" + keyword +"[^-]*)-?.*$";
Matcher matcher = Pattern.compile(pattern).matcher(message);
if (matcher.matches()) {
return matcher.group(1);
}
return null;
}
public static void main(String[] args) {
String s ="59USD-300kg-25mb_4G-48p/min(Incl. tax)-70y-2gb+1gb_Toffee-65USD-1km(mm/hr)";
long start=System.currentTimeMillis();
for(int i=0; i<10000000; i++) {
lookForKeyword(s, "USD");
}
System.out.println("Elapsed ms: " + (System.currentTimeMillis()-start));
start=System.currentTimeMillis();
for(int i=0; i<10000000; i++) {
getFirstKeywordMatch(s, "USD");
}
System.out.println("Elapsed ms: " + (System.currentTimeMillis()-start));
}
On my machine the regex approach takes about 4 times than my approach.
So while usually I'm a regex ambassador, unfortunately this is not one of the best use cases for them.