sebdoy
sebdoy

Reputation: 149

Slicing characters from string (flutter)

When a number is longer than 4 or more characters, I want to replace the last number(s) with 'k'. The final value should also be converted to a string. TIA!

Upvotes: 0

Views: 262

Answers (2)

Moaz El-sawaf
Moaz El-sawaf

Reputation: 3039

You can do this using the Right Pad concept with padRight method in the String class:

Example:

String formatNumber(int number) {
  final numberAsString = number.toString();
  return numberAsString.substring(0, 4).padRight(numberAsString.length, 'k');
}

And use it like the following, let's try the value 123456789:

void main() {
  final number = 123456789;
  print(formatNumber(number));
}

Output:

1234kkkkk

Upvotes: 0

Alex Shinkevich
Alex Shinkevich

Reputation: 357

final value = 54325342534.toString();
  print(value.replaceRange(3, value.length, 'k' * (value.length - 3)));

Upvotes: 3

Related Questions