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
130
votes
4 answers

Python split() without removing the delimiter

This code almost does what I need it to.. for line in all_lines: s = line.split('>') Except it removes all the '>' delimiters. So, Turns into ['
some1
  • 2,447
  • 8
  • 26
  • 23
128
votes
21 answers

How can I parse a CSV string with JavaScript, which contains comma in data?

I have the following type of string var string = "'string, duppi, du', 23, lala" I want to split the string into an array on each comma, but only the commas outside the single quotation marks. I can't figure out the right regular expression for the…
Björn
  • 12,587
  • 12
  • 51
  • 70
128
votes
2 answers

Java split() method strips empty strings at the end?

Check out the below program. try { for (String data : Files.readAllLines(Paths.get("D:/sample.txt"))){ String[] de = data.split(";"); System.out.println("Length = " + de.length); } } catch (IOException e) { …
raja
  • 4,043
  • 6
  • 30
  • 24
126
votes
6 answers

How to split a file into equal parts, without breaking individual lines?

I was wondering if it was possible to split a file into equal parts (edit: = all equal except for the last), without breaking the line? Using the split command in Unix, lines may be broken in half. Is there a way to, say, split up a file in 5 equal…
Abdel
  • 5,826
  • 12
  • 56
  • 77
125
votes
6 answers

How does Hadoop process records split across block boundaries?

According to the Hadoop - The Definitive Guide The logical records that FileInputFormats define do not usually fit neatly into HDFS blocks. For example, a TextInputFormat’s logical records are lines, which will cross HDFS boundaries more often than…
Praveen Sripati
  • 32,799
  • 16
  • 80
  • 117
123
votes
8 answers

How can I use "." as the delimiter with String.split() in java

What I am trying to do is read a .java file, and pick out all of the identifiers and store them in a list. My problem is with the .split() method. If you run this code the way it is, you will get ArrayOutOfBounds, but if you change the delimiter…
Matt
  • 1,353
  • 3
  • 11
  • 9
121
votes
13 answers

Split string with dot as delimiter

I am wondering if I am going about splitting a string on a . the right way? My code is: String[] fn = filename.split("."); return fn[0]; I only need the first part of the string, that's why I return the first item. I ask because I noticed in the…
Dean
  • 8,668
  • 17
  • 57
  • 86
119
votes
5 answers

Get last "column" after .str.split() operation on column in pandas DataFrame

I have a column in a pandas DataFrame that I would like to split on a single space. The splitting is simple enough with DataFrame.str.split(' '), but I can't make a new column from the last entry. When I .str.split() the column I get a list of…
Richard Herron
  • 9,760
  • 12
  • 69
  • 116
118
votes
7 answers

Is there a function in python to split a word into a list?

Is there a function in python to split a word into a list of single letters? e.g: s = "Word to Split" to get wordlist = ['W', 'o', 'r', 'd', ' ', 't', 'o', ' ', 'S', 'p', 'l', 'i', 't']
gath
  • 2,286
  • 5
  • 19
  • 11
117
votes
11 answers

How to count the number of lines of a string in javascript

I would like to count the number of lines in a string. I tried to use this stackoverflow answer, lines = str.split("\r\n|\r|\n"); return lines.length; on this string (which was originally a buffer): GET / HTTP/1.1 Host: localhost:8888 …
Itzik984
  • 15,968
  • 28
  • 69
  • 107
116
votes
5 answers

Objective-C Split()?

Is there any way to split strings in objective c into arrays? I mean like this - input string Yes:0:42:value into an array of (Yes,0,42,value)?
Christian Stewart
  • 15,217
  • 20
  • 82
  • 139
116
votes
3 answers

Why in Java 8 split sometimes removes empty strings at start of result array?

Before Java 8 when we split on empty string like String[] tokens = "abc".split(""); split mechanism would split in places marked with | |a|b|c| because empty space "" exists before and after each character. So as result it would generate at first…
Pshemo
  • 122,468
  • 25
  • 185
  • 269
115
votes
6 answers

How to split CSV files as per number of rows specified?

I've CSV file (around 10,000 rows ; each row having 300 columns) stored on LINUX server. I want to break this CSV file into 500 CSV files of 20 records each. (Each having same CSV header as present in original CSV) Is there any linux command to…
Pawan Mude
  • 1,609
  • 6
  • 19
  • 32
111
votes
6 answers

Splitting String with delimiter

I am currently trying to split a string 1128-2 so that I can have two separate values. For example, value1: 1128 and value2: 2, so that I can then use each value separately. I have tried split() but with no success. Is there a specific way Grails…
thehoule64
  • 1,761
  • 5
  • 15
  • 22
109
votes
4 answers

How to use split?

I need to break apart a string that always looks like this: something -- something_else. I need to put "something_else" in another input field. Currently, this string example is being added to an HTML table row on the fly like…
Matt
  • 5,542
  • 14
  • 48
  • 59