Reverse a String in Dart

Reverse a String in Dart Featured Image

Welcome back to yet another edition of Reverse a String in Every Language—Jeremy here! This time around, we have a guest post from Slashdoom who has been adding quite a few Dart snippets to our Sample Programs repo. As a result, I figured I’d feature one of their articles on the blog.

How to Implement the Solution

Below is the completed Dart solution:

void main(List<String> args) {
    print( reverse(args[0]) );
}
String reverse(input) {
    return input.split('').reversed.join();
}

Much like C or Java, Dart uses the function name main as an entry point for the program. In this case, we don’t need main to return any data so the type void is used. Within the main function’s parameters, you’ll see List<String> args which will define args as a list of strings and fill this list with input from the command line in the form of arguments which in this case are simply strings separated by spaces.

For example:

dart reverse-string.dart "Hello World"

…would fill the args list as [Hello World].

dart reverse-string.dart "Hello" "World"

…would fill the args list as [Hello, World].

It should be noted that in this example program only the first argument passed to the program will be processed because we’re only passing the first string in the list to our reverse function’s input by adding the index position [0] to args in the print( reverse(args[0]) ); line. So, in the second example above, only Hello would get reversed.

So let’s look at that reverse function. We’ve defined the function with type String as it’ll return our reversed string to be printed to the console. It takes the parameter input as an undefined variable but we’re assuming here that it will be a string value.

The Dart:core library String class contains the split method which will split a string or list of strings into a list of substrings based on the pattern given to it. If the pattern is empty as in our reverse function, split will break up the input into single-code unit strings (individual characters). So ‘Hello’ with the empty pattern '' returns [H, e, l, l, o].

After we’ve broken the string into characters with split, we’ll use the reversed property from the Dart:core library’s List class. This property simply takes a list and returns it in reversed order as an Iterable object. So our example list, [H, e, l, l, o], becomes (o, l, l, e, H).

For the purposes of this sample program, there is very little difference between List and Iterable objects. Both are in fact iterable. At a high level, lists will have additional functionality such as indexed read/write access to its elements and sorting functions. Iterables on the other hand are typically created once then accessed as readonly data during an iteration operation like a for-loop.

Both Iterables and Lists have the join method which will take their elements and concatenate them into a string. We use join here on the reversed Iterable which is returned as the final string value of our reverse function. This value is then printed out to the console from print function within main.

How to Run Solution

To run the Dart string reversal program, download the dart file from GitHub, install the Dart SDK as described at dart.dev, and run the following from the command line:

dart reverse-string.dart "Hello, World!"

Alternatively, you can copy the source code into DartPad, an online Dart interpreter. Just keep in mind that you won’t have access to the command line arguments input using this method so you’ll have to populate the args variable in the code instead. For example:

void main(List<String> args) {
    args = ['Hello World'];
    print( reverse(args[0]) );
}
String reverse(input) {
    return input.split('').reversed.join();

Sample Programs in Every Language

Jeremy here again! At this point, that’s all we have to share on how to reverse a string in Dart. If you want to learn more about Dart, head on over to the Sample Programs repo and check out the snippets. Alternatively, we have a page dedicated to the language.

In the meantime, we’d appreciate it if you gave the article a share. If you’d like articles like this to hit your inbox, hop on the mailing list. Finally, you can show your support for this website and all its associated projects by becoming a patron.

At any rate, that’s all we have today. If you’d like to be featured like this, head on over to the repo and share an article!

Series Navigation← Reverse a String in JavaFizz Buzz in Every Language →

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.