Skip to content

Latest commit

 

History

History
123 lines (120 loc) · 17.4 KB

split-in-other-languages.md

File metadata and controls

123 lines (120 loc) · 17.4 KB

Split in some programming languages

LanguageFunctions / Methods
Rust
.split(pat)An iterator over substrings of this string slice, separated by characters matched by a pattern. The pattern can be a &str, char, a slice of chars, or a function or closure that determines if a character matches.
.split_terminator(pat)Equivalent to split, except that the trailing substring is skipped if empty. This method can be used for string data that is terminated, rather than separated by a pattern.
.splitn(n, pat)... restricted to returning at most n items...
.split_once(delim)Splits the string on the first occurrence of the specified delimiter and returns prefix before delimiter and suffix after delimiter.
.split_inclusive(pat)... leaves the matched part as the terminator of the substring...
.split_ascii_whitespace()Splits a string slice by ASCII whitespace. The iterator returned will return string slices that are sub-slices of the original string slice, separated by any amount of ASCII whitespace.
.split_whitespace()... ‘Whitespace’ is defined according to the terms of the Unicode Derived Core Property White_Space.
.split_at(mid)Divide one string slice into two at an index. The argument, mid, should be a byte offset from the start of the string. It must also be on the boundary of a UTF-8 code point. The two slices returned go from the start of the string slice to mid, and from mid to the end of the string slice.
.split_at_mut(mid)Divide one mutable string slice into two at an index...
.rsplit(pat)... yielded in reverse order...
.rsplit_terminator(pat)
.rsplitn(n, pat)... starting from the end of the string, restricted to returning at most n items.
.rsplit_once(delim)Splits the string on the last occurrence of the specified delimiter...
Python
split([separator [maxsplit]])

Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).

If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, '1,,2'.split(',') returns ['1', '', '2']). The sep argument may consist of multiple characters (for example, '1<>2<>3'.split('<>') returns ['1', '2', '3']). Splitting an empty string with a specified separator returns [''].

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].

rsplit([separator [maxsplit]])Except for splitting from the right, rsplit() behaves like split().
splitlines([keepends])

Return a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the resulting list unless keepends is given and true.

Unlike split() when a delimiter string sep is given, this method returns an empty list for the empty string, and a terminal line break does not result in an extra line:

bytes.split(sep=None, maxsplit=- 1)

Split the binary sequence into subsequences of the same type, using sep as the delimiter string. If maxsplit is given and non-negative, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or is -1, then there is no limit on the number of splits (all possible splits are made).

If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty subsequences (for example, b'1,,2'.split(b',') returns [b'1', b'', b'2']). The sep argument may consist of a multibyte sequence (for example, b'1<>2<>3'.split(b'<>') returns [b'1', b'2', b'3']). Splitting an empty sequence with a specified separator returns [b''] or [bytearray(b'')] depending on the type of object being split. The sep argument may be any bytes-like object.

