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

The result list contains single spaces when splitting a string with re.split("( )+") – is there a better way?

I have the output of a command in tabular form. I'm parsing this output from a result file and storing it in a string. Each element in one row is separated by one or more whitespace characters, thus I'm using regular expressions to match 1 or more…
gjois
  • 2,025
  • 3
  • 18
  • 19
173
votes
12 answers

Java: Get last element after split

I am using the String split method and I want to have the last element. The size of the Array can change. Example: String one = "Düsseldorf - Zentrum - Günnewig Uebachs" String two = "Düsseldorf - Madison" I want to split the above Strings and get…
n00ki3
  • 14,529
  • 18
  • 56
  • 65
172
votes
20 answers

How can I split a text into sentences?

I have a text file. I need to get a list of sentences. How can this be implemented? There are a lot of subtleties, such as a dot being used in abbreviations. My old regular expression works badly: re.compile('(\.…
Artyom
  • 2,863
  • 3
  • 20
  • 15
171
votes
10 answers

Why are empty strings returned in split() results?

What is the point of '/segment/segment/'.split('/') returning ['', 'segment', 'segment', '']? Notice the empty elements. If you're splitting on a delimiter that happens to be at position one and at the very end of a string, what extra value does it…
orokusaki
  • 55,146
  • 59
  • 179
  • 257
170
votes
2 answers

How to join two tmux windows into one, as panes?

I have two tmux windows, with a single pane in each, and I would like to join these two panes together into a single window as a horizontal split panes. How could I do that?
RNA
  • 146,987
  • 15
  • 52
  • 70
169
votes
8 answers

How to split a string at the first `/` (slash) and surround part of it in a ``?

I want to format this date:
23/05/2013
. First I want to split the string at the first / and have the rest in the next line. Next, I’d like to surround the first part in a tag, as follows:
Mustapha Aoussar
  • 5,833
  • 15
  • 62
  • 107
168
votes
10 answers

Scanner vs. StringTokenizer vs. String.Split

I just learned about Java's Scanner class and now I'm wondering how it compares/competes with the StringTokenizer and String.Split. I know that the StringTokenizer and String.Split only work on Strings, so why would I want to use the Scanner for a…
Dave
  • 4,050
  • 6
  • 30
  • 35
167
votes
9 answers

How to split a string and assign it to variables

In Python it is possible to split a string and assign it to variables: ip, port = '127.0.0.1:5432'.split(':') but in Go it does not seem to work: ip, port := strings.Split("127.0.0.1:5432", ":") // assignment count mismatch: 2 = 1 Question: How to…
Pyt
  • 1,925
  • 2
  • 13
  • 10
166
votes
9 answers

How can I split and trim a string into parts all on one line?

I want to split this line: string line = "First Name ; string ; firstName"; into an array of their trimmed versions: "First Name" "string" "firstName" How can I do this all on one line? The following gives me an error "cannot convert type…
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
161
votes
6 answers

Parameter "stratify" from method "train_test_split" (scikit Learn)

I am trying to use train_test_split from package scikit Learn, but I am having trouble with parameter stratify. Hereafter is the code: from sklearn import cross_validation, datasets X = iris.data[:,:2] y =…
Daneel Olivaw
  • 2,077
  • 4
  • 15
  • 23
161
votes
19 answers

How to split the name string in mysql?

How to split the name string in mysql ? E.g.: name ----- Sachin ramesh tendulkar Rahul dravid Split the name like firstname,middlename,lastname: firstname middlename lastname --------- ------------ ------------ sachin ramesh …
Madhav
  • 2,285
  • 3
  • 17
  • 16
152
votes
13 answers

How to split newline

I'm using jQuery, and I have a textarea. When I submit by my button I will alert each text separated by newline. How to split my text when there is a newline? var ks = $('#keywords').val().split("\n"); (function($){ …
oknoorap
  • 1,923
  • 2
  • 14
  • 20
152
votes
23 answers

Split string to equal length substrings in Java

How to split the string "Thequickbrownfoxjumps" to substrings of equal size in Java. Eg. "Thequickbrownfoxjumps" of 4 equal size should give the output. ["Theq","uick","brow","nfox","jump","s"] Similar Question: Split string into equal-length…
Emil
  • 13,577
  • 18
  • 69
  • 108
152
votes
5 answers

What is causing the error `string.split is not a function`?

Why am I getting... Uncaught TypeError: string.split is not a function ...when I run... var string = document.location; var split = string.split('/');
erikvimz
  • 5,256
  • 6
  • 44
  • 60
150
votes
18 answers

How to split text without spaces into list of words

Input: "tableapplechairtablecupboard..." many words What would be an efficient algorithm to split such text to the list of words and get: Output: ["table", "apple", "chair", "table", ["cupboard", ["cup", "board"]], ...] First thing that cames to…
Sergey
  • 19,487
  • 13
  • 44
  • 68