/** * <p>Gets the String that is nested in between two instances of the * same String.</p> * * <p>A {@code null} input String returns {@code null}. * A {@code null} tag returns {@code null}.</p> * * <pre> * StringUtils.substringBetween(null, *) = null * StringUtils.substringBetween("", "") = "" * StringUtils.substringBetween("", "tag") = null * StringUtils.substringBetween("tagabctag", null) = null * StringUtils.substringBetween("tagabctag", "") = "" * StringUtils.substringBetween("tagabctag", "tag") = "abc" * </pre> * * @param str the String containing the substring, may be null * @param tag the String before and after the substring, may be null * @return the substring, {@code null} if no match * @since 2.0 */ public static String substringBetween(final String str, final String tag) { return substringBetween(str, tag, tag); }
@Test public void testSubstringBetween_StringStringString() { assertNull(StringUtils.substringBetween(null, "", "")); assertNull(StringUtils.substringBetween("", null, "")); assertNull(StringUtils.substringBetween("", "", null)); assertEquals("", StringUtils.substringBetween("", "", "")); assertEquals("", StringUtils.substringBetween("foo", "", "")); assertNull(StringUtils.substringBetween("foo", "", "]")); assertNull(StringUtils.substringBetween("foo", "[", "]")); assertEquals("", StringUtils.substringBetween(" ", " ", " ")); assertEquals("bar", StringUtils.substringBetween("<foo>bar</foo>", "<foo>", "</foo>") ); }
@Test public void testSubstringBetween_StringString() { assertNull(StringUtils.substringBetween(null, "tag")); assertEquals("", StringUtils.substringBetween("", "")); assertNull(StringUtils.substringBetween("", "abc")); assertEquals("", StringUtils.substringBetween(" ", " ")); assertNull(StringUtils.substringBetween("abc", null)); assertEquals("", StringUtils.substringBetween("abc", "")); assertNull(StringUtils.substringBetween("abc", "a")); assertEquals("bc", StringUtils.substringBetween("abca", "a")); assertEquals("bc", StringUtils.substringBetween("abcabca", "a")); assertEquals("bar", StringUtils.substringBetween("\nbar\n", "\n")); }
final String filename = StringUtils.substringBetween(contentDisposition, "filename=\"", "\"");
/** * Extracts module name from a metadata identification string * * @param metadataId * @return module name or null if metadataId is not a valid metadata identification string */ public static final String getModuleFromIdentificationString(String metadataId) { return StringUtils.defaultString(StringUtils.substringBetween(metadataId, "#", ":"), ""); } }
@Override public void setUri(String uri) { try { portName = StringUtils.substringBetween(uri, ConnectionDriver.JSSC.getProtocol(), ":"); baudRate = Integer.valueOf(StringUtils.substringAfterLast(uri, ":")); } catch (Exception e) { throw new ConnectionException("Couldn't parse connection string " + uri, e); } }
@Override public void setUri(String uri) { try { String portName = StringUtils.substringBetween(uri, ConnectionDriver.JSERIALCOMM.getProtocol(), ":"); int baudRate = Integer.valueOf(StringUtils.substringAfterLast(uri, ":")); initSerialPort(portName, baudRate); } catch (Exception e) { throw new ConnectionException("Couldn't parse connection string " + uri, e); } }
@Override public void setUri(String uri) { try { host = StringUtils.substringBetween(uri, ConnectionDriver.TCP.getProtocol(), ":"); port = Integer.valueOf(StringUtils.substringAfterLast(uri, ":")); } catch (Exception e) { throw new ConnectionException("Couldn't parse connection string " + uri, e); } if (StringUtils.isEmpty(host)) { throw new ConnectionException("Empty host in connection string."); } if ((port < 1) || (port > 65535)) { throw new ConnectionException("Please ensure port is a port number between 1 and 65535."); } }
@Override public List<String> getApplicationProfiles(String moduleName) { List<String> profiles = new ArrayList<String>(); final String applicationConfigFilename = StringUtils.removeEnd(getSpringConfigLocation(moduleName), DEFAULT_APPLICATION_CONFIG_FILE_EXTENSION); // Find application config files for (final FileDetails applicationConfig : fileManager .findMatchingAntPath(applicationConfigFilename + "*" + DEFAULT_APPLICATION_CONFIG_FILE_EXTENSION)) { final String applicationConfigPath = applicationConfig.getCanonicalPath(); if (!fileManager.exists(applicationConfigPath)) { continue; } // Extract profile String profile = StringUtils.substringBetween(applicationConfigPath, applicationConfigFilename, ".properties"); profiles.add(StringUtils.removeStart(profile, "-")); } return profiles; }
@Override public String getFieldName() { final String fieldName = StringUtils.substringBetween(identifier, ".", ";"); return fieldName; }
/** * Extracts module name from a metadata identification string * * @param metadataId * @return module name or null if metadataId is not a valid metadata identification string */ public static final String getModuleFromIdentificationString(String metadataId) { return StringUtils.defaultString(StringUtils.substringBetween(metadataId, "#", ":"), ""); } }
protected String resolveDomain(HttpServletRequest request) { final String url = request.getRequestURL().toString(); // protocol://domain:optionalPort/u/r.i final String domain = StringUtils.substringBefore(StringUtils.substringBetween(url, "://", "/"), ":"); return domain; } }
@Override String getValue(String uri, String placeholder) { final int index = NumberUtils.toInt(StringUtils.substringBetween(placeholder, "[", "]"), -1); String[] parts = URI.create(uri).getPath().split("/"); return parts.length > index ? parts[index] : ""; } },
public JLabel getIconLabel(String iconPath) { JLabel iconLabel = new JLabel(); try { BufferedImage buttonIcon = ImageIO.read(getClass().getClassLoader().getResource(iconPath)); iconLabel.setIcon(new ImageIcon(buttonIcon)); } catch (Exception e) { return getTextLabel(StringUtils.substringBetween(iconPath, "/", ".")); } return iconLabel; }
@Override protected void process(String message) { MercuryStoreCore.playerJoinSubject.onNext(StringUtils.substringBetween(message, " : ", " has joined the area.")); }
@Override protected String getValueFromSource(String key) { if (!USER_SETTINGS.exists()) { return null; } String content = getContent(USER_SETTINGS); String open = "<" + key + ">"; String close = "</" + key + ">"; return substringBetween(content, open, close); }
protected String getUser(String url) { for (String prefix : knownPrefixes) { if (url.startsWith(prefix)) { return checkNotBlank(substringBetween(url, prefix, "/"), "username"); } } throw illegalArgument("unable to extract GitHub username -> [%s]", url); }
private String applyQuoted(String identifier) { String unquotedIdentifier = substringBetween(identifier, quoteString); if (storeLowerCaseQuoted) { unquotedIdentifier = lowerCase(unquotedIdentifier); } else if (storeUpperCaseQuoted) { unquotedIdentifier = upperCase(unquotedIdentifier); } return unquotedIdentifier; }
@Test public void itProperlyTokenizesCommentBlocksContainingTags() { List<Token> tokens = tokens("comment-with-tags"); assertThat(tokens).hasSize(5); assertThat(tokens.get(4)).isInstanceOf(TagToken.class); assertThat(StringUtils.substringBetween(tokens.get(4).toString(), "{%", "%}").trim()).isEqualTo("endif"); }
@Test public void itProperlyTokenizesMultilineCommentTokens() { List<Token> tokens = tokens("multiline-comment"); assertThat(tokens).hasSize(3); assertThat(tokens.get(2)).isInstanceOf(TextToken.class); assertThat(StringUtils.substringBetween(tokens.get(2).toString(), "{~", "~}").trim()).isEqualTo("goodbye."); }