1 The `split()` function breaks the given string into multiple substrings, 2 using the given separator value. 3 4 The separator may be either a string or a regular expression value. 5 6 Returns an array containing the resulting parts. 7 8 Returns `null` if the given input value is not a string or if the separator 9 argument is neither a string nor a regular expression. 10 11 12 -- Testcase -- 13 {% 14 print(join("\n", [ 15 // split by string 16 split("foo|bar|baz", "|"), 17 18 // split by regexp 19 split("apples, bananas and strawberries are fruits", /, | and | are | /), 20 21 // splitting an empty string yields an array containing one empty string 22 split("", "|"), 23 split("", ""), 24 split("", /\s+/), 25 26 // splitting with an empty string as separator yields an array containing 27 // all characters individually 28 split("foo|bar|baz", ""), 29 split("foo|bar|baz", /()/), 30 31 // splitting on a separator not found within the string will yield an 32 // array containing the entire string as sole element 33 split("foo|bar|baz", "xxx"), 34 split("foo|bar|baz", /\d+/), 35 36 // subsequent separators are not coalesced 37 split("abc|||def", "|"), 38 split("foo1bar23baz", /[[:digit:]]/), 39 40 // leading and trailing empty substrings are retained 41 split("|abc|def|", "|"), 42 split(",foo;bar:", /[,;:]/), 43 44 // subject and split strings handle embedded \0 45 split("foo=1\0bar=2\0baz=3", "\0"), 46 47 // supplying a limit only splits the string into that many parts 48 split("foo=1=2=3", "=", 2), 49 50 // limit of one produces a result array conaining the entire string as sole item 51 split("foo=1=2=3", "=", 1), 52 53 // negative limit yields an empty result array 54 split("foo=1=2=3", "=", -1), 55 56 // zero limit yields an empty result array 57 split("foo=1=2=3", "=", 0), 58 ]), "\n"); 59 %} 60 -- End -- 61 62 -- Expect stdout -- 63 [ "foo", "bar", "baz" ] 64 [ "apples", "bananas", "strawberries", "fruits" ] 65 [ "" ] 66 [ "" ] 67 [ "" ] 68 [ "f", "o", "o", "|", "b", "a", "r", "|", "b", "a", "z" ] 69 [ "f", "o", "o", "|", "b", "a", "r", "|", "b", "a", "z" ] 70 [ "foo|bar|baz" ] 71 [ "foo|bar|baz" ] 72 [ "abc", "", "", "def" ] 73 [ "foo", "bar", "", "baz" ] 74 [ "", "abc", "def", "" ] 75 [ "", "foo", "bar", "" ] 76 [ "foo=1", "bar=2", "baz=3" ] 77 [ "foo", "1=2=3" ] 78 [ "foo=1=2=3" ] 79 [ ] 80 [ ] 81 -- End -- 82 83 84 Supplying an invalid input string value will yield `null`. 85 86 -- Testcase -- 87 {% 88 printf("%.J\n", split(true, "u")); 89 %} 90 -- End -- 91 92 -- Expect stdout -- 93 null 94 -- End -- 95 96 97 Supplying a non-string, non-regexp separator will yield `null`. 98 99 -- Testcase -- 100 {% 101 printf("%.J\n", split("null true false", true)); 102 %} 103 -- End -- 104 105 -- Expect stdout -- 106 null 107 -- End --
This page was automatically generated by LXR 0.3.1. • OpenWrt