Dart String Operations

DailyFluttter.Monster
3 min readAug 1, 2020

--

Inro

In this short post, I will be going over all the Dart operations that can be applied to a String object. API reference

codeUnitAt(int index)

This first method returns the 16-bit UTF-16 code unit at the given index ofthe string.

In the example code, your index is 6, which maps to letter i. The method then returns the UTF-16 code for ‘i’ -> 116

compareTo(String other)

This method compares two string and returns an int that can be 1,-1, or 0.

Note: the method is case sensitive

contains (Pattern other,[int startIndex = 0])

Returns a bool that is true if the String contains the given pattern. If a startIndex is provided then the pattern will only be checked after that index.

endsWith(String other)

Checks if a string ends with a given other string and returns a bool with the value true if it does and value false if it does not

indexOf(Pattern pattern, [int start])

Tries to find the given pattern and return its index in the String. Can add a start int so that only positions after the start are checked

padLeft(int width, [String padding = ‘ ‘]) & padRight(int width, [String padding = ‘ ‘])

These methods add padding to the string. It has two parameters: the width is the number of characters you want the string to have after the padding and the padding is the pattern that will be added to the left/right of the string to achieve the padded effect

replaceAll(Pattern from, String replacement)

This method replaces all the instances of a given pattern in a given string with
the replacement specified.

replaceAllMapped(Pattern from,String replace(Match match))

This method will replace a given string and is dependent on the content match. So unlike replaceAll it will give different replacements for according to the content.

In the example, the codify() method uses replaceAllMapped to add an x in between where the first vowel of each word is

replaceFirst(Pattern from, String replacement) & replaceFirstMapped(Pattern from,String replace(Match match))

These methods are similar to the previous two but they operate only on the first encountered case

replaceRange(int start,int end,String replacement)

This method is to replace a portion of a string specified with a start and end index with a given replacement

split(Pattern pattern)

This method returns a list of strings resulting from splitting the given string in the specified pattern

splitMapJoin(Pattern pattern,{String onMatch(Match match),String onNonMatch(String nonMatch)})

This method split this string similarly to the split() method, it then checks if the list element matches or not and return the replacement for when it does match and a replacement for when it doesn’t.

startsWith(Pattern pattern, [int index = 0])

Return a bool that evaluates if a given string starts with the specified pattern. You can add an index that specifies when to start looking for the match

substring(int startIndex, [int endIndex])

Return a string is contained within the given string and bounded by the startIndex and the endIndex

toLowerCase() & toUpperCase()

Simple enough, returns the string with all characters upper/lower case

trim() & trimRight() & trimLeft()

These methods remove the empty spaces surrounding the string on all, right, left sides respectively

Conclusion

I hope you enjoyed learning a bit about what we can do with strings in dart :)

Originally published at https://www.dailyflutter.monster.

--

--

DailyFluttter.Monster

I try to make cool things with flutter, hope you can enjoy following along. Feel free to contact me with ideas :)