Reverse a String in Dart
In this article, we will look at string reversal in Googleâs Dart language.
How to Implement the Solution
Below is the completed Dart solution:
1 2 3 4 5 6 | 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:
1 | dart reverse-string.dart "Hello World" |
âĶwould fill the args list as [Hello World].
1 | 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:
1 | 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:
1 2 3 4 5 6 7 | void main(List<String> args) { args = ['Hello World']; print( reverse(args[0]) );}String reverse(input) { return input.split('').reversed.join();} |
References
- P. Ryon (slashdoom), âReverse a String in Dart,â The Renegade Coder, 16-Oct-2019. [Online]. Available: https://therenegadecoder.com/code/reverse-a-string-in-dart/. [Accessed: 16-Oct-2019]. âĐ
sample-programs is maintained by TheRenegadeCoder.
Published on Java Code Geeks with permission by Jeremy Grifski, partner at our JCG program. See the original article here: Reverse a String in Dart Opinions expressed by Java Code Geeks contributors are their own. |


