Flutter: trim string after certain NUMBER of characters in dart

Viewed 7976

Say I have a string with n number of characters, but I want to trim it down to only 10 characters. (Given that at all times the string has greater that 10 characters) I don't know the contents of the string.

How to trim it in such a way?

I know how to trim it after a CERTAIN character

String s = "one.two";

//Removes everything after first '.'
String result = s.substring(0, s.indexOf('.'));
print(result);

But how to remove it after a CERTAIN NUMBER of characters?

6 Answers

All answers (using substring) get the first 10 UTF-16 code units, which is different than the first 10 characters because some characters consist of two code units. It is better to use the characters package:

import 'package:characters/characters.dart';

void main() {
  final str = "Hello  World";

  print(str.substring(0, 9)); // BAD
  print(str.characters.take(9)); // GOOD
}

prints

➜ dart main.dart
Hello  
Hello  W

With substring you might even get half a character (which isn't valid):

print(str.substring(0, 7)); // BAD
print(str.characters.take(7)); // GOOD

prints:

Hello �
Hello 

The above examples will fail if string's length is less than the trimmed length. The below code will work with both short and long strings:

import 'dart:math';

void main() {
  String s1 = 'abcdefghijklmnop';
  String s2 = 'abcdef';


  var trimmed = s1.substring(0, min(s1.length,10));
  print(trimmed);
  
  trimmed = s2.substring(0, min(s2.length,10));
  print(trimmed);
}

NOTE: Dart string routines operate on UTF-16 code units. For most of Latin and Cyrylic languages that is not a problem since all characters will fit into a single code unit. Yet emojis, some Asian, African and Middle-east languages might need 2 code units to encode a single character. E.g. ''.length will return 2 although it is a single character string. See characters package.

I think this should work.

String result = s.substring(0, 10);

To trim a String to a certain number of characters. The. code below works perfectly well:

 // initialise your string here
  String s = 'one.two.three.four.five';

  // trim the string by getting the first 10 characters
  String trimmedString = s.substring(0, 10);

  // print the first ten characters of the string
  print(trimmedString);

Output:

one.two.th

i hope this helps

You can do this in multiple ways.

  1. 'string'.substr(start, ?length) USE :- 'one.two.three.four.five'.substr(0, 10)
  2. 'string'.substring(start, ?end) USE :- 'one.two.three.four.five'.substring(0, 10)
  3. 'string'.slice(start, ?end) USE :- 'one.two.three.four.five'.slice(0, 10)

To trim all trailing/right characters by specified characters, use the method:

  static String trimLastCharacter(String srcStr, String pattern) {   
    if (srcStr.length > 0) {   
      if (srcStr.endsWith(pattern)) { 
        final v = srcStr.substring(0, srcStr.length - 1 - pattern.length);
        return trimLastCharacter(v, pattern);
      } 
      return srcStr; 
    }   
    return srcStr;
  }

For example, you want to remove all 0 behind the decimals

$123.98760000

then, call it by

trimLastCharacter("$123.98760000", "0")

output:

$123.9876
Related