commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nick Manley (JIRA)" <>
Subject [jira] [Commented] (LANG-1237) String Split For Fixed-Length Strings
Date Tue, 24 May 2016 22:39:12 GMT


Nick Manley commented on LANG-1237:

I'm not seeing this saving a lot of effort over using the substring method. There are three
issues with it. The first is that if you are splitting a long string, then the arguments on
the splitOnBoundary method can get a bit long. I wouldn't like seeing a method with a dozen
numbers as the arguments. The second issue is what happens when you exceed the length of the
String? Do you really want to throw an OutOfBoundsException and lose everything that you did
read up to that point? I would think you would want to read as much as you could and only
error on the last field that exceeds the String length. The third issue relates to what Sebb
said about the 0-based index. Often times when working with fixed length formats, I know the
length of each field and don't want to have to calculate it's offset from zero. This is especially
true if I want to modify the order of fields, have optional fields, have different record
types, etc. The suggested splitOnBoundary method is not very flexible for that.

I have a project at work that reads from a file where the lines are fixed width. I created
a class called FixedWidthLineReader with methods like nextInt, nextString, nextDate, etc that
each takes the length of the next expected field and can convert it to the appropriate type.
I'm not sure if something like that is appropriate for Apache Commons Lang, but I happen to
like it and it keeps things pretty readable.

> String Split For Fixed-Length Strings
> -------------------------------------
>                 Key: LANG-1237
>                 URL:
>             Project: Commons Lang
>          Issue Type: New Feature
>          Components: lang.text.*
>            Reporter: BELUGA BEHR
> Create a StringSplit method that splits on defined boundaries.
> {code:java}
> /**
>  * Used for fixed-length string formats
>  * splitOnBoundary("HelloBigWorld", 4);
>  * Returns ["Hello","BigWorld"]
>  *
>  * splitOnBoundary("HelloBigWorld", 4, 7);
>  * Returns ["Hello","Big","World"]
>  */
> String[] splitOnBoundary(String str, int... boundary);
> {code}

This message was sent by Atlassian JIRA

View raw message