Hi Guy’s Welcome to Proto Coders Point.
If you are unaware of what is consecutive white spaces in a string, then let me tell. This consecutive white spaces are basically a multiple spaces that are in a given sentences for example: “Hi Bro. Welcome to ProtoCodersPoint.com” here you can see that in a sentence of string there are while spaces. But this won’t look good and also occupy blank space.
In this Flutter Dart Article let’s walk through the way by which we can remove consecutive white space and keep only single white space.
1. Using dart regular Expression (i.e. RegExp)
In Below Example we will make use of (RegExp) method in dart, Here we will pass a regular expression by which it will match all the whitespace characters in the string and then replace them with the single space. This can be implemented by using the replaceAll() method of the string class in dart.
Below are the steps to be followed
1. Firstly match one or more while space characters in a string by creating a RegExp object with the pattern r"\s+"
.
2. Call the replaceAll method on the string and pass the RegExp object and a single space as arguments.
3. Return or print the resulting removed white space from the string.
Code Example below.
// main.dart import 'package:flutter/foundation.dart' show debugPrint; // Define a function that removes multiple spaces from a string String removeMultipleSpaces(String input) { // Create a RegExp object with the pattern r"\s+" RegExp whitespaceRE = RegExp(r"\s+"); // Replace all the white space characters with a single space String output = input.replaceAll(whitespaceRE, " "); // Return the resulting string return output; } void main() { String input = "Hi buddy. Welcome to Proto Coders Point."; String output = removeMultipleSpaces(input); debugPrint(output); }
2. Using split() and join()
An another way to remove white space from a string is by making use of split()
and join()
method of the string class. split() method will split the given input string into a list of substrings based on the white space characters and then use the join() method to join them back into the single string with a single space as the separator.
Example program
// main.dart import 'package:flutter/foundation.dart' show debugPrint; // Define a function that removes multiple spaces from a string String removeMultipleSpaces(String input) { // Split the input string into a list of substrings List<String> substrings = input.split(" "); // Remove empty substrings from the list substrings = substrings.where((substring) => substring.isNotEmpty).toList(); // Join the substrings back into a single string with a single space as the separator String output = substrings.join(" "); // Return the resulting string return output; } void main() { String input = "ProtoCodersPoint is a website to get solution to Programming methods."; String output = removeMultipleSpaces(input); debugPrint(output); }
3. Using StringBuffer and for loop
Here is an another way to remove white spaces from a given string, and that is by making use of Dart StringBuffer() object and then loop it using simple for loop, While Looping each character from the string will check if the character if white space, if not then append the character to the StringBuffer() object and then after the loop is completed finally step is to convert the buffer object to a string by using .toString() method.
Example program:
// main.dart import 'package:flutter/foundation.dart' show debugPrint; // Define a function that removes multiple spaces from a string String removeMultipleSpaces(String input) { // Create a StringBuffer object with an empty initial content StringBuffer buffer = StringBuffer(); // Create a boolean variable to keep track of whether the previous character was a white space character or not bool prevIsSpace = false; // Use a for loop to iterate over each character in the input string for (int i = 0; i < input.length; i++) { // Get the current character String char = input[i]; // If the current character is not a white space character if (char != " ") { // Append it to the buffer and set the boolean variable to false buffer.write(char); prevIsSpace = false; } // If the current character is a white space character and the boolean variable is false else if (!prevIsSpace) { // Append it to the buffer and set the boolean variable to true buffer.write(char); prevIsSpace = true; } // If the current character is a white space character and the boolean variable is true, do nothing } // Convert the buffer into a string String output = buffer.toString(); // Return the resulting string return output; } void main() { String input = "ProtoCodersPoint is a website to get solution to Programming methods. "; String output = removeMultipleSpaces(input); debugPrint(output); }
Conclusion
In this Article, We learnt how to remove consecutive white spaces from a given sentence(string) by 3 different way as stated above.