splitWithCount method
- Info: Split long text to pieces controlled with chunk length.
- Params:
chunkLength
is chunk length. Min value is 1.
- Returns: List
- Notes:
Implementation
List<String> splitWithCount(int chunkLength) {
if (isEmpty) {
throw Exception("String is empty.");
} // Empty.
if (chunkLength < 1) {
throw Exception("Chunk length is smaller than 1.");
} // Smaller than 1.
List<String> exportList = <String>[]; // return list.
for (int i = 0; i < length; i = i + chunkLength) {
if (chunkLength + i > length) {
// If chunk length is bigger than text length.
chunkLength = length - i; // Swap
}
String piece; // Print piece.
try // Length is bigger than chunk
{
piece = substring(i, chunkLength + i); // Creating piece.
} catch (e) // Length is smaller than chunk
{
piece = "null"; // If error, return 'null'.
}
exportList.add(piece); // Add to export list.
}
return exportList; // Return export list.
}