How to split a string in Elixir
Overview
The split() method is used to split a string based on the given pattern.
Syntax
String.split(str, pat)
Parameters
str: The string to split.pat: The pattern to use to split the string. The pattern can be a regex or a string.
Return value
The method returns a list.
Code
string = "hello educative"pattern = "\s"split_string = String.split(string, pattern)IO.inspect split_string
Explanation
- Line 1: We define the string to be split.
- Line 2: We define the pattern to use.
- Line 3: We split the string into substrings based on the given pattern using the
String.split()method. - Line 4: We print the list returned after the split.
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved