Dart – Splitting of String

In Dart splitting of a string can be done with the help split string function in the dart. It is a built-in function use to split the string into substring across a common character.

Syntax:

string_name.split(pattern)

This function splits the string into substring across the given pattern and then store them to a list.

Example 1: Splitting a string across space.

Dart



filter_none

edit
close

play_arrow

link
brightness_4
code

// Main function
void main() {
    
  // Creating a string
  String gfg = "Geeks For Geeks !!";
    
  // Splitting the string
  // across spaces
  print(gfg.split(" "));
}

chevron_right


 

Output:

[Geeks, For, Geeks, !!]

 

Example 2: Splitting each character of the string.

 

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

// Main function
void main() {
    
  // Creating a string
  String gfg = "GeeksForGeeks";
    
  // Splitting each
  // character of the string
  print(gfg.split(""));
}

chevron_right


 
 

Output:

[G, e, e, k, s, F, o, r, G, e, e, k, s]

Apart from the above pattern, the pattern can also be a regex. It is useful when we have to split across a group of characters like numbers, special characters, etc.

 

Example 3: Splitting a string across any number present in it. (Using regex)

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

// Main function
void main() {
  // Creating a string
  String gfg = "Geeks1For2Geeks3is4the5best6computer7science8website.";
    
  // Splitting each character
  // of the string
  print(gfg.split(new RegExp(r"[0-9]")));
}

chevron_right


 

Output:

[Geeks, For, Geeks, is, the, best, computer, science, website.]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.