bytearray.split(sep=None, maxsplit=- 1)^...
bytes.rsplit(sep=None, maxsplit=- 1)Except for splitting from the right, rsplit() behaves like split().
bytearray.rsplit(sep=None, maxsplit=- 1)^...
JavaScript, ECMAScript
split([separator [limit]])Divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a pattern; where the pattern is provided as the first parameter in the method's call. When found, separator is removed from the string, and the substrings are returned in an array. If separator is a regular expression with capturing parentheses, then each time separator matches, the results (including any undefined results) of the capturing parentheses are spliced into the output array. If the separator is an array, then that Array is coerced to a String and used as a separator.
Go
Split(s, sep)Slices s into all substrings separated by sep and returns a slice of the substrings between those separators. If s does not contain sep and sep is not empty, Split returns a slice of length 1 whose only element is s. If sep is empty, Split splits after each UTF-8 sequence. If both s and sep are empty, Split returns an empty slice. It is equivalent to SplitN with a count of -1.
SplitN(s, sep, n)Slices s into substrings separated by sep and returns a slice of the substrings between those separators. The count determines the number of substrings to return
SplitAfter(s, sep)Slices s into all substrings after each instance of sep and returns a slice of those substrings. If s does not contain sep and sep is not empty, SplitAfter returns a slice of length 1 whose only element is s. If sep is empty, SplitAfter splits after each UTF-8 sequence. If both s and sep are empty, SplitAfter returns an empty slice. It is equivalent to SplitAfterN with a count of -1.
SplitAfterN(s, sep, n)Slices s into substrings after each instance of sep and returns a slice of those substrings. The count determines the number of substrings to return
Fields(s)Splits the string s around each instance of one or more consecutive white space characters, as defined by unicode.IsSpace, returning a slice of substrings of s or an empty slice if s contains only white space.
FieldsFunc(s, f)Splits the string s at each run of Unicode code points c satisfying f(c) and returns an array of slices of s. If all code points in s satisfy f(c) or the string is empty, an empty slice is returned. FieldsFunc makes no guarantees about the order in which it calls f(c) and assumes that f always returns the same value for a given c.
re.split(s, n)Slices s into substrings separated by the expression and returns a slice of the substrings between those expression matches. The slice returned by this method consists of all the substrings of s not contained in the slice returned by FindAllString. When called on an expression that contains no metacharacters, it is equivalent to strings.SplitN.
Wolfram
StringSplit["string"]Splits "string" into a list of substrings separated by whitespace.
StringSplit["string",patt]Splits into substrings separated by delimiters matching the string expression patt.
StringSplit["string",{p1,p2,…}]Splits at any of the p(i).
StringSplit["string",patt->val]Inserts val at the position of each delimiter.
StringSplit["string",{p1->v1,…}]Inserts v(i) at the position of each delimiter p(i).
StringSplit["string",patt,n]Splits into at most n substrings.
StringSplit[{s1,s2,…},p]Gives the list of results for each of the s(i).
SplitBy[list,f]Splits list into sublists consisting of runs of successive elements that give the same value when f is applied.
SplitBy[list,{f1,f2,…}]Recursively splits list into sublists by testing elements successively with each of the f(i).
Notes:
SplitBy[list,…] splits but does not rearrange list.
SplitBy performs comparisons only on adjacent pairs of elements.
SplitBy[list] is equivalent to SplitBy[list,Identity], which is also equivalent to Split[list].
SplitBy[list,{f1,f2}] is equivalent to Map[SplitBy[#,f2]&,SplitBy[list,f1]].
Split[list]Splits list into sublists consisting of runs of identical elements.
Split[list,test]Treats pairs of adjacent elements as identical whenever applying the function test to them yields True.
SequenceSplit[list,patt]Splits list into sublists separated by sequences that match the sequence pattern patt. By default gives the list of sublists of list that occur between sequences defined by patt; it does not include the sequences themselves.
SequenceSplit[list,patt->rhs]Inserts rhs at the position of each matched sequence.
SequenceSplit[list,{patt1->rhs1,…}]Inserts rhs(i) at the position of each patt(i). (SequenceSplit[list,{patt(1)->rhs(1),…,patt(a),…}] includes rhs(i) at the position of sequences matching patt(1) but omits sequences matching patt(a).)
SequenceSplit[list,patt,n]Splits into at most n sublists.
ResourceFunction["StringSplitAfter"]["string"]Splits "string" into a list of substrings after each whitespace.
ResourceFunction["StringSplitAfter"]["string",patt]Splits "string" into substrings after delimiters matching the string expression patt.
ResourceFunction["StringSplitAfter"]["string",{p1,p2,…}]Splits "string" after any of the p(i).
ResourceFunction["StringSplitAfter"]["string",patt->val]Replaces each delimiter matching patt with val.
ResourceFunction["StringSplitAfter"]["string",{p1->v1,…}]Replaces each delimiter matching p(i) with v(i).
ResourceFunction["StringSplitAfter"][{s1,s2,…},p]Gives the list of results for each of the s(i).
ResourceFunction["StringSplitBefore"]["string"]Splits "string" into a list of substrings before each whitespace.
ResourceFunction["StringSplitBefore"]["string",patt]Splits "string" into substrings before delimiters matching the string expression patt.
ResourceFunction["StringSplitBefore"]["string",{p1,p2,…}]Splits "string" before any of the p(i).
ResourceFunction["StringSplitBefore"]["string",patt->val]Replaces each delimiter matching patt with val.
ResourceFunction["StringSplitBefore"]["string",{p1->v1,…}]Replaces each delimiter matching p(i) with v(i).
ResourceFunction["StringSplitBefore"][{s1,s2,…},p]Gives the list of results for each of the s(i).
ResourceFunction["SplitWhen"][list,f]Splits list into sublists, splitting after each ei for which f[ei] is True.
ResourceFunction["SplitByPatterns"][list,{patt1,patt2,…}]Splits list into sublists consisting of runs of successive elements that match the same patti.
ResourceFunction["RandomSplit"][list,n]Randomly cuts list into n segments.
ResourceFunction["RandomSplit"][n]Represents an operator form of ResourceFunction["RandomSplit"] that can be applied to an expression.
ResourceFunction["TrainTestSplit"][data]Splits data into a pair of shuffled training and testing sets.
VideoSplit, AudioSplit, ImageSplitCompare...
.NET
Split(Char, Int32, StringSplitOptions)Splits a string into a maximum number of substrings based on a specified delimiting character and, optionally, options. Splits a string into a maximum number of substrings based on the provided character separator, optionally omitting empty substrings from the result.
Split(String[], Int32, StringSplitOptions)Splits a string into a maximum number of substrings based on specified delimiting strings and, optionally, options.
Split(Char[], Int32, StringSplitOptions)Splits a string into a maximum number of substrings based on specified delimiting characters and, optionally, options.
Split(String[], StringSplitOptions)Splits a string into substrings based on a specified delimiting string and, optionally, options.
Split(String, Int32, StringSplitOptions)Splits a string into a maximum number of substrings based on a specified delimiting string and, optionally, options.
Split(Char[], StringSplitOptions)Splits a string into substrings based on specified delimiting characters and options.
Split(Char[], Int32)Splits a string into a maximum number of substrings based on specified delimiting characters.
Split(Char, StringSplitOptions)Splits a string into substrings based on a specified delimiting character and, optionally, options.
Split(String, StringSplitOptions)Splits a string into substrings that are based on the provided string separator.
Split(Char[])Splits a string into substrings based on specified delimiting characters.
See also: https://docs.microsoft.com/en-us/dotnet/api/system.stringsplitoptions?view=net-5.0
Java
split(String regex)Splits this string around matches of the given regular expression.
split(String regex, int limit)Splits this string around matches of the given regular expression.
Racket
(split-at lst pos) → list? any/c
lst : any/c
pos : exact-nonnegative-integer?
Splits a list at position.
Returns the same result as (values (take lst pos) (drop lst pos))
except that it can be faster, but it will still take time proportional to pos.
(splitf-at lst pred) → list? any/c
lst : any/c
pred : procedure?
Splits a list by predicate.
Returns the same result as (values (takef lst pred) (dropf lst pred)) except that it can be faster.
(split-at-right lst pos) → list? any/c
lst : any/c
pos : exact-nonnegative-integer?
Splits a list at position from the end.
Returns the same result as (values (drop-right lst pos) (take-right lst pos)) except that it can be faster, but it will still take time proportional to the length of lst.
(splitf-at-right lst pred) → list? any/c
lst : any/c
pred : procedure?
Like takef, dropf, and splitf-at, but combined with the from-right functionality of take-right, drop-right, and split-at-right.
(split-common-prefix l r [same?]) → list? list? list?
l : list?
r : list?
same? : (any/c any/c . -> . any/c) = equal?
Returns the longest common prefix together with the tails of l and r with the common prefix removed.
Example:
(split-common-prefix '(a b c d) '(a b x y z))
'(a b)
'(c d)
'(x y z)

See also: