Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Utility class providing additional methods useful for string manipulation
- *
- * @version 0.1
- * @author Marty Chang (Slalom Consulting)
- */
- public class StringUtil {
- /*
- * @param haystack The string
- * @param regExp The regular expression
- * @return The first substring of the string that matches the regular
- * expression; <code>null</code> otherwise
- */
- public static String isolateFirst(String haystack, String regExp) {
- String match = null;
- // Attempt to split the haystack using the regular expression.
- // If the split successfully produces two "ends", use the
- // info about the ends to extract the match in the middle
- List<String> haystackEnds = haystack.split(regExp, 2);
- if (haystackEnds.size() == 2) {
- String frontEnd = haystackEnds.get(0);
- String backEnd = haystackEnds.get(1);
- match = haystack.substring(frontEnd.length(),
- haystack.length() - backEnd.length());
- }
- // Return the match
- return match;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment