How to reverse a string In Dart

In Dart programming, reversing a string involves changing the order of characters within the string so that the last character becomes the first, the second-to-last character becomes the second, and so on. This process flips the string horizontally. There are several approaches we can use to reverse a string in Dart.

  1. Using built-in Functions

  2. Using a loop

  3. Using recursion

Now we will discuss each one in detail.

Using built-in functions

Dart provides a built-in method to reverse a string using the split() and join() functions. Here's how you can do it:

void main() {
String originalString = "Hello, World!";
// Split the string into individual characters
List<String> characters = originalString.split('');
// Reverse the order and join the characters
String reversedString = characters.reversed.join();
print(reversedString); // Output: !dlroW ,olleH
}

In this approach, we split the original string into individual characters using the split() function and then reverse the order of these characters using the reversed property. Finally, we use the join() function to convert the reversed list of characters back into a string.

Using a loop

Another approach to reversing a string is by using a loop to iterate through the characters in reverse order and constructing the reversed string:

String reverseString(String input) {
String reversed = '';
for (int i = input.length - 1; i >= 0; i--) {
reversed += input[i];
}
return reversed;
}
void main() {
String originalString = "Hello, World!";
String reversedString = reverseString(originalString);
print(reversedString); // Output: !dlroW ,olleH
}

In this approach, we define a function the reverseString() that takes an input string and iterates through its characters in reverse order, appending each character to the reversed string.

Using recursion

Recursion is another technique that can be used to reverse a string. Here's an example of how to implement it:

String reverseString(String input) {
if (input.isEmpty) {
return input;
}
return reverseString(input.substring(1)) + input[0];
}
void main() {
String originalString = "Hello, World!";
String reversedString = reverseString(originalString);
print(reversedString); // Output: !dlroW ,olleH
}

In this recursive approach, the reverseString() function calls itself with a substring of the input string, effectively reversing the order of characters.

Complexity

Reversing a string in Dart can be accomplished using various techniques, each with time and space complexities.

Built-in functions

Using loop

Using recursive

Time complexity

O(n)

O(n)

O(n)

Space complexity

O(n)

O(n)

O(n)

Conclusion

The choice of method depends on factors such as the size of the string, performance requirements, and memory considerations. Any method is acceptable for short strings, but for longer strings, the built-in function approach might be more efficient due to its optimized operations. When memory usage is a concern, the loop approach could be preferred over recursion due to its lower space complexity.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved