by @kodeazy

Flutter How to remove whitespaces from string without using any predefined function?

Home » flutter » Flutter How to remove whitespaces from string without using any predefined function?
  • In this blog we will be discussing on how to remove white spaces from string in flutter.
  • I had a string as below

    String str = "Dart remove empty space ";
  • To remove empty space from all occurences of the string folow below steps.
  • loop through the characters of the string and check for the white space.
  • If found white space at particular index replace the character as below syntax.

    for (int i = 0; i < str.length; i++) {
    if (!str[i].contains(' ')) {
      stringAfterRemovingWhiteSpace = stringAfterRemovingWhiteSpace + "" + str[i];
    }
    }
  • Below is the sample example on how to remove white space

    import 'package:flutter/foundation.dart';
    import 'package:flutter/material.dart';
    void main() {
    String str = "Dart remove empty space ";
    String stringAfterRemovingWhiteSpace = '';
    for (int i = 0; i < str.length; i++) {
    if (!str[i].contains(' ')) {
      stringAfterRemovingWhiteSpace = stringAfterRemovingWhiteSpace + "" + str[i];
    }
    }
    print(stringAfterRemovingWhiteSpace);
    }

    Output

    Dartremoveemptyspace