September 15, 200916 yr Hi, Is there any function that can return a string in which there are some parameters? Like a printf in C. Example of what the function could look like : C style printf("The sum of %d and %d is %d", 2, 5, 7) FM style printf(myString, lstParameters) I guess the in FM the function should receive a list instead of parameters. BTW, I know that printf doesn't return a string... Thanks
September 15, 200916 yr FileMaker doens't have any native functions for that purpose (as I can recall). The easy way might be to use a Substitute function: Let([ a = 2; b = 5; c = 7 ]; Substitute( "The sum of _a and _b is _c" ; [ "_a"; a ] ; [ "_b"; b ] ; [ "_c"; c ] ) But that isn't very dynamic. You'll need to use a recursive function or CustomList to do this with a dynamic amount of placeholders. I'm not aware of any pre-existing custom functions for this either, but I haven't checked. The place to check is in the libraries at BrianDunning.com and fmFunctions.com. Here is an example: /* printf( string ; placeholder ; replacementValues ) */ Let([ sPlaceholder = Position( string; placeholder; 1; 1 ) ; stringNew = Replace( string ; sPlaceholder ; Length( placeholder ) ; GetValue( replacementValues; 1 ) ) ]; Case( //--Another placeholder and value exist-- Position( string; placeholder; 1; 2 ) > 0 and ValueCount( replacementValues ) > 1 ; printf( stringNew ; placeholder ; RightValues( replacementValues ; ValueCount( replacementValues ) - 1 ) ) //--Additional branches for error trapping can be added here-- //--Else, return result-- ; stringNew ) //End Case ) //End Let Is that what you're looking for? I haven't really tested this, but it might be enough to get your started.
September 15, 200916 yr Author Yes it is. I will try and tweak it this week and get back with the results. Thanks
September 18, 200916 yr Author Your function seems to work quite well. I think you should consider publishing it on fmfunctions or/and briandunning site. In my case, this function will be useful in a multilingual project. Especially, where I need to replace some values inside a string. Thanks! Edited September 18, 200916 yr by Guest
September 18, 200916 yr I'm glad it's working for you! I normally wouldn't post a function until it's been proven in production or heavy usage, but I might take your recommendation - this one seems harmless enough.
Create an account or sign in to comment