wrapString method

String wrapString(
  1. int afterWords
)

afterWords will add new line after the selected word Example 'Hi, my name is'.wrapString(2)

will print: Hi, my name is

Implementation

String wrapString(int afterWords) {
  final wordsArr = this?.split(' ') ?? [];

  if (wordsArr.length > 2) {
    final int middle = (this?.indexOf(wordsArr[afterWords]) ?? 0) - 1;
    final prefix = this?.substring(0, middle);
    final postfix = this?.substring(middle + 1);
    return '$prefix\n$postfix';
  }

  return this ?? '';
}