String handling functions
-
I realise that there's probably bigger fish to fry at the moment, but this is a wish list section and I feel this would greatly extend conditional g code functionality.
I'd like to see a few string handling routines built into RRF.
Each on their own bay have limited use, but together become quite powerful.
They would all be recognised as available in most programming languages though the syntax would differ.These would be
length("string") - return the length of a string
e.g. length(0:/sys/config.g) = 14pos("pattern","string") - return the index of the first occurrence of a substring within a string.
Return -1 if not founde.g pos(".","0:/sys/config.g") = 12 (zero based)
copy("string",index,"nnn") - return a substring containing "nnn" characters starting at index (zero based)
e.g. copy("my_string",3,3) = "str"
leftStr("string",nnn) - return a string of "nnn" length starting at the first character
e.g. left("my_string",4) = "my_s"rightStr("string",nnn) - same as above but return the last "nnn" characters
strReplace("substring","new string", "string",replaceParams) - replaces occurrences of a substring with another.
Parameters to replace all, and ignore case.e.g. strReplace("P6","B7","z2P6p6macro.g"[replaceAll,IgnoreCase] = "z2B7B7macro.g"
or by changing parameters it could also be
z2B7p6macro.gupperCase("string") - return a string in all caps
e.g upperCase("myString") = "MYSTRING"lowerCase("string") - same except return all lowercase
-
@owend said in String handling functions:
lowerCase("string") - same except return all lowercase
Any use cases?
Yes, I know, it's a common push back technique.
-
@zapta
Most of the use cases I've had would be for making code more portable through not hard coding things.
It's been a case of doing something and thinking if I could just extract that part of the string, or change it to that, then I could do it this way.
In each case I've just moved on and done what I had to do within the existing framework.
As I said, each on their own they're less powerful. -
@owend, it may be helpful in the use case below, splinting the terms and converting them to numbers
https://forum.duet3d.com/topic/27201/macro-parameter-letters