commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Benedikt Ritter (JIRA)" <j...@apache.org>
Subject [jira] [Comment Edited] (LANG-895) Improving StringUtils#substringAfterLast
Date Sat, 11 Jan 2014 13:40:50 GMT

    [ https://issues.apache.org/jira/browse/LANG-895?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13665127#comment-13665127
] 

Benedikt Ritter edited comment on LANG-895 at 1/11/14 1:39 PM:
---------------------------------------------------------------

Here is a possible implementation of the new method public static String substringAfterLast(String
str, String separator, String defaultValue):

{code:java}
public static String substringAfterLast(String str, String separator, String defaultValue)
{
 if (isEmpty(str)) {
            return str;
        }
        if (isEmpty(separator)) {
            return EMPTY;
        }
        int pos = str.lastIndexOf(separator);
        if (pos == INDEX_NOT_FOUND || pos == str.length() - separator.length()) {
            return defaultValue;
        }
        return str.substring(pos + separator.length());
}
{code}

And the refactored, old method public static String substringAfterLast(String str, String
separator) could be implemented as follows:

{code:java}
public static String substringAfterLast(String str, String separator) {
   return substringAfterLast(str, separator, EMPTY);
}
{code}

Cheers,
Ivan


was (Author: ihristov):
Here is a possible implementation of the new method public static String substringAfterLast(String
str, String separator, String defaultValue):

public static String substringAfterLast(String str, String separator, String defaultValue)
{
 if (isEmpty(str)) {
            return str;
        }
        if (isEmpty(separator)) {
            return EMPTY;
        }
        int pos = str.lastIndexOf(separator);
        if (pos == INDEX_NOT_FOUND || pos == str.length() - separator.length()) {
            return defaultValue;
        }
        return str.substring(pos + separator.length());
}

And the refactored, old method public static String substringAfterLast(String str, String
separator) could be implemented as follows:

public static String substringAfterLast(String str, String separator) {
   return substringAfterLast(str, separator, EMPTY);
}

Cheers,
Ivan

> Improving StringUtils#substringAfterLast
> ----------------------------------------
>
>                 Key: LANG-895
>                 URL: https://issues.apache.org/jira/browse/LANG-895
>             Project: Commons Lang
>          Issue Type: New Feature
>          Components: lang.*
>    Affects Versions: 3.1
>         Environment: any
>            Reporter: Ivan Hristov
>            Priority: Minor
>             Fix For: Review Patch
>
>
> At the moment, we have the following method public static String substringAfterLast(String
str, String separator) in the class StringUtils which returns an empty string in case no separator
is present. I think, it would be handy to be able to control what is returned in case no separator
is found. For this reason, I would propose to have a method with the following signature:
> public static String substringAfterLast(String str, String separator, String defaultValue).
The same proposal holds for public static String substringAfter(String str, String separator).
>  



--
This message was sent by Atlassian JIRA
(v6.1.5#6160)

Mime
View raw message