moses
moses

Reputation: 155

Extract Substring from String java

I want to extract specific substrings from a string:

String source = "info1 info1ContentA info1ContentB info3 info3ContentA info3ContentB"+
"info2 info2ContentA";

The result should be:

String info1 ="info1ContentA info1ContentB";
String info2 ="info2ContentA";
String info3 ="info3ContentA info3ContentB";

For me it's very difficult to extract the informations, because sometimes after "info" their are one, two or more content informations. Another problem that occurs is, that the order of info1, info2 etc. is not sorted and the "real data" doesn't contain a ascending number.

My first idea was to add info1, info2, info3 etc to an ArrayList.

private ArrayList<String> arr = new ArrayList<String>();
arr.add("info1");
arr.add("info2");
arr.add("info3");

Now I want to extract the substring with the method StringUtils.substringBetween() from Apache Commons (https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.4):

String result = StringUtils.substringBetween(source, arr.get(0), arr.get(1));

This works, if info1 is in the string before info2, but like I said the "real data" is not sorted.

Any idea how I can fix this?

Upvotes: 0

Views: 1543

Answers (2)

Tempestas Ludi
Tempestas Ludi

Reputation: 1165

You can use a regular expression, like this:

String source = "info1 info1ContentA info1ContentB info3 info3ContentA info3ContentB info2 info2ContentA";
for (int i = 1; i < 3; i++) {
    Pattern pattern = Pattern.compile("info" + i + "Content[A-Z]");
    Matcher matcher = pattern.matcher(source);
    List<String> matches = new ArrayList<>();
    while (matcher.find()) {
        matches.add(matcher.group());
    }
    // process the matches list
}

Upvotes: 0

m.antkowicz
m.antkowicz

Reputation: 13571

Split those string by space and then use String's method startsWith to add the part to proper result string

Map<String, String> resultMap = new HashMap<String, String>();
String[] prefixes = new String[]{"info1", "info2", "info3"};    
String source = "info1 info1ContentA info1ContentB info3 info3ContentA info3ContentB"+" info2 info2ContentA";
String[] parts = source.split(" ");

for(String part : parts) {
    for(String prefix : prefixes) {
        if(part.startsWith(prefix) {
            String currentResult = (resultMap.containsKey(prefix) ? resultMap.get(prefix) + part + " " : part);
            resultMap.put(prefix, currentResult);
        }
    }
}

Also consider using StringBuilder instead of adding string parts


If you cannot be sure that parts will be embraces with spaces you can change at the beginning all part to <SPACE>part in your source string using String replace method

Upvotes: 1

Related Questions