Dart Splitting of String (original) (raw)

Last Updated : 07 Apr, 2025

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 substrings across the given pattern and then stores them in a **list.

split

Splitting a string across space

**Example:

Dart `

// Main function void main() { // Creating a string String gfg = "Geeks For Geeks !!";

// Splitting the string
// across spaces
print(gfg.split(" "));

}

`

**Output:

[Geeks, For, Geeks, !!]

Splitting each character of the string

**Example:

Dart `

// Main function void main() { // Creating a string String gfg = "GeeksForGeeks";

// Splitting each
// character of the string
print(gfg.split(""));

}

`

**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.

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

**Example:

Dart `

// Main function void main() { // Creating a string String gfg = "Geeks1For2Geeks3is4the5best6computer7science8website.";

// Splitting each character
// of the string
print(gfg.split(new RegExp(r"[0-9]")));

}

`

**Output:

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