Questions tagged [split]

Use this tag for questions about separating an item (e.g. a string) into parts, often by a delimiter or regular expression.

It is most often applied when splitting a string by a delimiter, such as when parsing a comma separated value file. Some languages such as Ruby and Java support splitting a string according to regular expression as well.

A split string produces an array of strings.

split can also refer to

  1. the split function
  2. the String#split method
  3. the str.split method
  4. the explode function.
  5. the split method
  6. the split method
  7. the // String.Split method
  8. the / split command line utility
  9. the STRING_SPLIT function

All these functions/methods split strings on a delimiter.

The split function in divides data into groups by a specified factor.

24261 questions
251
votes
4 answers

Can I split an already split hunk with git?

I've recently discovered git's patch option to the add command, and I must say it really is a fantastic feature. I also discovered that a large hunk could be split into smaller hunks by hitting the s key, which adds to the precision of the…
greg0ire
  • 22,714
  • 16
  • 72
  • 101
250
votes
16 answers

How to split a comma-separated string?

I have a String with an unknown length that looks something like this "dog, cat, bear, elephant, ..., giraffe" What would be the optimal way to divide this string at the commas so each word could become an element of an ArrayList? For…
James Fazio
  • 6,370
  • 9
  • 38
  • 47
248
votes
11 answers

Split string, convert ToList() in one line

I have a string that has numbers string sNumbers = "1,2,3,4,5"; I can split it then convert it to List sNumbers.Split( new[] { ',' } ).ToList(); How can I convert string array to integer list? So that I'll be able to convert string[] to…
uzay95
  • 16,052
  • 31
  • 116
  • 182
245
votes
5 answers

Split a string by a delimiter in Python

How to split this string where __ is the delimiter MATCHES__STRING To get an output of ['MATCHES', 'STRING']? For splitting specifically on whitespace, see How do I split a string into a list of words?. To extract everything before the first…
Hulk
  • 32,860
  • 62
  • 144
  • 215
243
votes
5 answers

How to split a delimited string in Ruby and convert it to an array?

I have a string "1,2,3,4" and I'd like to convert it into an array: [1,2,3,4] How?
Neil Middleton
  • 22,105
  • 18
  • 80
  • 134
213
votes
7 answers

The split() method in Java does not work on a dot (.)

I have prepared a simple code snippet in order to separate the erroneous portion from my web application. public class Main { public static void main(String[] args) throws IOException { System.out.print("\nEnter a string:->"); …
Bhavesh
  • 4,607
  • 13
  • 43
  • 70
212
votes
22 answers

Split list into smaller lists (split in half)

I am looking for a way to easily split a python list in half. So that if I have an array: A = [0,1,2,3,4,5] I would be able to get: B = [0,1,2] C = [3,4,5]
corymathews
  • 12,289
  • 14
  • 57
  • 77
203
votes
6 answers

split string only on first instance - java

I want to split a string by '=' charecter. But I want it to split on first instance only. How can I do that ? Here is a JavaScript example for '_' char but it doesn't work for me split string only on first instance of specified character Example…
dracula
  • 4,413
  • 6
  • 26
  • 31
200
votes
28 answers

How to split data into training/testing sets using sample function

I've just started using R and I'm not sure how to incorporate my dataset with the following sample code: sample(x, size, replace = FALSE, prob = NULL) I have a dataset that I need to put into a training (75%) and testing (25%) set. I'm not sure…
Susie Humby
  • 2,001
  • 2
  • 13
  • 3
198
votes
6 answers

How would I get everything before a : in a string Python

I am looking for a way to get all of the letters in a string before a : but I have no idea on where to start. Would I use regex? If so how? string = "Username: How are you today?" Can someone show me a example on what I could do?
0Cool
  • 2,335
  • 4
  • 16
  • 16
195
votes
25 answers

Split string with delimiters in C

How do I write a function to split and return an array for a string with delimiters in the C programming language? char* str = "JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC"; str_split(str,',');
namco
  • 6,208
  • 20
  • 59
  • 83
194
votes
7 answers

When splitting an empty string in Python, why does split() return an empty list while split('\n') returns ['']?

I am using split('\n') to get lines in one string, and found that ''.split() returns an empty list, [], while ''.split('\n') returns ['']. Is there any specific reason for such a difference? And is there any more convenient way to count lines in a…
godice
  • 1,989
  • 2
  • 13
  • 9
188
votes
27 answers

T-SQL split string

I have a SQL Server 2008 R2 column containing a string which I need to split by a comma. I have seen many answers on StackOverflow but none of them works in R2. I have made sure I have select permissions on any split function examples. Any help…
Lee Grindon
  • 2,117
  • 2
  • 16
  • 8
187
votes
7 answers

How to change size of split screen emacs windows?

I have emacs split horizontally - on top I'm editing Perl code, the bottom is the shell. By default emacs makes the two windows equal in size, but I'd like the shell buffer smaller (maybe half the size?). I was wondering how I could do that.
Steve
  • 1,873
  • 2
  • 12
  • 4
186
votes
6 answers

string.split - by multiple character delimiter

i am having trouble splitting a string in c# with a delimiter of "][". For example the string "abc][rfd][5][,][." Should yield an array containing; abc rfd 5 , . But I cannot seem to get it to work, even if I try RegEx I cannot get a split on the…
enricco