C# Regex.Split: Removing empty results

Regex lineSplitter = new Regex(@”[\s*\*]*\|[\s*\*]*”); var columns = lineSplitter.Split(data).Where(s => s != String.Empty); or you could simply do: string[] columns = data.Split(new char[] {‘|’}, StringSplitOptions.RemoveEmptyEntries); foreach (string c in columns) this.textBox1.Text += “[” + c.Trim(‘ ‘, ‘*’) + “] ” + “\r\n”; And no, there is no option to remove empty entries for RegEx.Split as … Read more

Java split String performances

String.split(String) won’t create regexp if your pattern is only one character long. When splitting by single character, it will use specialized code which is pretty efficient. StringTokenizer is not much faster in this particular case. This was introduced in OpenJDK7/OracleJDK7. Here’s a bug report and a commit. I’ve made a simple benchmark here. $ java … Read more

Split a string with two delimiters into two arrays (explode twice)

You can explode the string by commas, and then explode each of those values by x, inserting the result values from that into the two arrays: $str = “20×9999,24×65,40×5”; $array1 = array(); $array2 = array(); foreach (explode(‘,’, $str) as $key => $xy) { list($array1[$key], $array2[$key]) = explode(‘x’, $xy); } Alternatively, you can use preg_match_all, matching … Read more

JavaScript: split doesn’t work in IE?

you could add the code below to you program and it will work. var split; // Avoid running twice; that would break the `nativeSplit` reference split = split || function (undef) { var nativeSplit = String.prototype.split, compliantExecNpcg = /()??/.exec(“”)[1] === undef, // NPCG: nonparticipating capturing group self; self = function (str, separator, limit) { // … Read more