Question

I have a string which looks like:

file1.txt:1,2,3|file2.txt:4,5,6,12,13,14|file3:7,| .........

A unit of information in separated by | and it should remain intact. Suppose that the String above is 2100 chars long. I want to split the String on | in such a that there are max of 1024 char in a partition. The last characters of 1st partition should looks like:

 ............ |file103:223

I am using Apache Commons StringUtils.split() but unable to come up with an optimal solution. Any help is highly appreciated.

Was it helpful?

Solution

A splitting function probably isn't going to care about your 1024 character limit. Two options off the top of my head are

  1. startIndex = 0
  2. endIndex = 1024
  3. endIndex = string.lastIndexOf('|', endIndex)
  4. partitionList.add(string.subString(startIndex, endIndex))
  5. startIndex = endIndex
  6. endIndex += 1024
  7. goto 3 :)

Or something like that, but check it for off-by-one errors. Alternatively, do something simpler like

  1. split the string on '|'
  2. concatenate pieces not exceeding 1024 in length, by counting the size of each piece
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top