Often,the best approach is the simplest. With a single character delimiter, we can write simple code to split the string. Sometimes a regular expression is needed.
First example.Here we introduce a string with a plus delimiter. The "plus" character separates four color names. We call split with a one-character string argument: the "+" char.Strings
Result: An array is returned from the split method. It has four elements, one string for each color word.Array
Regex.Sometimes we need a more complex approach to separating strings. We can use a regular expression. Here we split on one or more non-word characters (like spaces or newlines).
Tip: With a regular expression, we can treat multiple delimiters as a single one. This eliminates empty entries.
Get numbers.With split() and a regular expression we can get the numbers from a string. We split on non-digit characters. The uppercase "D+" means one or more non-digit chars.
Tip: To extract numbers, we can split on "not number" characters. Then we use Number() to convert the strings to actual numeric values.
Character set.We can split on a set of characters—here we have 3 char delimiters, and we split a string on all of them in a single call. We use a character set in the regular expression.
Tip: Be careful to escape metacharacters in the regex. If the pattern does not work, try escaping some characters with the backslash.
Benchmark.Split() takes more time to handle more complex delimiter patterns. Here we time split on a single-char space. We compare this to splitting on non-word characters.
Program 1: The program splits the input string in a tight loop on the space character.
Program 2: The page splits the input string with a non-word character—this is a character class.
Tip: When possible split() on the simplest possible string. Use a regular expression only when needed.
So: If we call split() with the same strings many times, this will execute quickly. Chromium V8 first implemented this optimization.Cache String.prototype.split: mozilla.org
Note: In my testing, avoiding the overhead of split on the first call is still better for performance. The array is a faster syntax.
|Split cache: Optimize the common obfuscator pattern where ["foo","bar","baz"] gets converted fo "foo,bar,baz".split(",").|
Dot Net Perls
|© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, firstname.lastname@example.org.|