Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use StringUtils.substringsBetween();?

can someone demonstrate how to work with StringUtils.substringsBetween() method in java?

like image 318
Navdroid Avatar asked Jan 11 '12 07:01

Navdroid


3 Answers

String foo = "<foo>foo</foo>";
String bar = StringUtils.substringBetween(foo, "<foo>", "</foo>");

The variable bar will have the String "foo".

like image 133
Ramon Saraiva Avatar answered Oct 14 '22 14:10

Ramon Saraiva


This one? Pretty clear from the JavaDoc:

Searches a String for substrings delimited by a start and end tag, returning all matching substrings in an array.

A null input String returns null. A null open/close returns null (no match). An empty ("") open/close returns null (no match).

StringUtils.substringsBetween("[a][b][c]", "[", "]") = ["a","b","c"]
StringUtils.substringsBetween(null, *, *)            = null
StringUtils.substringsBetween(*, null, *)            = null
StringUtils.substringsBetween(*, *, null)            = null
StringUtils.substringsBetween("", "[", "]")          = []
like image 30
aspartame Avatar answered Oct 14 '22 14:10

aspartame


String bigString = "Quick brown fox jumps over the lazy dog";
String smallString = org.apache.commons.lang.StringUtils.subStringBetween(bigString, "brown", "the");

System.out.println(smallString);

output - 

jumps over
like image 36
Acn Avatar answered Oct 14 '22 15:10

Acn