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
108
votes
6 answers

How to split elements of a list?

I have a list: my_list = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847'] How can I delete the \t and everything after to get this result: ['element1', 'element2', 'element3']
user808545
  • 1,551
  • 4
  • 15
  • 15
107
votes
5 answers

Split Spark dataframe string column into multiple columns

I've seen various people suggesting that Dataframe.explode is a useful way to do this, but it results in more rows than the original dataframe, which isn't what I want at all. I simply want to do the Dataframe equivalent of the very…
Peter Gaultney
  • 3,269
  • 4
  • 16
  • 20
107
votes
4 answers

Java split string to array

I need help with the split() method. I have the followingString: String values = "0|0|0|1|||0|1|0|||"; I need to put the values into an array. There are 3 possible strings: "0", "1", and "" My problem is, when i try to use split(): String[] array…
Dusan
  • 3,284
  • 6
  • 25
  • 46
106
votes
9 answers

Split text file into smaller multiple text file using command line

I have multiple text file with about 100,000 lines and I want to split them into smaller text files of 5000 lines each. I used: split -l 5000 filename.txt That creates files: xaa xab aac xad xbe aaf files with no extensions. I just want to call…
ashleybee97
  • 1,205
  • 3
  • 11
  • 8
105
votes
5 answers

Split JavaScript array in chunks using Lodash

I need to split a JavaScript array into n sized chunks. E.g.: Given this array ["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13"] and a n equals to 4, the output should be this: [ ["a1", "a2", "a3", "a4"], ["a5",…
Cesar Canassa
  • 18,659
  • 11
  • 66
  • 69
105
votes
12 answers

Split one file into multiple files based on delimiter

I have one file with -| as delimiter after each section...need to create separate files for each section using unix. example of input…
user1499178
  • 1,059
  • 2
  • 8
  • 3
104
votes
3 answers

Split a large dataframe into a list of data frames based on common value in column

I have a data frame with 10 columns, collecting actions of "users", where one of the columns contains an ID (not unique, identifying user)(column 10). the length of the data frame is about 750000 rows. I am trying to extract individual data frames…
MartinT
  • 1,671
  • 3
  • 14
  • 14
104
votes
6 answers

Java equivalent to Explode and Implode(PHP)

I am new in Java although had a good experience in PHP, and looking for perfect replacement for explode and implode (available in PHP) functions in Java. I have Googled for the same but not satisfied with the results. Anyone has the good solution…
Pankaj Wanjari
  • 1,275
  • 2
  • 9
  • 11
102
votes
6 answers

How do I extract a single chunk of bytes from within a file?

On a Linux desktop (RHEL4) I want to extract a range of bytes (typically less than 1000) from within a large file (>1 Gig). I know the offset into the file and the size of the chunk. I can write code to do this but is there a command line…
DanM
  • 2,331
  • 2
  • 18
  • 14
101
votes
8 answers

Why is splitting a string slower in C++ than Python?

I'm trying to convert some code from Python to C++ in an effort to gain a little bit of speed and sharpen my rusty C++ skills. Yesterday I was shocked when a naive implementation of reading lines from stdin was much faster in Python than C++ (see…
JJC
  • 9,547
  • 8
  • 48
  • 53
100
votes
5 answers

How to split path by last slash?

I have a file (say called list.txt) that contains relative paths to files, one path per line, i.e. something like this: foo/bar/file1 foo/bar/baz/file2 goo/file3 I need to write a bash script that processes one path at a time, splits it at the last…
I Z
  • 5,719
  • 19
  • 53
  • 100
99
votes
1 answer

How to file split at a line number

I want to split a 400k line long log file from a particular line number. For this question, lets make this an arbitrary number 300k. Is there a linux command that allows me to do this (within the script)? I know split lets me split the file in equal…
denormalizer
  • 2,186
  • 3
  • 24
  • 36
99
votes
17 answers

Split string once in javascript?

How can I split a string only once, i.e. make 1|Ceci n'est pas une pipe: | Oui parse to: ["1", "Ceci n'est pas une pipe: | Oui"]? The limit in split doesn't seem to help...
Stavros Korokithakis
  • 4,680
  • 10
  • 35
  • 42
96
votes
4 answers

Split a string only by first space in python

I have string for example: "238 NEO Sports". I want to split this string only at the first space. The output should be ["238","NEO Sports"]. One way I could think of is by using split() and finally merging the last two strings returned. Is there a…
bazinga
  • 2,120
  • 4
  • 21
  • 35
96
votes
7 answers

Split string in JavaScript and detect line break

I have a small function I found that takes a string from a textarea and then puts it into a canvas element and wraps the text when the line gets too long. But it doesn't detect line breaks. This is what it's doing and what it should…
Dustin Silk
  • 4,320
  • 5
  • 32
  • 48