1 The `substr()` function extracts a portion of the given input string, 2 specified by offset and length. and returns the resulting substring. 3 4 If neither an offset, nor a length argument are provided, a copy of 5 the entire input string is returned. 6 7 If just an offset is specified, the entire remainder of the input string 8 after the specified offset is returned. 9 10 If both an offset and a length are specified, then that much characters 11 of the string are extracted, beginning at the offset. 12 13 If either offset or length are negative, they're counted towards the end 14 of the string. If either value exceeds the input string length, it is 15 capped to the length. 16 17 Returns the resulting substring. 18 19 Returns `null` if the given input value is not a string. 20 21 22 -- Testcase -- 23 {% 24 printf("%.J\n", [ 25 // extract entire string 26 substr("Hello world!"), 27 28 // extract anything after the 3rd character 29 substr("Hello world!", 3), 30 31 // extract the last 6 characters 32 substr("Hello world!", -6), 33 34 // extract chars 5-8 35 substr("Hello world!", 4, 3), 36 37 // extract characters 8-10 38 substr("Hello world!", -5, -2), 39 40 // overlong values are capped 41 substr("Hello world!", 100), 42 substr("Hello world!", 0, 100), 43 substr("Hello world!", 100, 100), 44 45 // invalid offset or length values are treated as 0 46 substr("Hello world!", "inval"), 47 substr("Hello world!", "inval", "inval") 48 ]); 49 %} 50 -- End -- 51 52 -- Expect stdout -- 53 [ 54 "Hello world!", 55 "lo world!", 56 "world!", 57 "o w", 58 "orl", 59 "", 60 "Hello world!", 61 "", 62 "Hello world!", 63 "" 64 ] 65 -- End -- 66 67 68 Supplying an invalid input string value will yield `null`. 69 70 -- Testcase -- 71 {% 72 printf("%.J\n", substr(true, 0, 1)); 73 %} 74 -- End -- 75 76 -- Expect stdout -- 77 null 78 -- End --
This page was automatically generated by LXR 0.3.1. • OpenWrt