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
378
votes
5 answers

Java String split removed empty values

I am trying to split the Value using a separator. But I am finding the surprising results String data = "5|6|7||8|9||"; String[] split = data.split("\\|"); System.out.println(split.length); I am expecting to get 8 values.…
RaceBase
  • 18,428
  • 47
  • 141
  • 202
358
votes
13 answers

Split a Pandas column of lists into multiple columns

I have a Pandas DataFrame with one column: import pandas as pd df = pd.DataFrame({"teams": [["SF", "NYG"] for _ in range(7)]}) teams 0 [SF, NYG] 1 [SF, NYG] 2 [SF, NYG] 3 [SF, NYG] 4 [SF, NYG] 5 [SF, NYG] 6 [SF, NYG] How can split…
bgame2498
  • 4,467
  • 5
  • 15
  • 19
354
votes
12 answers

How can I swap positions of two open files (in splits) in vim?

Assume I've got some arbitrary layout of splits in vim. ____________________ | one | two | | | | | |______| | | three| | | | |___________|______| Is there a way to swap one and two and…
wes
  • 7,795
  • 6
  • 31
  • 41
349
votes
7 answers

How do I split a multi-line string into multiple lines?

I have a multi-line string that I want to do an operation on each line, like so: inputString = """Line 1 Line 2 Line 3""" I want to iterate on each line: for line in inputString: doStuff()
bradtgmurray
  • 13,683
  • 10
  • 38
  • 36
339
votes
11 answers

How to split one string into multiple strings separated by at least one space in bash shell?

I have a string containing many words with at least one space between each two. How can I split the string into individual words so I can loop through them? The string is passed as an argument. E.g. ${2} == "cat cat file". How can I loop through…
derrdji
  • 12,661
  • 21
  • 68
  • 78
331
votes
18 answers

Split data frame string column into multiple columns

I'd like to take data of the form before = data.frame(attr = c(1,30,4,6), type=c('foo_and_bar','foo_and_bar_2')) attr type 1 1 foo_and_bar 2 30 foo_and_bar_2 3 4 foo_and_bar 4 6 foo_and_bar_2 and use split() on the column…
jkebinger
  • 3,944
  • 4
  • 19
  • 14
327
votes
18 answers

Turning a Comma Separated string into individual rows

I have a SQL Table like this: SomeID OtherID Data abcdef-..... cdef123-... 18,20,22 abcdef-..... 4554a24-... 17,19 987654-..... 12324a2-... 13,19,20 Is there a query where I can perform a query like SELECT OtherID, SplitData WHERE…
Michael Stum
  • 177,530
  • 117
  • 400
  • 535
325
votes
21 answers

Split a List into smaller lists of N size

I am attempting to split a list into a series of smaller lists. My Problem: My function to split lists doesn't split them into lists of the correct size. It should split them into lists of size 30 but instead it splits them into lists of size…
sazr
  • 24,984
  • 66
  • 194
  • 362
309
votes
23 answers

Split large string in n-size chunks in JavaScript

I would like to split a very large string (let's say, 10,000 characters) into N-size chunks. What would be the best way in terms of performance to do this? For instance: "1234567890" split by 2 would become ["12", "34", "56", "78", "90"]. Would…
tribe84
  • 5,432
  • 5
  • 28
  • 30
308
votes
7 answers

How can I split a string with a string delimiter?

I have this string: "My name is Marco and I'm from Italy" I'd like to split it, with the delimiter being is Marco and, so I should get an array with My name at [0] and I'm from Italy at [1]. How can I do it with C#? I tried with: .Split("is Marco…
markzzz
  • 47,390
  • 120
  • 299
  • 507
305
votes
18 answers

How can I split a string into segments of n characters?

As the title says, I've got a string and I want to split into segments of n characters. For example: var str = 'abcdefghijkl'; after some magic with n=3, it will become var arr = ['abc','def','ghi','jkl']; Is there a way to do this?
Ben
  • 54,723
  • 49
  • 178
  • 224
305
votes
6 answers

How can I split up a Git commit buried in history?

I flubbed up my history and want to do some changes to it. Problem is, I have a commit with two unrelated changes, and this commit is surrounded by some other changes in my local (non-pushed) history. I want to split up this commit before I push it…
Ben
  • 7,692
  • 15
  • 49
  • 64
291
votes
11 answers

Cancel split window in Vim

I have split my windows horizontally. Now how can I return to normal mode, i.e. no split window just one window without cancelling all of my open windows. I have 5 and do not want to "quit", just want to get out of split window.
Sudeep
  • 3,015
  • 3
  • 17
  • 8
269
votes
2 answers

Unpacking a list / tuple of pairs into two lists / tuples

I have a list that looks like this: my_list = [('1','a'),('2','b'),('3','c'),('4','d')] I want to separate the list in 2 lists. list1 = ['1','2','3','4'] list2 = ['a','b','c','d'] I can do it for example with: list1 = [] list2 = [] for i in list: …
Breixo
  • 2,860
  • 2
  • 15
  • 9
269
votes
11 answers

Getting the last element of a split string array

I need to get the last element of a split array with multiple separators. The separators are commas and space. If there are no separators it should return the original string. If the string is "how,are you doing, today?" it should return "today?" If…
sol