Jump to content

String with parameters in it à la "printf"


This topic is 5355 days old. Please don't post here. Open a new topic instead.

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by Guest
Link to comment
Share on other sites

This topic is 5355 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.