Java Code Examples for com.networknt.utility.StringUtils#removeEnd()

The following examples show how to use com.networknt.utility.StringUtils#removeEnd() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: URLNormalizer.java    From light-4j with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Removes any trailing slash (/) from a URL, before fragment
 * (#) or query string (?).</p>
 *
 * <p><b>Please Note:</b> Removing trailing slashes form URLs
 * could potentially break their semantic equivalence.</p>
 * <code>http://www.example.com/alice/ &rarr;
 *       http://www.example.com/alice</code>
 * @return this instance
 * @since 1.11.0
 */
public URLNormalizer removeTrailingSlash() {
    String urlRoot = HttpURL.getRoot(url);
    String path = toURL().getPath();
    String urlRootAndPath = urlRoot + path;

    if (path.endsWith("/")) {
        String newPath = StringUtils.removeEnd(path, "/");
        String newUrlRootAndPath = urlRoot + newPath;
        url = StringUtils.replaceOnce(
                url, urlRootAndPath, newUrlRootAndPath);
    }
    return this;
}
 
Example 2
Source File: URLNormalizer.java    From light-4j with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Removes trailing question mark ("?").</p>
 * <code>http://www.example.com/display? &rarr;
 *       http://www.example.com/display </code>
 * @return this instance
 */
public URLNormalizer removeTrailingQuestionMark() {
    if (url.endsWith("?") && StringUtils.countMatches(url, "?") == 1) {
        url = StringUtils.removeEnd(url, "?");
    }
    return this;
}
 
Example 3
Source File: URLNormalizer.java    From light-4j with Apache License 2.0 3 votes vote down vote up
/**
 * <p>Removes trailing hash character ("#").</p>
 * <code>http://www.example.com/path# &rarr;
 *       http://www.example.com/path</code>
 * <p>
 * This only removes the hash character if it is the last character.
 * To remove an entire URL fragment, use {@link #removeFragment()}.
 * </p>
 * @return this instance
 * @since 1.13.0
 */
public URLNormalizer removeTrailingHash() {
    if (url.endsWith("#") && StringUtils.countMatches(url, "#") == 1) {
        url = StringUtils.removeEnd(url, "#");
    }
    return this;
}