2.5. Finding Nested Strings

Problem

You want to locate strings nested within other strings.

Solution

Use StringUtils.substringBetween( ). This method will return a string surrounded by two strings, which are supplied as parameters. The following example demonstrates the use of this method to extract content from HTML:

String htmlContent = "<html>\n" +
                     "  <head>\n" +
                     "    <title>Test Page</title>\n" +
                     "  </head>\n" +
                     "  <body>\n" +
                     "    <p>This is a TEST!</p>\n" +
                     "  </body>\n" +
                     "</html>";

// Extract the title from this XHTML content 
String title = StringUtils.substringBetween(htmlContent, "<title>", 
"</title>");

System.out.println( "Title: " + title );

This code extracts the title from this HTML document and prints the following:

Title: Test Page

Discussion

In the Solution section, the substringBetween() method returns the first string between the open and close strings—the title of an HTML document. The previous example only contained one nested element, but what happens when a string contains multiple elements nested in the same two strings? In the following example, three variables are extracted from a string using substringBetween( ):

String variables = "{45}, {35}, {120}" ; List numbers = new ArrayList( ); String variablesTemp = variables; while( StringUtils.substringBetween( variablesTemp, "{", "}" ) != null ) { String numberStr = StringUtils.substringBetween( variables, "{", "}" ); Double number = new Double( numberStr ); numbers.add( number ); variablesTemp = variablesTemp.substring( ...

Get Jakarta Commons Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.