Inbuilt methods of dart string

Published by admin on

Dart inbuilt methods of dart string

Introduction :

String in dart is a sequence of UTF-16 characters. The String class comes with a lot of different methods. In this post, we are listing these methods below with a short description for each.

 

String methods :

toLowerCase() → String :

It converts all characters of the string to lowercase. If the string already has all lowercase characters, it returns the same string.

toUpperCase() → String :

Same as above. The only difference is that it converts all characters to uppercase. If all characters are already uppercase, it returns the same string.

contains(Pattern p, [ int startIndex = 0 ]) → bool :

Returns a boolean value true or false based on the string contains a substring or not.

 

Replace operations :

replaceAll(Pattern s1, String s2) → String :

Replaces all substring that matches s1 with s2.

replaceAllMapped(Pattern s1, String s2(Match match)) → String :

It uses one Match to compute a string that will replace all strings matched with s1.

replaceFirst(Pattern s1, String s2, [ int startIndex = 0 ]) → String :

It replaces the first matched string with s1. That string is replaced by s2. Optionally, we can also provide one start index to search in the string.

replaceFirstMapped(Pattern s1, String s2(Match match), [ int startIndex = 0 ]) → String :

This method is also used to replace the first occurrence of a substring in a string. It uses Match to produce the new string. Optionally, we can define the start index to search in the string.

replaceRange(int startIndex, int endIndex, String s) → String :

This method is used to replace a specific part of a string defined by the startIndex and endIndex.

 

Trim operations :

trim() → String :

It removes the leading and trailing whitespace of a string and returns one new string.

trimLeft() → String :

It removes the leading whitespace characters of a string and returns one new string.

trimRight() → String :

It removes the trailing whitespace characters of a string and returns one new string.

 

Padding operations :

padLeft(int width, [ String s = ‘ ‘ ]) → String :

Here, width is the required width of the string. If it is shorter than this width, pad this with the optional string s. Returns the new string.

padRight(int width, [ String s = ‘ ‘ ]) → String :

Similar to the above one. The only difference is that it pads to the right.

 

Index operations :

indexOf(Pattern p, [ int startIndex ]) → int :

It returns the index of a pattern p in a string. It checks for the pattern in the string starting from startIndex, which is optional.

lastIndexOf(Pattern p, [ int startIndex ]) → int :

This is similar to the above. The only difference is that it returns the last index.

 

Split operations :

split(Pattern p) → List :

Splits the string using a pattern and returns one list of strings.

splitMapJoin(Pattern p, { String onMatch(Match m), String onNonMatch(String s) }) → String :

This method splits the string, converts its split parts and create one new string by combining them. It returns that string.

 

Starts, Ends and substring :

startsWith(Pattern p, [ int i = 0 ]) → bool :

Returns a boolean if the string starts with a pattern p. Optionally, we can also send one index as the starting point for the search.

endsWith(String s) → bool :

Returns one boolean value if the string ends with a substring s.

substring(int i, [ int j ]) → String :

Returns one substring starts from start index i and ends at end index j. The end index is optional.

 

Match check :

allMatches(String s, [ int i = 0 ]) → Iterable :

Match a pattern against a string repeatedly.

matchAsPrefix(String s, [ int start = 0 ]) → Match :

Match a pattern against the start of a string.

 

Other :

compareTo(String s) → int :

Compares a string with another string. It returns one integer value.

codeUnitAt(int i) → int :

Find the code unit at any index position i.

toString() → String :

Get the string representation value of this object.

noSuchMethod(Invocation invocation) → dynamic :

Invokes if we try to access a non-existing method or non-existing property.

Categories: dart

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *