splitting a string based on multiple char delimiters

Use string.Split(char []) string strings = “4,6,8\n9,4”; string [] split = strings .Split(new Char [] {‘,’ , ‘\n’ }); EDIT Try following if you get any unnecessary empty items. String.Split Method (String[], StringSplitOptions) string [] split = strings .Split(new Char [] {‘,’ , ‘\n’ }, StringSplitOptions.RemoveEmptyEntries); EDIT2 This works for your updated question. Add all … 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

Measuring the length of string containing wide characters

As documented: The Length property returns the number of Char objects in this instance, not the number of Unicode characters. The reason is that a Unicode character might be represented by more than one Char. Use the System.Globalization.StringInfo class to work with each Unicode character instead of each Char. Getting length: new System.Globalization.StringInfo(“友𠂇又”).LengthInTextElements Getting each … Read more

Splitting a math expression string into tokens in Python

You should split on the character set [+-/*] after removing the whitespace from the string: >>> import re >>> def mysplit(mystr): … return re.split(“([+-/*])”, mystr.replace(” “, “”)) … >>> mysplit(“A7*4”) [‘A7’, ‘*’, ‘4’] >>> mysplit(“Z3+8”) [‘Z3’, ‘+’, ‘8’] >>> mysplit(“B6 / 11”) [‘B6′, “https://stackoverflow.com/”, ’11’] >>>