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
644
votes
35 answers

Parse (split) a string in C++ using string delimiter (standard C++)

I am parsing a string in C++ using the following: using namespace std; string parsed,input="text to be parsed"; stringstream input_stringstream(input); if (getline(input_stringstream,parsed,' ')) { // do some processing. } Parsing with a…
TheCrazyProgrammer
  • 7,918
  • 8
  • 25
  • 41
638
votes
4 answers

Split string on whitespace in Python

I'm looking for the Python equivalent of String str = "many fancy word \nhello \thi"; String whiteSpaceRegex = "\\s"; String[] words = str.split(whiteSpaceRegex); ["many", "fancy", "word", "hello", "hi"]
siamii
  • 23,374
  • 28
  • 93
  • 143
636
votes
10 answers

How do I split a string into a list of words?

How do I split a sentence and store each word in a list? e.g. "these are words" ⟶ ["these", "are", "words"] To split on other delimiters, see Split a string by a delimiter in python. To split into individual characters, see How do I split a…
Thanx
  • 7,403
  • 5
  • 21
  • 12
628
votes
16 answers

How do I split a string, breaking at a particular character?

I have this string 'john smith~123 Street~Apt 4~New York~NY~12345' Using JavaScript, what is the fastest way to parse this into var name = "john smith"; var street= "123 Street"; //etc...
ctrlShiftBryan
  • 27,092
  • 26
  • 73
  • 78
593
votes
19 answers

Split string every nth character

How do I split a string every nth character? '1234567890' → ['12', '34', '56', '78', '90'] For the same question with a list, see How do I split a list into equally-sized chunks?.
Brandon L Burnett
  • 5,947
  • 3
  • 14
  • 4
587
votes
13 answers

How to split a string with any whitespace chars as delimiters

What regex pattern would need I to pass to java.lang.String.split() to split a String into an Array of substrings using all whitespace characters (' ', '\t', '\n', etc.) as delimiters?
mcjabberz
  • 9,788
  • 10
  • 36
  • 38
529
votes
46 answers

How do I split a delimited string so I can access individual items?

Using SQL Server, how do I split a string so I can access item x? Take a string "Hello John Smith". How can I split the string by space and access the item at index 1 which should return "John"?
GateKiller
  • 74,180
  • 73
  • 171
  • 204
480
votes
5 answers

Splitting on first occurrence

What would be the best way to split a string on the first occurrence of a delimiter? For example: "123mango abcd mango kiwi peach" splitting on the first mango to get: "abcd mango kiwi peach" To split on the last occurrence instead, see partition…
Acorn
  • 49,061
  • 27
  • 133
  • 172
478
votes
4 answers

Java string split with "." (dot)

Why does the second line of this code throw ArrayIndexOutOfBoundsException? String filename = "D:/some folder/001.docx"; String extensionRemoved = filename.split(".")[0]; While this works: String driveLetter = filename.split("/")[0]; I use Java 7.
Ali Ismayilov
  • 5,727
  • 2
  • 22
  • 24
475
votes
37 answers

How do I tokenize a string in C++?

Java has a convenient split method: String str = "The quick brown fox"; String[] results = str.split(" "); Is there an easy way to do this in C++?
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
459
votes
21 answers

Split Java String by New Line

I'm trying to split text in a JTextArea using a regex to split the String by \n However, this does not work and I also tried by \r\n|\r|n and many other combination of regexes. Code: public void insertUpdate(DocumentEvent e) { String split[],…
dr.manhattan
  • 4,842
  • 3
  • 19
  • 10
446
votes
10 answers

How to split by comma and strip white spaces in Python?

I have some python code that splits on comma, but doesn't strip the whitespace: >>> string = "blah, lots , of , spaces, here " >>> mylist = string.split(',') >>> print mylist ['blah', ' lots ', ' of ', ' spaces', ' here '] I would rather end…
Mr_Chimp
  • 6,658
  • 5
  • 37
  • 47
438
votes
17 answers

How to split a String by space

I need to split my String by spaces. For this I tried: str = "Hello I'm your String"; String[] splited = str.split(" "); But it doesn't seem to work.
safari
  • 7,565
  • 18
  • 56
  • 82
389
votes
17 answers

How to split a string in shell and get the last field

Suppose I have the string 1:2:3:4:5 and I want to get its last field (5 in this case). How do I do that using Bash? I tried cut, but I don't know how to specify the last field with -f.
cd1
  • 15,908
  • 12
  • 46
  • 47
384
votes
3 answers

Splitting on last delimiter in Python string?

What's the recommended Python idiom for splitting a string on the last occurrence of the delimiter in the string? example: # instead of regular split >> s = "a,b,c,d" >> s.split(",") >> ['a', 'b', 'c', 'd'] # ..split only on last occurrence of ','…
user248237