split

split(string, [chars]; limit=0, keep=true)

Return an array of substrings by splitting the given string on occurrences of the given character delimiters, which may be specified in any of the formats allowed by search's second argument (i.e. a single character, collection of characters, string, or regular expression). If chars is omitted, it defaults to the set of all space characters, and keep is taken to be false. The two keyword arguments are optional: they are are a maximum size for the result and a flag determining whether empty fields should be kept in the result.

Examples

  1. Split a string using a single character delimiter:

    julia> split("Hello,World", ',')
    2-element Array{SubString{String},1}:
    "Hello"
    "World"

    This example splits the given string "Hello,World" using the comma (,) as the delimiter.

  2. Split a string using multiple characters as delimiters:

    julia> split("Julia is awesome!", [' ', 'a'])
    5-element Array{SubString{String},1}:
    "Jul"
    "i"
    "is"
    "esome!"
    "wesome!"

    In this example, the string "Julia is awesome!" is split using both space (``) and letter 'a' as delimiters.

  3. Split a string using a regular expression:

    julia> split("Julia programming language", r"\s+")
    3-element Array{SubString{String},1}:
    "Julia"
    "programming"
    "language"

    Here, the string "Julia programming language" is split using the regular expression \s+, which matches one or more whitespace characters.

  4. Limit the number of resulting substrings:

    julia> split("apple,banana,orange,grape", ',', limit=2)
    2-element Array{SubString{String},1}:
    "apple"
    "banana,orange,grape"

    By setting the limit parameter to 2, the resulting array is limited to two substrings.

  5. Remove empty fields from the result:
    julia> split("The quick  brown  fox", keep=false)
    4-element Array{SubString{String},1}:
    "The"
    "quick"
    "brown"
    "fox"

    Here, the keep parameter is set to false, resulting in empty fields being removed from the output.

Common mistake example:

julia> split("Hello World", ",")
1-element Array{SubString{String},1}:
 "Hello World"

In this example, the delimiter , does not exist in the given string. It's important to ensure that the delimiter matches an actual occurrence in the string to split it correctly.

See Also

ascii, base64decode, Base64DecodePipe, base64encode, Base64EncodePipe, bin, bits, bytestring, charwidth, chomp, chop, chr2ind, contains, endswith, escape_string, graphemes, ind2chr, iscntrl, istext, isupper, isvalid, join, lcfirst, lowercase, lpad, lstrip, normalize_string, num2hex, parseip, randstring, readuntil, replace, repr, rpad, rsplit, rstrip, search, searchindex, split, startswith, string, stringmime, strip, strwidth, summary, takebuf_string, ucfirst, unescape_string, uppercase, utf16, utf32, utf8, wstring,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: