How do I split a string into a list of words?
Given a string sentence, this stores each word in a list called words: words = sentence.split()
Given a string sentence, this stores each word in a list called words: words = sentence.split()
Convert comma separated String to List List<String> items = Arrays.asList(str.split(“\\s*,\\s*”)); The above code splits the string on a delimiter defined as: zero or more whitespace, a literal comma, zero or more whitespace which will place the words into the list and collapse any whitespace between the words and commas. Please note that this returns simply … Read more
Vim mailing list says (re-formatted for better readability): To change two vertically split windows to horizonally split Ctrl–w t Ctrl–w K Horizontally to vertically: Ctrl–w t Ctrl–w H Explanations: Ctrl–w t makes the first (topleft) window current Ctrl–w K moves the current window to full-width at the very top Ctrl–w H moves the current window … Read more
Pass in a regexp as the parameter: js> “Hello awesome, world!”.split(/[\s,]+/) Hello,awesome,world! Edited to add: You can get the last element by selecting the length of the array minus 1: >>> bits = “Hello awesome, world!”.split(/[\s,]+/) [“Hello”, “awesome”, “world!”] >>> bit = bits[bits.length – 1] “world!” … and if the pattern doesn’t match: >>> bits … Read more
Luckily, Python has this built-in 🙂 import re re.split(‘; |, ‘, string_to_split) Update:Following your comment: >>> a=”Beautiful, is; better*than\nugly” >>> import re >>> re.split(‘; |, |\*|\n’,a) [‘Beautiful’, ‘is’, ‘better’, ‘than’, ‘ugly’]
re.split() re.split(pattern, string[, maxsplit=0]) Split string by the occurrences of pattern. If capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the resulting list. If maxsplit is nonzero, at most maxsplit splits occur, and the remainder of the string is returned as the … Read more
Just call componentsSeparatedByString method on your fullName import Foundation var fullName: String = “First Last” let fullNameArr = fullName.componentsSeparatedByString(” “) var firstName: String = fullNameArr[0] var lastName: String = fullNameArr[1] Update for Swift 3+ import Foundation let fullName = “First Last” let fullNameArr = fullName.components(separatedBy: ” “) let name = fullNameArr[0] let surname = fullNameArr[1]
In order to split by a string you’ll have to use the string array overload. string data = “THExxQUICKxxBROWNxxFOX”; return data.Split(new string[] { “xx” }, StringSplitOptions.None);
var array = string.split(‘,’); MDN reference, mostly helpful for the possibly unexpected behavior of the limit parameter. (Hint: “a,b,c”.split(“,”, 2) comes out to [“a”, “b”], not [“a”, “b,c”].)
IFS=’, ‘ read -r -a array <<< “$string” Note that the characters in $IFS are treated individually as separators so that in this case fields may be separated by either a comma or a space rather than the sequence of the two characters. Interestingly though, empty fields aren’t created when comma-space appears in the input … Read more