split
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 string at nth occurrence of a given character
>>> n = 2 >>> groups = text.split(‘_’) >>> ‘_’.join(groups[:n]), ‘_’.join(groups[n:]) (’20_231′, ‘myString_234’) Seems like this is the most readable way, the alternative is regex)
Splitting a string with multiple spaces
Since the argument to split() is a regular expression, you can look for one or more spaces (” +”) instead of just one space (” “). String[] array = s.split(” +”);
Java replace all square brackets in a string
The replaceAll method is attempting to match the String literal [] which does not exist within the String try replacing these items separately. String str = “[Chrissman-@1]”; str = str.replaceAll(“\\[“, “”).replaceAll(“\\]”,””);
How to count the number of lines of a string in javascript
Using a regular expression you can count the number of lines as str.split(/\r\n|\r|\n/).length Alternately you can try split method as below. var lines = $(“#ptest”).val().split(“\n”); alert(lines.length); working solution: http://jsfiddle.net/C8CaX/
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