[jira] [Commented] (LANG-1124) Add split by length methods in StringUtils
[ https://issues.apache.org/jira/browse/LANG-1124?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16222323#comment-16222323 ] ASF GitHub Bot commented on LANG-1124: -- Github user asfgit closed the pull request at: https://github.com/apache/commons-lang/pull/75 > Add split by length methods in StringUtils > -- > > Key: LANG-1124 > URL: https://issues.apache.org/jira/browse/LANG-1124 > Project: Commons Lang > Issue Type: New Feature > Components: lang.* >Reporter: Loic Guibert > > Add methods to split a String by fixed lengths : > {code:java} > public static String[] splitByLength(String str, int ... lengths); > public static String[] splitByLengthRepeatedly(String str, int ... lengths); > {code} > Detail : > {code:java} > /** > * Split a String into an array, using an array of fixed string > lengths. > * > * If not null String input, the returned array size is same as the input > lengths array. > * > * A null input String returns {@code null}. > * A {@code null} or empty input lengths array returns an empty array. > * A {@code 0} in the input lengths array results in en empty string. > * > * Extra characters are ignored (ie String length greater than sum of > split lengths). > * All empty substrings other than zero length requested, are returned {@code > null}. > * > * > * StringUtils.splitByLength(null, *) = null > * StringUtils.splitByLength("abc")= [] > * StringUtils.splitByLength("abc", null) = [] > * StringUtils.splitByLength("abc", [])= [] > * StringUtils.splitByLength("", 2, 4, 1) = [null, null, null] > * > * StringUtils.splitByLength("abcdefg", 2, 4, 1) = ["ab", "cdef", "g"] > * StringUtils.splitByLength("abcdefghij", 2, 4, 1) = ["ab", "cdef", "g"] > * StringUtils.splitByLength("abcdefg", 2, 4, 5) = ["ab", "cdef", "g"] > * StringUtils.splitByLength("abcdef", 2, 4, 1) = ["ab", "cdef", null] > * > * StringUtils.splitByLength(" abcdef", 2, 4, 1) = [" a", "bcde", "f"] > * StringUtils.splitByLength("abcdef ", 2, 4, 1) = ["ab", "cdef", " "] > * StringUtils.splitByLength("abcdefg", 2, 4, 0, 1) = ["ab", "cdef", "", "g"] > * StringUtils.splitByLength("abcdefg", -1) = {@link > IllegalArgumentException} > * > * > * @param str the String to parse, may be null > * @param lengths the string lengths where to cut, may be null, must not be > negative > * @return an array of splitted Strings, {@code null} if null String input > * @throws IllegalArgumentException > * if one of the lengths is negative > */ > public static String[] splitByLength(String str, int ... lengths); > /** > * Split a String into an array, using an array of fixed string lengths > repeated as > * many times as necessary to reach the String end. > * > * If not null String input, the returned array size is a multiple of the > input lengths array. > * > * A null input String returns {@code null}. > * A {@code null} or empty input lengths array returns an empty array. > * A {@code 0} in the input lengths array results in en empty string. > * > * All empty substrings other than zero length requested and following > substrings, > * are returned {@code null}. > * > * > * StringUtils.splitByLengthRepeated(null, *) = null > * StringUtils.splitByLengthRepeated("abc")= [] > * StringUtils.splitByLengthRepeated("abc", null) = [] > * StringUtils.splitByLengthRepeated("abc", [])= [] > * StringUtils.splitByLengthRepeated("", 2, 4, 1) = [null, null, null] > * > * StringUtils.splitByLengthRepeated("abcdefghij", 2, 3) = ["ab", "cde", > "fg", "hij"] > * StringUtils.splitByLengthRepeated("abcdefgh", 2, 3) = ["ab", "cde", > "fg", "h"] > * StringUtils.splitByLengthRepeated("abcdefg", 2, 3)= ["ab", "cde", > "fg", null] > * > * StringUtils.splitByLengthRepeated(" abcdef", 2, 3)= [" a", "bcd", > "ef", null] > * StringUtils.splitByLengthRepeated("abcdef ", 2, 3)= ["ab", "cde", > "f ", null] > * StringUtils.splitByLengthRepeated("abcdef", 2, 3, 0, 1) = ["ab", "cde", > "", "f"] > * StringUtils.splitByLengthRepeated("abcdefg", 2, 3, 0, 1) = ["ab", "cde", > "", "f", > * "g", null, > null, null] > * StringUtils.splitByLengthRepeated("abcdefgh", 2, 0, 1, 0) = ["ab", "", > "c", "", > * "de", "", > "f", "", > * "gh", "", > null, null] > * StringUtils.splitByLengthRepeated("abcdefg", 2, 0, 1, 0) = ["ab", "", "c", > "", > * "de", "", > "f", "", > * "g", null, > null, null] > *
[jira] [Commented] (LANG-1124) Add split by length methods in StringUtils
[ https://issues.apache.org/jira/browse/LANG-1124?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanelfocusedCommentId=14547613#comment-14547613 ] ASF GitHub Bot commented on LANG-1124: -- Github user britter commented on the pull request: https://github.com/apache/commons-lang/pull/75#issuecomment-102940510 @rikles thank you for your thorough feedback. Give me some time to go through your comments. I'll have time to have a look later this week. Add split by length methods in StringUtils -- Key: LANG-1124 URL: https://issues.apache.org/jira/browse/LANG-1124 Project: Commons Lang Issue Type: New Feature Components: lang.* Reporter: Loic Guibert Add methods to split a String by fixed lengths : {code:java} public static String[] splitByLength(String str, int ... lengths); public static String[] splitByLengthRepeatedly(String str, int ... lengths); {code} Detail : {code:java} /** * pSplit a String into an array, using an array of fixed string lengths./p * * pIf not null String input, the returned array size is same as the input lengths array./p * * pA null input String returns {@code null}. * A {@code null} or empty input lengths array returns an empty array. * A {@code 0} in the input lengths array results in en empty string./p * * pExtra characters are ignored (ie String length greater than sum of split lengths). * All empty substrings other than zero length requested, are returned {@code null}./p * * pre * StringUtils.splitByLength(null, *) = null * StringUtils.splitByLength(abc)= [] * StringUtils.splitByLength(abc, null) = [] * StringUtils.splitByLength(abc, [])= [] * StringUtils.splitByLength(, 2, 4, 1) = [null, null, null] * * StringUtils.splitByLength(abcdefg, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefghij, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefg, 2, 4, 5) = [ab, cdef, g] * StringUtils.splitByLength(abcdef, 2, 4, 1) = [ab, cdef, null] * * StringUtils.splitByLength( abcdef, 2, 4, 1) = [ a, bcde, f] * StringUtils.splitByLength(abcdef , 2, 4, 1) = [ab, cdef, ] * StringUtils.splitByLength(abcdefg, 2, 4, 0, 1) = [ab, cdef, , g] * StringUtils.splitByLength(abcdefg, -1) = {@link IllegalArgumentException} * /pre * * @param str the String to parse, may be null * @param lengths the string lengths where to cut, may be null, must not be negative * @return an array of splitted Strings, {@code null} if null String input * @throws IllegalArgumentException * if one of the lengths is negative */ public static String[] splitByLength(String str, int ... lengths); /** * pSplit a String into an array, using an array of fixed string lengths repeated as * many times as necessary to reach the String end./p * * pIf not null String input, the returned array size is a multiple of the input lengths array./p * * pA null input String returns {@code null}. * A {@code null} or empty input lengths array returns an empty array. * A {@code 0} in the input lengths array results in en empty string./p * * pAll empty substrings other than zero length requested and following substrings, * are returned {@code null}./p * * pre * StringUtils.splitByLengthRepeated(null, *) = null * StringUtils.splitByLengthRepeated(abc)= [] * StringUtils.splitByLengthRepeated(abc, null) = [] * StringUtils.splitByLengthRepeated(abc, [])= [] * StringUtils.splitByLengthRepeated(, 2, 4, 1) = [null, null, null] * * StringUtils.splitByLengthRepeated(abcdefghij, 2, 3) = [ab, cde, fg, hij] * StringUtils.splitByLengthRepeated(abcdefgh, 2, 3) = [ab, cde, fg, h] * StringUtils.splitByLengthRepeated(abcdefg, 2, 3)= [ab, cde, fg, null] * * StringUtils.splitByLengthRepeated( abcdef, 2, 3)= [ a, bcd, ef, null] * StringUtils.splitByLengthRepeated(abcdef , 2, 3)= [ab, cde, f , null] * StringUtils.splitByLengthRepeated(abcdef, 2, 3, 0, 1) = [ab, cde, , f] * StringUtils.splitByLengthRepeated(abcdefg, 2, 3, 0, 1) = [ab, cde, , f, * g, null, null, null] * StringUtils.splitByLengthRepeated(abcdefgh, 2, 0, 1, 0) = [ab, , c, , * de, , f, , * gh, , null, null] * StringUtils.splitByLengthRepeated(abcdefg, 2, 0, 1, 0) = [ab, , c, , * de, , f, , * g, null, null, null] * StringUtils.splitByLengthRepeated(abcdefg, -1) = {@link
[jira] [Commented] (LANG-1124) Add split by length methods in StringUtils
[ https://issues.apache.org/jira/browse/LANG-1124?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanelfocusedCommentId=14546754#comment-14546754 ] ASF GitHub Bot commented on LANG-1124: -- Github user rikles commented on a diff in the pull request: https://github.com/apache/commons-lang/pull/75#discussion_r30461235 --- Diff: src/main/java/org/apache/commons/lang3/StringUtils.java --- @@ -3277,6 +3277,164 @@ public static String substringBetween(final String str, final String open, final return list.toArray(new String[list.size()]); } +/** + * pSplit a String into an array, using an array of fixed string lengths./p + * + * pIf not null String input, the returned array size is same as the input lengths array./p + * + * pA null input String returns {@code null}. + * A {@code null} or empty input lengths array returns an empty array. + * A {@code 0} in the input lengths array results in en empty string./p + * + * pExtra characters are ignored (ie String length greater than sum of split lengths). + * All empty substrings other than zero length requested, are returned {@code null}./p + * + * pre + * StringUtils.splitByLength(null, *) = null + * StringUtils.splitByLength(abc)= [] + * StringUtils.splitByLength(abc, null) = [] + * StringUtils.splitByLength(abc, [])= [] + * StringUtils.splitByLength(, 2, 4, 1) = [null, null, null] + * + * StringUtils.splitByLength(abcdefg, 2, 4, 1) = [ab, cdef, g] + * StringUtils.splitByLength(abcdefghij, 2, 4, 1) = [ab, cdef, g] + * StringUtils.splitByLength(abcdefg, 2, 4, 5) = [ab, cdef, g] + * StringUtils.splitByLength(abcdef, 2, 4, 1) = [ab, cdef, null] + * + * StringUtils.splitByLength( abcdef, 2, 4, 1) = [ a, bcde, f] + * StringUtils.splitByLength(abcdef , 2, 4, 1) = [ab, cdef, ] + * StringUtils.splitByLength(abcdefg, 2, 4, 0, 1) = [ab, cdef, , g] + * StringUtils.splitByLength(abcdefg, -1) = {@link IllegalArgumentException} --- End diff -- It's true that negative numbers could be easily promotes to 0. But here, it's not like in `StringUtils.split(String, String, int)` where the int value indicate the maximum number of results we want. It's very common that passing a zero or negative value mean _no limit_. Here, we request several column lengths. I thought it was natural and easier to consider negative lengths as a coding mistake. I even wondered if we could interpret negative values like : * _unlimited length_. But what about `StringUtils.splitByLength(abcdefg, -1, 2, -1, -1)` ... ? * _backward move_ like `StringUtils.splitByLength(abcde, 3, -2, 4) = [abc, bcde]`. But what about `StringUtils.splitByLength(abcdef, 1, -4, 3)` ... ? I don't know what to think about it... Add split by length methods in StringUtils -- Key: LANG-1124 URL: https://issues.apache.org/jira/browse/LANG-1124 Project: Commons Lang Issue Type: New Feature Components: lang.* Reporter: Loic Guibert Add methods to split a String by fixed lengths : {code:java} public static String[] splitByLength(String str, int ... lengths); public static String[] splitByLengthRepeatedly(String str, int ... lengths); {code} Detail : {code:java} /** * pSplit a String into an array, using an array of fixed string lengths./p * * pIf not null String input, the returned array size is same as the input lengths array./p * * pA null input String returns {@code null}. * A {@code null} or empty input lengths array returns an empty array. * A {@code 0} in the input lengths array results in en empty string./p * * pExtra characters are ignored (ie String length greater than sum of split lengths). * All empty substrings other than zero length requested, are returned {@code null}./p * * pre * StringUtils.splitByLength(null, *) = null * StringUtils.splitByLength(abc)= [] * StringUtils.splitByLength(abc, null) = [] * StringUtils.splitByLength(abc, [])= [] * StringUtils.splitByLength(, 2, 4, 1) = [null, null, null] * * StringUtils.splitByLength(abcdefg, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefghij, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefg, 2, 4, 5) = [ab, cdef, g] * StringUtils.splitByLength(abcdef, 2, 4, 1) = [ab, cdef, null] * * StringUtils.splitByLength( abcdef, 2, 4, 1) = [ a, bcde, f] * StringUtils.splitByLength(abcdef , 2, 4, 1) = [ab, cdef, ] * StringUtils.splitByLength(abcdefg, 2, 4, 0, 1) = [ab, cdef, , g] *
[jira] [Commented] (LANG-1124) Add split by length methods in StringUtils
[ https://issues.apache.org/jira/browse/LANG-1124?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanelfocusedCommentId=14546744#comment-14546744 ] ASF GitHub Bot commented on LANG-1124: -- Github user rikles commented on a diff in the pull request: https://github.com/apache/commons-lang/pull/75#discussion_r30461086 --- Diff: src/main/java/org/apache/commons/lang3/StringUtils.java --- @@ -3277,6 +3277,164 @@ public static String substringBetween(final String str, final String open, final return list.toArray(new String[list.size()]); } +/** + * pSplit a String into an array, using an array of fixed string lengths./p + * + * pIf not null String input, the returned array size is same as the input lengths array./p + * + * pA null input String returns {@code null}. + * A {@code null} or empty input lengths array returns an empty array. + * A {@code 0} in the input lengths array results in en empty string./p + * + * pExtra characters are ignored (ie String length greater than sum of split lengths). + * All empty substrings other than zero length requested, are returned {@code null}./p + * + * pre + * StringUtils.splitByLength(null, *) = null + * StringUtils.splitByLength(abc)= [] + * StringUtils.splitByLength(abc, null) = [] + * StringUtils.splitByLength(abc, [])= [] + * StringUtils.splitByLength(, 2, 4, 1) = [null, null, null] + * + * StringUtils.splitByLength(abcdefg, 2, 4, 1) = [ab, cdef, g] + * StringUtils.splitByLength(abcdefghij, 2, 4, 1) = [ab, cdef, g] + * StringUtils.splitByLength(abcdefg, 2, 4, 5) = [ab, cdef, g] + * StringUtils.splitByLength(abcdef, 2, 4, 1) = [ab, cdef, null] --- End diff -- Good point. My idea was to indicate that there is no more characters to extract the explicitly requested column length text. But in the other hand, this can cause `NullPointerException` if the returned array is used without check... Why I used this approach : with `null` values, in case of hard coded lengths, we can simply check the returned array with a _for each_ loop, even later in other piece of code : ```java String[] cols = StringUtils.splitByLength(input, 2, 3, 1); // ... for (String col : cols) { if (col == null) { break; } // Do something } ``` Without `null` values, we must have a lengths array reference : ```java int[] LENGTHS = { 2, 3, 0, 1 }; String[] cols = StringUtils.splitByLength(input, LENGTHS); int index = 0; for (String col : cols) { if (col.length() == 0 LENGTHS[index] 0) { break; } index++; // Do something } ``` Of course, we can also check the input string length before calling `StringUtils.splitByLength`, but we have to get the lengths sum. And what about this case : `StringUtils.splitByLength(abcd, 1, 2, 2)` ? I don't know which is best... What do you think ? Add split by length methods in StringUtils -- Key: LANG-1124 URL: https://issues.apache.org/jira/browse/LANG-1124 Project: Commons Lang Issue Type: New Feature Components: lang.* Reporter: Loic Guibert Add methods to split a String by fixed lengths : {code:java} public static String[] splitByLength(String str, int ... lengths); public static String[] splitByLengthRepeatedly(String str, int ... lengths); {code} Detail : {code:java} /** * pSplit a String into an array, using an array of fixed string lengths./p * * pIf not null String input, the returned array size is same as the input lengths array./p * * pA null input String returns {@code null}. * A {@code null} or empty input lengths array returns an empty array. * A {@code 0} in the input lengths array results in en empty string./p * * pExtra characters are ignored (ie String length greater than sum of split lengths). * All empty substrings other than zero length requested, are returned {@code null}./p * * pre * StringUtils.splitByLength(null, *) = null * StringUtils.splitByLength(abc)= [] * StringUtils.splitByLength(abc, null) = [] * StringUtils.splitByLength(abc, [])= [] * StringUtils.splitByLength(, 2, 4, 1) = [null, null, null] * * StringUtils.splitByLength(abcdefg, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefghij, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefg, 2, 4, 5) = [ab, cdef, g] * StringUtils.splitByLength(abcdef, 2, 4, 1) = [ab, cdef, null] * * StringUtils.splitByLength( abcdef, 2, 4, 1) = [ a,
[jira] [Commented] (LANG-1124) Add split by length methods in StringUtils
[ https://issues.apache.org/jira/browse/LANG-1124?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanelfocusedCommentId=14546732#comment-14546732 ] ASF GitHub Bot commented on LANG-1124: -- Github user rikles commented on a diff in the pull request: https://github.com/apache/commons-lang/pull/75#discussion_r30460736 --- Diff: src/main/java/org/apache/commons/lang3/StringUtils.java --- @@ -3277,6 +3277,164 @@ public static String substringBetween(final String str, final String open, final return list.toArray(new String[list.size()]); } +/** + * pSplit a String into an array, using an array of fixed string lengths./p + * + * pIf not null String input, the returned array size is same as the input lengths array./p + * + * pA null input String returns {@code null}. + * A {@code null} or empty input lengths array returns an empty array. + * A {@code 0} in the input lengths array results in en empty string./p + * + * pExtra characters are ignored (ie String length greater than sum of split lengths). + * All empty substrings other than zero length requested, are returned {@code null}./p + * + * pre + * StringUtils.splitByLength(null, *) = null + * StringUtils.splitByLength(abc)= [] + * StringUtils.splitByLength(abc, null) = [] + * StringUtils.splitByLength(abc, [])= [] + * StringUtils.splitByLength(, 2, 4, 1) = [null, null, null] + * + * StringUtils.splitByLength(abcdefg, 2, 4, 1) = [ab, cdef, g] --- End diff -- Like said in the next line : `StringUtils.splitByLength(abcdefg, 2, 2)` will return `[ab, cd ]`. `StringUtils.splitByLength(abcdefghij, 2, 4, 1) = [ab, cdef, g]` I asked myself the question during development. Do we discard the extra characters ? I think it would be nice to let users decide. Moreover, depending on use case, it could be useful to keep/discard the first extra characters (like parsing a single line commented out string). I propose to : * add a private `splitByLengthWorker(String string, boolean splitFromEnd, boolean discardExtraChar, int ... lengths)` * keep this `splitByLength(String, int ...)` method logic as default : `return splitByLengthWorker(string, false, true, lengths)`. So, by default, the returned array is same size as the `int ... lengths` array param and this behavior is interesting on parsing fixed column lengths strings. * add a `splitByLengthKeepExtraChar(String, int ...)` : `return splitByLengthWorker(string, false, false, lengths)` * add a `splitByLengthFromEnd(String, int ...)` : `return splitByLengthWorker(string, true, false, lengths)` * add a `splitByLengthFromEndKeepExtraChar(String, int ...)` : `return splitByLengthWorker(string, true, true, lengths)` A question : For _split from end_ methods, which call do you think is more logic : _right aligned/end to start_ lengths, _reversed/not reversed_ result ? * `StringUtils.splitByLengthFromEndKeepExtraChar(__abcdef, 1, 2, 3) = [__, a, bc, def]` - (RA, NR) * `StringUtils.splitByLengthFromEndKeepExtraChar(__abcdef, 1, 2, 3) = [def, bc, a, __]` - (RA, R) * `StringUtils.splitByLengthFromEndKeepExtraChar(__abcdef, 1, 2, 3) = [f, de, abc, __]` - (E2S, R) * `StringUtils.splitByLengthFromEndKeepExtraChar(__abcdef, 1, 2, 3) = [__, abc, de, f]` - (E2S, NR) I think the first one is more readable, we can visually understand the splitting, but may be less intuitive : ``` StringUtils.splitByLengthFromEnd(ABCDEFGHIJKLM, 3, 4, 5) = [BCD, EFGH, IJKLM] [3][4_][_5_] ABCDEFGHIJKLM ``` Add split by length methods in StringUtils -- Key: LANG-1124 URL: https://issues.apache.org/jira/browse/LANG-1124 Project: Commons Lang Issue Type: New Feature Components: lang.* Reporter: Loic Guibert Add methods to split a String by fixed lengths : {code:java} public static String[] splitByLength(String str, int ... lengths); public static String[] splitByLengthRepeatedly(String str, int ... lengths); {code} Detail : {code:java} /** * pSplit a String into an array, using an array of fixed string lengths./p * * pIf not null String input, the returned array size is same as the input lengths array./p * * pA null input String returns {@code null}. * A {@code null} or empty input lengths array returns an empty array. * A {@code 0} in the input lengths array results in en empty string./p * * pExtra characters are ignored (ie String length greater than sum of split lengths). * All empty substrings other than zero length requested, are returned {@code null}./p *
[jira] [Commented] (LANG-1124) Add split by length methods in StringUtils
[ https://issues.apache.org/jira/browse/LANG-1124?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanelfocusedCommentId=14547034#comment-14547034 ] ASF GitHub Bot commented on LANG-1124: -- Github user rikles commented on a diff in the pull request: https://github.com/apache/commons-lang/pull/75#discussion_r30467016 --- Diff: src/main/java/org/apache/commons/lang3/StringUtils.java --- @@ -3277,6 +3277,164 @@ public static String substringBetween(final String str, final String open, final return list.toArray(new String[list.size()]); } +/** + * pSplit a String into an array, using an array of fixed string lengths./p + * + * pIf not null String input, the returned array size is same as the input lengths array./p + * + * pA null input String returns {@code null}. + * A {@code null} or empty input lengths array returns an empty array. + * A {@code 0} in the input lengths array results in en empty string./p + * + * pExtra characters are ignored (ie String length greater than sum of split lengths). + * All empty substrings other than zero length requested, are returned {@code null}./p + * + * pre + * StringUtils.splitByLength(null, *) = null + * StringUtils.splitByLength(abc)= [] + * StringUtils.splitByLength(abc, null) = [] + * StringUtils.splitByLength(abc, [])= [] + * StringUtils.splitByLength(, 2, 4, 1) = [null, null, null] + * + * StringUtils.splitByLength(abcdefg, 2, 4, 1) = [ab, cdef, g] + * StringUtils.splitByLength(abcdefghij, 2, 4, 1) = [ab, cdef, g] + * StringUtils.splitByLength(abcdefg, 2, 4, 5) = [ab, cdef, g] + * StringUtils.splitByLength(abcdef, 2, 4, 1) = [ab, cdef, null] + * + * StringUtils.splitByLength( abcdef, 2, 4, 1) = [ a, bcde, f] + * StringUtils.splitByLength(abcdef , 2, 4, 1) = [ab, cdef, ] + * StringUtils.splitByLength(abcdefg, 2, 4, 0, 1) = [ab, cdef, , g] + * StringUtils.splitByLength(abcdefg, -1) = {@link IllegalArgumentException} --- End diff -- Another way to deal with negative values could be to treat them as _discard # characters_ : `StringUtils.splitByLength(abcdef, 2, -3, 1) = [ab, f]` Add split by length methods in StringUtils -- Key: LANG-1124 URL: https://issues.apache.org/jira/browse/LANG-1124 Project: Commons Lang Issue Type: New Feature Components: lang.* Reporter: Loic Guibert Add methods to split a String by fixed lengths : {code:java} public static String[] splitByLength(String str, int ... lengths); public static String[] splitByLengthRepeatedly(String str, int ... lengths); {code} Detail : {code:java} /** * pSplit a String into an array, using an array of fixed string lengths./p * * pIf not null String input, the returned array size is same as the input lengths array./p * * pA null input String returns {@code null}. * A {@code null} or empty input lengths array returns an empty array. * A {@code 0} in the input lengths array results in en empty string./p * * pExtra characters are ignored (ie String length greater than sum of split lengths). * All empty substrings other than zero length requested, are returned {@code null}./p * * pre * StringUtils.splitByLength(null, *) = null * StringUtils.splitByLength(abc)= [] * StringUtils.splitByLength(abc, null) = [] * StringUtils.splitByLength(abc, [])= [] * StringUtils.splitByLength(, 2, 4, 1) = [null, null, null] * * StringUtils.splitByLength(abcdefg, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefghij, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefg, 2, 4, 5) = [ab, cdef, g] * StringUtils.splitByLength(abcdef, 2, 4, 1) = [ab, cdef, null] * * StringUtils.splitByLength( abcdef, 2, 4, 1) = [ a, bcde, f] * StringUtils.splitByLength(abcdef , 2, 4, 1) = [ab, cdef, ] * StringUtils.splitByLength(abcdefg, 2, 4, 0, 1) = [ab, cdef, , g] * StringUtils.splitByLength(abcdefg, -1) = {@link IllegalArgumentException} * /pre * * @param str the String to parse, may be null * @param lengths the string lengths where to cut, may be null, must not be negative * @return an array of splitted Strings, {@code null} if null String input * @throws IllegalArgumentException * if one of the lengths is negative */ public static String[] splitByLength(String str, int ... lengths); /** * pSplit a String into an array, using an array of fixed string lengths repeated as * many times as necessary to reach the String end./p * * pIf not null
[jira] [Commented] (LANG-1124) Add split by length methods in StringUtils
[ https://issues.apache.org/jira/browse/LANG-1124?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanelfocusedCommentId=14546690#comment-14546690 ] ASF GitHub Bot commented on LANG-1124: -- Github user rikles commented on a diff in the pull request: https://github.com/apache/commons-lang/pull/75#discussion_r30460281 --- Diff: src/main/java/org/apache/commons/lang3/StringUtils.java --- @@ -3277,6 +3277,164 @@ public static String substringBetween(final String str, final String open, final return list.toArray(new String[list.size()]); } +/** + * pSplit a String into an array, using an array of fixed string lengths./p + * + * pIf not null String input, the returned array size is same as the input lengths array./p + * + * pA null input String returns {@code null}. + * A {@code null} or empty input lengths array returns an empty array. + * A {@code 0} in the input lengths array results in en empty string./p + * + * pExtra characters are ignored (ie String length greater than sum of split lengths). + * All empty substrings other than zero length requested, are returned {@code null}./p + * + * pre + * StringUtils.splitByLength(null, *) = null + * StringUtils.splitByLength(abc)= [] + * StringUtils.splitByLength(abc, null) = [] + * StringUtils.splitByLength(abc, [])= [] + * StringUtils.splitByLength(, 2, 4, 1) = [null, null, null] --- End diff -- I followed the same logic as in other `StringUtils.split*(...)` methods : return `null` if the input string is `null`. So `StringUtils.splitByLength(null, *)` return `null` Add split by length methods in StringUtils -- Key: LANG-1124 URL: https://issues.apache.org/jira/browse/LANG-1124 Project: Commons Lang Issue Type: New Feature Components: lang.* Reporter: Loic Guibert Add methods to split a String by fixed lengths : {code:java} public static String[] splitByLength(String str, int ... lengths); public static String[] splitByLengthRepeatedly(String str, int ... lengths); {code} Detail : {code:java} /** * pSplit a String into an array, using an array of fixed string lengths./p * * pIf not null String input, the returned array size is same as the input lengths array./p * * pA null input String returns {@code null}. * A {@code null} or empty input lengths array returns an empty array. * A {@code 0} in the input lengths array results in en empty string./p * * pExtra characters are ignored (ie String length greater than sum of split lengths). * All empty substrings other than zero length requested, are returned {@code null}./p * * pre * StringUtils.splitByLength(null, *) = null * StringUtils.splitByLength(abc)= [] * StringUtils.splitByLength(abc, null) = [] * StringUtils.splitByLength(abc, [])= [] * StringUtils.splitByLength(, 2, 4, 1) = [null, null, null] * * StringUtils.splitByLength(abcdefg, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefghij, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefg, 2, 4, 5) = [ab, cdef, g] * StringUtils.splitByLength(abcdef, 2, 4, 1) = [ab, cdef, null] * * StringUtils.splitByLength( abcdef, 2, 4, 1) = [ a, bcde, f] * StringUtils.splitByLength(abcdef , 2, 4, 1) = [ab, cdef, ] * StringUtils.splitByLength(abcdefg, 2, 4, 0, 1) = [ab, cdef, , g] * StringUtils.splitByLength(abcdefg, -1) = {@link IllegalArgumentException} * /pre * * @param str the String to parse, may be null * @param lengths the string lengths where to cut, may be null, must not be negative * @return an array of splitted Strings, {@code null} if null String input * @throws IllegalArgumentException * if one of the lengths is negative */ public static String[] splitByLength(String str, int ... lengths); /** * pSplit a String into an array, using an array of fixed string lengths repeated as * many times as necessary to reach the String end./p * * pIf not null String input, the returned array size is a multiple of the input lengths array./p * * pA null input String returns {@code null}. * A {@code null} or empty input lengths array returns an empty array. * A {@code 0} in the input lengths array results in en empty string./p * * pAll empty substrings other than zero length requested and following substrings, * are returned {@code null}./p * * pre * StringUtils.splitByLengthRepeated(null, *) = null * StringUtils.splitByLengthRepeated(abc)= [] * StringUtils.splitByLengthRepeated(abc, null) = [] * StringUtils.splitByLengthRepeated(abc, [])= []
[jira] [Commented] (LANG-1124) Add split by length methods in StringUtils
[ https://issues.apache.org/jira/browse/LANG-1124?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanelfocusedCommentId=14538225#comment-14538225 ] ASF GitHub Bot commented on LANG-1124: -- Github user britter commented on a diff in the pull request: https://github.com/apache/commons-lang/pull/75#discussion_r30061214 --- Diff: src/main/java/org/apache/commons/lang3/StringUtils.java --- @@ -3277,6 +3277,164 @@ public static String substringBetween(final String str, final String open, final return list.toArray(new String[list.size()]); } +/** + * pSplit a String into an array, using an array of fixed string lengths./p + * + * pIf not null String input, the returned array size is same as the input lengths array./p + * + * pA null input String returns {@code null}. + * A {@code null} or empty input lengths array returns an empty array. + * A {@code 0} in the input lengths array results in en empty string./p + * + * pExtra characters are ignored (ie String length greater than sum of split lengths). + * All empty substrings other than zero length requested, are returned {@code null}./p + * + * pre + * StringUtils.splitByLength(null, *) = null + * StringUtils.splitByLength(abc)= [] + * StringUtils.splitByLength(abc, null) = [] + * StringUtils.splitByLength(abc, [])= [] + * StringUtils.splitByLength(, 2, 4, 1) = [null, null, null] + * + * StringUtils.splitByLength(abcdefg, 2, 4, 1) = [ab, cdef, g] + * StringUtils.splitByLength(abcdefghij, 2, 4, 1) = [ab, cdef, g] + * StringUtils.splitByLength(abcdefg, 2, 4, 5) = [ab, cdef, g] + * StringUtils.splitByLength(abcdef, 2, 4, 1) = [ab, cdef, null] + * + * StringUtils.splitByLength( abcdef, 2, 4, 1) = [ a, bcde, f] + * StringUtils.splitByLength(abcdef , 2, 4, 1) = [ab, cdef, ] + * StringUtils.splitByLength(abcdefg, 2, 4, 0, 1) = [ab, cdef, , g] + * StringUtils.splitByLength(abcdefg, -1) = {@link IllegalArgumentException} --- End diff -- I'm undecided about this. The ```StringUtils.split(String, String, int)``` method will not fail if negative values are provided. But I'm not sure how to behave for an input like ```[3, -3, 3]```. I think we should handle this like the ```ArrayUtils``` class behaves: it promotes negative indicies to 0. So a negative number should add an empty string to the resulting array. Add split by length methods in StringUtils -- Key: LANG-1124 URL: https://issues.apache.org/jira/browse/LANG-1124 Project: Commons Lang Issue Type: New Feature Components: lang.* Reporter: Loic Guibert Add methods to split a String by fixed lengths : {code:java} public static String[] splitByLength(String str, int ... lengths); public static String[] splitByLengthRepeatedly(String str, int ... lengths); {code} Detail : {code:java} /** * pSplit a String into an array, using an array of fixed string lengths./p * * pIf not null String input, the returned array size is same as the input lengths array./p * * pA null input String returns {@code null}. * A {@code null} or empty input lengths array returns an empty array. * A {@code 0} in the input lengths array results in en empty string./p * * pExtra characters are ignored (ie String length greater than sum of split lengths). * All empty substrings other than zero length requested, are returned {@code null}./p * * pre * StringUtils.splitByLength(null, *) = null * StringUtils.splitByLength(abc)= [] * StringUtils.splitByLength(abc, null) = [] * StringUtils.splitByLength(abc, [])= [] * StringUtils.splitByLength(, 2, 4, 1) = [null, null, null] * * StringUtils.splitByLength(abcdefg, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefghij, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefg, 2, 4, 5) = [ab, cdef, g] * StringUtils.splitByLength(abcdef, 2, 4, 1) = [ab, cdef, null] * * StringUtils.splitByLength( abcdef, 2, 4, 1) = [ a, bcde, f] * StringUtils.splitByLength(abcdef , 2, 4, 1) = [ab, cdef, ] * StringUtils.splitByLength(abcdefg, 2, 4, 0, 1) = [ab, cdef, , g] * StringUtils.splitByLength(abcdefg, -1) = {@link IllegalArgumentException} * /pre * * @param str the String to parse, may be null * @param lengths the string lengths where to cut, may be null, must not be negative * @return an array of splitted Strings, {@code null} if null String input * @throws IllegalArgumentException * if one of the lengths is negative */ public
[jira] [Commented] (LANG-1124) Add split by length methods in StringUtils
[ https://issues.apache.org/jira/browse/LANG-1124?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanelfocusedCommentId=14538660#comment-14538660 ] ASF GitHub Bot commented on LANG-1124: -- Github user thiagoh commented on the pull request: https://github.com/apache/commons-lang/pull/75#issuecomment-101048576 is this finished? Add split by length methods in StringUtils -- Key: LANG-1124 URL: https://issues.apache.org/jira/browse/LANG-1124 Project: Commons Lang Issue Type: New Feature Components: lang.* Reporter: Loic Guibert Add methods to split a String by fixed lengths : {code:java} public static String[] splitByLength(String str, int ... lengths); public static String[] splitByLengthRepeatedly(String str, int ... lengths); {code} Detail : {code:java} /** * pSplit a String into an array, using an array of fixed string lengths./p * * pIf not null String input, the returned array size is same as the input lengths array./p * * pA null input String returns {@code null}. * A {@code null} or empty input lengths array returns an empty array. * A {@code 0} in the input lengths array results in en empty string./p * * pExtra characters are ignored (ie String length greater than sum of split lengths). * All empty substrings other than zero length requested, are returned {@code null}./p * * pre * StringUtils.splitByLength(null, *) = null * StringUtils.splitByLength(abc)= [] * StringUtils.splitByLength(abc, null) = [] * StringUtils.splitByLength(abc, [])= [] * StringUtils.splitByLength(, 2, 4, 1) = [null, null, null] * * StringUtils.splitByLength(abcdefg, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefghij, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefg, 2, 4, 5) = [ab, cdef, g] * StringUtils.splitByLength(abcdef, 2, 4, 1) = [ab, cdef, null] * * StringUtils.splitByLength( abcdef, 2, 4, 1) = [ a, bcde, f] * StringUtils.splitByLength(abcdef , 2, 4, 1) = [ab, cdef, ] * StringUtils.splitByLength(abcdefg, 2, 4, 0, 1) = [ab, cdef, , g] * StringUtils.splitByLength(abcdefg, -1) = {@link IllegalArgumentException} * /pre * * @param str the String to parse, may be null * @param lengths the string lengths where to cut, may be null, must not be negative * @return an array of splitted Strings, {@code null} if null String input * @throws IllegalArgumentException * if one of the lengths is negative */ public static String[] splitByLength(String str, int ... lengths); /** * pSplit a String into an array, using an array of fixed string lengths repeated as * many times as necessary to reach the String end./p * * pIf not null String input, the returned array size is a multiple of the input lengths array./p * * pA null input String returns {@code null}. * A {@code null} or empty input lengths array returns an empty array. * A {@code 0} in the input lengths array results in en empty string./p * * pAll empty substrings other than zero length requested and following substrings, * are returned {@code null}./p * * pre * StringUtils.splitByLengthRepeated(null, *) = null * StringUtils.splitByLengthRepeated(abc)= [] * StringUtils.splitByLengthRepeated(abc, null) = [] * StringUtils.splitByLengthRepeated(abc, [])= [] * StringUtils.splitByLengthRepeated(, 2, 4, 1) = [null, null, null] * * StringUtils.splitByLengthRepeated(abcdefghij, 2, 3) = [ab, cde, fg, hij] * StringUtils.splitByLengthRepeated(abcdefgh, 2, 3) = [ab, cde, fg, h] * StringUtils.splitByLengthRepeated(abcdefg, 2, 3)= [ab, cde, fg, null] * * StringUtils.splitByLengthRepeated( abcdef, 2, 3)= [ a, bcd, ef, null] * StringUtils.splitByLengthRepeated(abcdef , 2, 3)= [ab, cde, f , null] * StringUtils.splitByLengthRepeated(abcdef, 2, 3, 0, 1) = [ab, cde, , f] * StringUtils.splitByLengthRepeated(abcdefg, 2, 3, 0, 1) = [ab, cde, , f, * g, null, null, null] * StringUtils.splitByLengthRepeated(abcdefgh, 2, 0, 1, 0) = [ab, , c, , * de, , f, , * gh, , null, null] * StringUtils.splitByLengthRepeated(abcdefg, 2, 0, 1, 0) = [ab, , c, , * de, , f, , * g, null, null, null] * StringUtils.splitByLengthRepeated(abcdefg, -1) = {@link IllegalArgumentException} * StringUtils.splitByLengthRepeated(abcdefg, 0, 0)= {@link IllegalArgumentException} * /pre
[jira] [Commented] (LANG-1124) Add split by length methods in StringUtils
[ https://issues.apache.org/jira/browse/LANG-1124?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanelfocusedCommentId=14539285#comment-14539285 ] ASF GitHub Bot commented on LANG-1124: -- Github user britter commented on the pull request: https://github.com/apache/commons-lang/pull/75#issuecomment-101139996 @thiagoh no, we have to discuss what I've commented. Add split by length methods in StringUtils -- Key: LANG-1124 URL: https://issues.apache.org/jira/browse/LANG-1124 Project: Commons Lang Issue Type: New Feature Components: lang.* Reporter: Loic Guibert Add methods to split a String by fixed lengths : {code:java} public static String[] splitByLength(String str, int ... lengths); public static String[] splitByLengthRepeatedly(String str, int ... lengths); {code} Detail : {code:java} /** * pSplit a String into an array, using an array of fixed string lengths./p * * pIf not null String input, the returned array size is same as the input lengths array./p * * pA null input String returns {@code null}. * A {@code null} or empty input lengths array returns an empty array. * A {@code 0} in the input lengths array results in en empty string./p * * pExtra characters are ignored (ie String length greater than sum of split lengths). * All empty substrings other than zero length requested, are returned {@code null}./p * * pre * StringUtils.splitByLength(null, *) = null * StringUtils.splitByLength(abc)= [] * StringUtils.splitByLength(abc, null) = [] * StringUtils.splitByLength(abc, [])= [] * StringUtils.splitByLength(, 2, 4, 1) = [null, null, null] * * StringUtils.splitByLength(abcdefg, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefghij, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefg, 2, 4, 5) = [ab, cdef, g] * StringUtils.splitByLength(abcdef, 2, 4, 1) = [ab, cdef, null] * * StringUtils.splitByLength( abcdef, 2, 4, 1) = [ a, bcde, f] * StringUtils.splitByLength(abcdef , 2, 4, 1) = [ab, cdef, ] * StringUtils.splitByLength(abcdefg, 2, 4, 0, 1) = [ab, cdef, , g] * StringUtils.splitByLength(abcdefg, -1) = {@link IllegalArgumentException} * /pre * * @param str the String to parse, may be null * @param lengths the string lengths where to cut, may be null, must not be negative * @return an array of splitted Strings, {@code null} if null String input * @throws IllegalArgumentException * if one of the lengths is negative */ public static String[] splitByLength(String str, int ... lengths); /** * pSplit a String into an array, using an array of fixed string lengths repeated as * many times as necessary to reach the String end./p * * pIf not null String input, the returned array size is a multiple of the input lengths array./p * * pA null input String returns {@code null}. * A {@code null} or empty input lengths array returns an empty array. * A {@code 0} in the input lengths array results in en empty string./p * * pAll empty substrings other than zero length requested and following substrings, * are returned {@code null}./p * * pre * StringUtils.splitByLengthRepeated(null, *) = null * StringUtils.splitByLengthRepeated(abc)= [] * StringUtils.splitByLengthRepeated(abc, null) = [] * StringUtils.splitByLengthRepeated(abc, [])= [] * StringUtils.splitByLengthRepeated(, 2, 4, 1) = [null, null, null] * * StringUtils.splitByLengthRepeated(abcdefghij, 2, 3) = [ab, cde, fg, hij] * StringUtils.splitByLengthRepeated(abcdefgh, 2, 3) = [ab, cde, fg, h] * StringUtils.splitByLengthRepeated(abcdefg, 2, 3)= [ab, cde, fg, null] * * StringUtils.splitByLengthRepeated( abcdef, 2, 3)= [ a, bcd, ef, null] * StringUtils.splitByLengthRepeated(abcdef , 2, 3)= [ab, cde, f , null] * StringUtils.splitByLengthRepeated(abcdef, 2, 3, 0, 1) = [ab, cde, , f] * StringUtils.splitByLengthRepeated(abcdefg, 2, 3, 0, 1) = [ab, cde, , f, * g, null, null, null] * StringUtils.splitByLengthRepeated(abcdefgh, 2, 0, 1, 0) = [ab, , c, , * de, , f, , * gh, , null, null] * StringUtils.splitByLengthRepeated(abcdefg, 2, 0, 1, 0) = [ab, , c, , * de, , f, , * g, null, null, null] * StringUtils.splitByLengthRepeated(abcdefg, -1) = {@link IllegalArgumentException} * StringUtils.splitByLengthRepeated(abcdefg, 0, 0)= {@link
[jira] [Commented] (LANG-1124) Add split by length methods in StringUtils
[ https://issues.apache.org/jira/browse/LANG-1124?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanelfocusedCommentId=14538231#comment-14538231 ] ASF GitHub Bot commented on LANG-1124: -- Github user britter commented on a diff in the pull request: https://github.com/apache/commons-lang/pull/75#discussion_r30061452 --- Diff: src/main/java/org/apache/commons/lang3/StringUtils.java --- @@ -3277,6 +3277,164 @@ public static String substringBetween(final String str, final String open, final return list.toArray(new String[list.size()]); } +/** + * pSplit a String into an array, using an array of fixed string lengths./p + * + * pIf not null String input, the returned array size is same as the input lengths array./p + * + * pA null input String returns {@code null}. + * A {@code null} or empty input lengths array returns an empty array. + * A {@code 0} in the input lengths array results in en empty string./p + * + * pExtra characters are ignored (ie String length greater than sum of split lengths). + * All empty substrings other than zero length requested, are returned {@code null}./p + * + * pre + * StringUtils.splitByLength(null, *) = null + * StringUtils.splitByLength(abc)= [] + * StringUtils.splitByLength(abc, null) = [] + * StringUtils.splitByLength(abc, [])= [] + * StringUtils.splitByLength(, 2, 4, 1) = [null, null, null] + * + * StringUtils.splitByLength(abcdefg, 2, 4, 1) = [ab, cdef, g] --- End diff -- What would be the result of ```StringUtils.splitByLength(abcdefg, 2, 2)```? I think it should be [ab, cd efg]. Add split by length methods in StringUtils -- Key: LANG-1124 URL: https://issues.apache.org/jira/browse/LANG-1124 Project: Commons Lang Issue Type: New Feature Components: lang.* Reporter: Loic Guibert Add methods to split a String by fixed lengths : {code:java} public static String[] splitByLength(String str, int ... lengths); public static String[] splitByLengthRepeatedly(String str, int ... lengths); {code} Detail : {code:java} /** * pSplit a String into an array, using an array of fixed string lengths./p * * pIf not null String input, the returned array size is same as the input lengths array./p * * pA null input String returns {@code null}. * A {@code null} or empty input lengths array returns an empty array. * A {@code 0} in the input lengths array results in en empty string./p * * pExtra characters are ignored (ie String length greater than sum of split lengths). * All empty substrings other than zero length requested, are returned {@code null}./p * * pre * StringUtils.splitByLength(null, *) = null * StringUtils.splitByLength(abc)= [] * StringUtils.splitByLength(abc, null) = [] * StringUtils.splitByLength(abc, [])= [] * StringUtils.splitByLength(, 2, 4, 1) = [null, null, null] * * StringUtils.splitByLength(abcdefg, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefghij, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefg, 2, 4, 5) = [ab, cdef, g] * StringUtils.splitByLength(abcdef, 2, 4, 1) = [ab, cdef, null] * * StringUtils.splitByLength( abcdef, 2, 4, 1) = [ a, bcde, f] * StringUtils.splitByLength(abcdef , 2, 4, 1) = [ab, cdef, ] * StringUtils.splitByLength(abcdefg, 2, 4, 0, 1) = [ab, cdef, , g] * StringUtils.splitByLength(abcdefg, -1) = {@link IllegalArgumentException} * /pre * * @param str the String to parse, may be null * @param lengths the string lengths where to cut, may be null, must not be negative * @return an array of splitted Strings, {@code null} if null String input * @throws IllegalArgumentException * if one of the lengths is negative */ public static String[] splitByLength(String str, int ... lengths); /** * pSplit a String into an array, using an array of fixed string lengths repeated as * many times as necessary to reach the String end./p * * pIf not null String input, the returned array size is a multiple of the input lengths array./p * * pA null input String returns {@code null}. * A {@code null} or empty input lengths array returns an empty array. * A {@code 0} in the input lengths array results in en empty string./p * * pAll empty substrings other than zero length requested and following substrings, * are returned {@code null}./p * * pre * StringUtils.splitByLengthRepeated(null, *) = null * StringUtils.splitByLengthRepeated(abc)= [] * StringUtils.splitByLengthRepeated(abc, null) = [] *
[jira] [Commented] (LANG-1124) Add split by length methods in StringUtils
[ https://issues.apache.org/jira/browse/LANG-1124?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanelfocusedCommentId=14538210#comment-14538210 ] ASF GitHub Bot commented on LANG-1124: -- Github user britter commented on a diff in the pull request: https://github.com/apache/commons-lang/pull/75#discussion_r30060493 --- Diff: src/main/java/org/apache/commons/lang3/StringUtils.java --- @@ -3277,6 +3277,164 @@ public static String substringBetween(final String str, final String open, final return list.toArray(new String[list.size()]); } +/** + * pSplit a String into an array, using an array of fixed string lengths./p + * + * pIf not null String input, the returned array size is same as the input lengths array./p + * + * pA null input String returns {@code null}. + * A {@code null} or empty input lengths array returns an empty array. + * A {@code 0} in the input lengths array results in en empty string./p + * + * pExtra characters are ignored (ie String length greater than sum of split lengths). + * All empty substrings other than zero length requested, are returned {@code null}./p + * + * pre + * StringUtils.splitByLength(null, *) = null + * StringUtils.splitByLength(abc)= [] + * StringUtils.splitByLength(abc, null) = [] + * StringUtils.splitByLength(abc, [])= [] + * StringUtils.splitByLength(, 2, 4, 1) = [null, null, null] + * + * StringUtils.splitByLength(abcdefg, 2, 4, 1) = [ab, cdef, g] + * StringUtils.splitByLength(abcdefghij, 2, 4, 1) = [ab, cdef, g] + * StringUtils.splitByLength(abcdefg, 2, 4, 5) = [ab, cdef, g] + * StringUtils.splitByLength(abcdef, 2, 4, 1) = [ab, cdef, null] --- End diff -- I'm not sure whether it is a good idea to add null entries to the result. Add split by length methods in StringUtils -- Key: LANG-1124 URL: https://issues.apache.org/jira/browse/LANG-1124 Project: Commons Lang Issue Type: New Feature Components: lang.* Reporter: Loic Guibert Add methods to split a String by fixed lengths : {code:java} public static String[] splitByLength(String str, int ... lengths); public static String[] splitByLengthRepeatedly(String str, int ... lengths); {code} Detail : {code:java} /** * pSplit a String into an array, using an array of fixed string lengths./p * * pIf not null String input, the returned array size is same as the input lengths array./p * * pA null input String returns {@code null}. * A {@code null} or empty input lengths array returns an empty array. * A {@code 0} in the input lengths array results in en empty string./p * * pExtra characters are ignored (ie String length greater than sum of split lengths). * All empty substrings other than zero length requested, are returned {@code null}./p * * pre * StringUtils.splitByLength(null, *) = null * StringUtils.splitByLength(abc)= [] * StringUtils.splitByLength(abc, null) = [] * StringUtils.splitByLength(abc, [])= [] * StringUtils.splitByLength(, 2, 4, 1) = [null, null, null] * * StringUtils.splitByLength(abcdefg, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefghij, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefg, 2, 4, 5) = [ab, cdef, g] * StringUtils.splitByLength(abcdef, 2, 4, 1) = [ab, cdef, null] * * StringUtils.splitByLength( abcdef, 2, 4, 1) = [ a, bcde, f] * StringUtils.splitByLength(abcdef , 2, 4, 1) = [ab, cdef, ] * StringUtils.splitByLength(abcdefg, 2, 4, 0, 1) = [ab, cdef, , g] * StringUtils.splitByLength(abcdefg, -1) = {@link IllegalArgumentException} * /pre * * @param str the String to parse, may be null * @param lengths the string lengths where to cut, may be null, must not be negative * @return an array of splitted Strings, {@code null} if null String input * @throws IllegalArgumentException * if one of the lengths is negative */ public static String[] splitByLength(String str, int ... lengths); /** * pSplit a String into an array, using an array of fixed string lengths repeated as * many times as necessary to reach the String end./p * * pIf not null String input, the returned array size is a multiple of the input lengths array./p * * pA null input String returns {@code null}. * A {@code null} or empty input lengths array returns an empty array. * A {@code 0} in the input lengths array results in en empty string./p * * pAll empty substrings other than zero length requested and following substrings, * are returned {@code null}./p * * pre *
[jira] [Commented] (LANG-1124) Add split by length methods in StringUtils
[ https://issues.apache.org/jira/browse/LANG-1124?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanelfocusedCommentId=14538208#comment-14538208 ] ASF GitHub Bot commented on LANG-1124: -- Github user britter commented on a diff in the pull request: https://github.com/apache/commons-lang/pull/75#discussion_r30060436 --- Diff: src/main/java/org/apache/commons/lang3/StringUtils.java --- @@ -3277,6 +3277,164 @@ public static String substringBetween(final String str, final String open, final return list.toArray(new String[list.size()]); } +/** + * pSplit a String into an array, using an array of fixed string lengths./p + * + * pIf not null String input, the returned array size is same as the input lengths array./p + * + * pA null input String returns {@code null}. + * A {@code null} or empty input lengths array returns an empty array. + * A {@code 0} in the input lengths array results in en empty string./p + * + * pExtra characters are ignored (ie String length greater than sum of split lengths). + * All empty substrings other than zero length requested, are returned {@code null}./p + * + * pre + * StringUtils.splitByLength(null, *) = null + * StringUtils.splitByLength(abc)= [] + * StringUtils.splitByLength(abc, null) = [] + * StringUtils.splitByLength(abc, [])= [] + * StringUtils.splitByLength(, 2, 4, 1) = [null, null, null] --- End diff -- According to ```StringUtils.split(String, char)```this should better return an empty array. Add split by length methods in StringUtils -- Key: LANG-1124 URL: https://issues.apache.org/jira/browse/LANG-1124 Project: Commons Lang Issue Type: New Feature Components: lang.* Reporter: Loic Guibert Add methods to split a String by fixed lengths : {code:java} public static String[] splitByLength(String str, int ... lengths); public static String[] splitByLengthRepeatedly(String str, int ... lengths); {code} Detail : {code:java} /** * pSplit a String into an array, using an array of fixed string lengths./p * * pIf not null String input, the returned array size is same as the input lengths array./p * * pA null input String returns {@code null}. * A {@code null} or empty input lengths array returns an empty array. * A {@code 0} in the input lengths array results in en empty string./p * * pExtra characters are ignored (ie String length greater than sum of split lengths). * All empty substrings other than zero length requested, are returned {@code null}./p * * pre * StringUtils.splitByLength(null, *) = null * StringUtils.splitByLength(abc)= [] * StringUtils.splitByLength(abc, null) = [] * StringUtils.splitByLength(abc, [])= [] * StringUtils.splitByLength(, 2, 4, 1) = [null, null, null] * * StringUtils.splitByLength(abcdefg, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefghij, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefg, 2, 4, 5) = [ab, cdef, g] * StringUtils.splitByLength(abcdef, 2, 4, 1) = [ab, cdef, null] * * StringUtils.splitByLength( abcdef, 2, 4, 1) = [ a, bcde, f] * StringUtils.splitByLength(abcdef , 2, 4, 1) = [ab, cdef, ] * StringUtils.splitByLength(abcdefg, 2, 4, 0, 1) = [ab, cdef, , g] * StringUtils.splitByLength(abcdefg, -1) = {@link IllegalArgumentException} * /pre * * @param str the String to parse, may be null * @param lengths the string lengths where to cut, may be null, must not be negative * @return an array of splitted Strings, {@code null} if null String input * @throws IllegalArgumentException * if one of the lengths is negative */ public static String[] splitByLength(String str, int ... lengths); /** * pSplit a String into an array, using an array of fixed string lengths repeated as * many times as necessary to reach the String end./p * * pIf not null String input, the returned array size is a multiple of the input lengths array./p * * pA null input String returns {@code null}. * A {@code null} or empty input lengths array returns an empty array. * A {@code 0} in the input lengths array results in en empty string./p * * pAll empty substrings other than zero length requested and following substrings, * are returned {@code null}./p * * pre * StringUtils.splitByLengthRepeated(null, *) = null * StringUtils.splitByLengthRepeated(abc)= [] * StringUtils.splitByLengthRepeated(abc, null) = [] * StringUtils.splitByLengthRepeated(abc, [])= [] * StringUtils.splitByLengthRepeated(, 2, 4, 1) = [null, null, null] * *
[jira] [Commented] (LANG-1124) Add split by length methods in StringUtils
[ https://issues.apache.org/jira/browse/LANG-1124?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanelfocusedCommentId=14521137#comment-14521137 ] ASF GitHub Bot commented on LANG-1124: -- GitHub user rikles opened a pull request: https://github.com/apache/commons-lang/pull/75 LANG-1124: Add StringUtils split by length methods Add methods to split a String by fixed lengths : ```java public static String[] splitByLength(String str, int ... lengths); public static String[] splitByLengthRepeatedly(String str, int ... lengths); ``` You can merge this pull request into a Git repository by running: $ git pull https://github.com/rikles/commons-lang fix-LANG-1124 Alternatively you can review and apply these changes as the patch at: https://github.com/apache/commons-lang/pull/75.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #75 commit 029b1c2f99369f1e2fa1e5230bed9e44be819189 Author: Loic Guibert lfdummy-apa...@yahoo.fr Date: 2015-04-30T08:24:44Z LANG-1124: Add StringUtils split by length methods Add split by length methods in StringUtils -- Key: LANG-1124 URL: https://issues.apache.org/jira/browse/LANG-1124 Project: Commons Lang Issue Type: New Feature Components: lang.* Reporter: Loic Guibert Add methods to split a String by fixed lengths : {code:java} public static String[] splitByLength(String str, int ... lengths); public static String[] splitByLengthRepeatedly(String str, int ... lengths); {code} Detail : {code:java} /** * pSplit a String into an array, using an array of fixed string lengths./p * * pIf not null String input, the returned array size is same as the input lengths array./p * * pA null input String returns {@code null}. * A {@code null} or empty input lengths array returns an empty array. * A {@code 0} in the input lengths array results in en empty string./p * * pExtra characters are ignored (ie String length greater than sum of split lengths). * All empty substrings other than zero length requested, are returned {@code null}./p * * pre * StringUtils.splitByLength(null, *) = null * StringUtils.splitByLength(abc)= [] * StringUtils.splitByLength(abc, null) = [] * StringUtils.splitByLength(abc, [])= [] * StringUtils.splitByLength(, 2, 4, 1) = [null, null, null] * * StringUtils.splitByLength(abcdefg, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefghij, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefg, 2, 4, 5) = [ab, cdef, g] * StringUtils.splitByLength(abcdef, 2, 4, 1) = [ab, cdef, null] * * StringUtils.splitByLength( abcdef, 2, 4, 1) = [ a, bcde, f] * StringUtils.splitByLength(abcdef , 2, 4, 1) = [ab, cdef, ] * StringUtils.splitByLength(abcdefg, 2, 4, 0, 1) = [ab, cdef, , g] * StringUtils.splitByLength(abcdefg, -1) = {@link IllegalArgumentException} * /pre * * @param str the String to parse, may be null * @param lengths the string lengths where to cut, may be null, must not be negative * @return an array of splitted Strings, {@code null} if null String input * @throws IllegalArgumentException * if one of the lengths is negative */ public static String[] splitByLength(String str, int ... lengths); /** * pSplit a String into an array, using an array of fixed string lengths repeated as * many times as necessary to reach the String end./p * * pIf not null String input, the returned array size is a multiple of the input lengths array./p * * pA null input String returns {@code null}. * A {@code null} or empty input lengths array returns an empty array. * A {@code 0} in the input lengths array results in en empty string./p * * pAll empty substrings other than zero length requested and following substrings, * are returned {@code null}./p * * pre * StringUtils.splitByLengthRepeated(null, *) = null * StringUtils.splitByLengthRepeated(abc)= [] * StringUtils.splitByLengthRepeated(abc, null) = [] * StringUtils.splitByLengthRepeated(abc, [])= [] * StringUtils.splitByLengthRepeated(, 2, 4, 1) = [null, null, null] * * StringUtils.splitByLengthRepeated(abcdefghij, 2, 3) = [ab, cde, fg, hij] * StringUtils.splitByLengthRepeated(abcdefgh, 2, 3) = [ab, cde, fg, h] * StringUtils.splitByLengthRepeated(abcdefg, 2, 3)= [ab, cde, fg, null] * * StringUtils.splitByLengthRepeated( abcdef, 2, 3)= [ a, bcd, ef, null] * StringUtils.splitByLengthRepeated(abcdef , 2, 3)= [ab, cde, f , null] * StringUtils.splitByLengthRepeated(abcdef, 2, 3, 0, 1) = [ab, cde, , f] *
[jira] [Commented] (LANG-1124) Add split by length methods in StringUtils
[ https://issues.apache.org/jira/browse/LANG-1124?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanelfocusedCommentId=14521269#comment-14521269 ] ASF GitHub Bot commented on LANG-1124: -- Github user coveralls commented on the pull request: https://github.com/apache/commons-lang/pull/75#issuecomment-97729139 [![Coverage Status](https://coveralls.io/builds/2456530/badge)](https://coveralls.io/builds/2456530) Coverage increased (+0.03%) to 93.36% when pulling **7133787f58254bdb3cb4ff3a37eda0138745aa8d on rikles:fix-LANG-1124** into **f431270c59b6d4558da8dff625a7738591a0d541 on apache:master**. Add split by length methods in StringUtils -- Key: LANG-1124 URL: https://issues.apache.org/jira/browse/LANG-1124 Project: Commons Lang Issue Type: New Feature Components: lang.* Reporter: Loic Guibert Add methods to split a String by fixed lengths : {code:java} public static String[] splitByLength(String str, int ... lengths); public static String[] splitByLengthRepeatedly(String str, int ... lengths); {code} Detail : {code:java} /** * pSplit a String into an array, using an array of fixed string lengths./p * * pIf not null String input, the returned array size is same as the input lengths array./p * * pA null input String returns {@code null}. * A {@code null} or empty input lengths array returns an empty array. * A {@code 0} in the input lengths array results in en empty string./p * * pExtra characters are ignored (ie String length greater than sum of split lengths). * All empty substrings other than zero length requested, are returned {@code null}./p * * pre * StringUtils.splitByLength(null, *) = null * StringUtils.splitByLength(abc)= [] * StringUtils.splitByLength(abc, null) = [] * StringUtils.splitByLength(abc, [])= [] * StringUtils.splitByLength(, 2, 4, 1) = [null, null, null] * * StringUtils.splitByLength(abcdefg, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefghij, 2, 4, 1) = [ab, cdef, g] * StringUtils.splitByLength(abcdefg, 2, 4, 5) = [ab, cdef, g] * StringUtils.splitByLength(abcdef, 2, 4, 1) = [ab, cdef, null] * * StringUtils.splitByLength( abcdef, 2, 4, 1) = [ a, bcde, f] * StringUtils.splitByLength(abcdef , 2, 4, 1) = [ab, cdef, ] * StringUtils.splitByLength(abcdefg, 2, 4, 0, 1) = [ab, cdef, , g] * StringUtils.splitByLength(abcdefg, -1) = {@link IllegalArgumentException} * /pre * * @param str the String to parse, may be null * @param lengths the string lengths where to cut, may be null, must not be negative * @return an array of splitted Strings, {@code null} if null String input * @throws IllegalArgumentException * if one of the lengths is negative */ public static String[] splitByLength(String str, int ... lengths); /** * pSplit a String into an array, using an array of fixed string lengths repeated as * many times as necessary to reach the String end./p * * pIf not null String input, the returned array size is a multiple of the input lengths array./p * * pA null input String returns {@code null}. * A {@code null} or empty input lengths array returns an empty array. * A {@code 0} in the input lengths array results in en empty string./p * * pAll empty substrings other than zero length requested and following substrings, * are returned {@code null}./p * * pre * StringUtils.splitByLengthRepeated(null, *) = null * StringUtils.splitByLengthRepeated(abc)= [] * StringUtils.splitByLengthRepeated(abc, null) = [] * StringUtils.splitByLengthRepeated(abc, [])= [] * StringUtils.splitByLengthRepeated(, 2, 4, 1) = [null, null, null] * * StringUtils.splitByLengthRepeated(abcdefghij, 2, 3) = [ab, cde, fg, hij] * StringUtils.splitByLengthRepeated(abcdefgh, 2, 3) = [ab, cde, fg, h] * StringUtils.splitByLengthRepeated(abcdefg, 2, 3)= [ab, cde, fg, null] * * StringUtils.splitByLengthRepeated( abcdef, 2, 3)= [ a, bcd, ef, null] * StringUtils.splitByLengthRepeated(abcdef , 2, 3)= [ab, cde, f , null] * StringUtils.splitByLengthRepeated(abcdef, 2, 3, 0, 1) = [ab, cde, , f] * StringUtils.splitByLengthRepeated(abcdefg, 2, 3, 0, 1) = [ab, cde, , f, * g, null, null, null] * StringUtils.splitByLengthRepeated(abcdefgh, 2, 0, 1, 0) = [ab, , c, , * de, , f, , * gh, , null, null] * StringUtils.splitByLengthRepeated(abcdefg, 2, 0, 1, 0) = [ab, , c, , * de, , f, ,