javaindexoflastindexof

Get Substring of A String with Spaces and Quotes using IndexOf


I have the following String and would like to extract the contents of rule i.e. My Rule Description Looks Like This:

rule "My Rule Description Looks Like This"      
        followed by some white space other characters such as quotes".

When i use the following I get a java.lang.StringIndexOutOfBoundsException: String index out of range: -2:

String ruleName = rule.substring(rule.indexOf("rule \"" + 7, rule.indexOf("\""));

and when i use lastIndexOf:

String ruleName = rule.substring(rule.indexOf("rule \"" + 7, rule.lastIndexOf("\""));

the code executes OK but the output looks like:

My Rule Description Looks Like This"        
        followed by some white space other characters and quotes

Any ideas why the first option throws an exception using indexOf?


Solution

  • For any sort of complex text extraction, you might want to consider using regular expressions. Here is a short script which can extract the rule, and it avoids nasty string manipulations, which, as you have seen, can be prone to error.

    String line = "rule \"My Rule Description Looks Like This\"\n";
    line += "followed by some white space other characters such as quotes\".";
    String pattern = "rule\\s+\"(.*?)\".*";
    
    Pattern r = Pattern.compile(pattern, Pattern.DOTALL);
    Matcher m = r.matcher(line);
    if (m.find()) {
       System.out.println("Found a rule: " + m.group(1) );
    } else {
       System.out.println("Could not find a rule.");
    }
    

    Output:

    My Rule Description Looks Like This
    

    Demo here:

    Rextester