Recent

Author Topic: Strings with parameters  (Read 2522 times)

Vodnik

  • Full Member
  • ***
  • Posts: 210
Strings with parameters
« on: January 04, 2019, 05:14:39 pm »
I'm looking for the feature which name I don't know.
The idea is to have a string with parameters like:
"There were found %n occurrences of %w in file %f".
Parameters %n, %w and %f are set somehow during run-time.
When string is output, % parameters are replaced by their values:
"There were found 3 occurrences of 'image' in file text.txt".
Similar functionality is possible for SQL commands in TSQLQuery.
What about common strings?
Well, StringReplace can be used, but maybe there is something special already designed?


balazsszekely

  • Guest
Re: Strings with parameters
« Reply #1 on: January 04, 2019, 05:26:00 pm »
Code: Pascal  [Select][+][-]
  1. ShowMessage(Format('There were found %s occurrences of %s in file %s', ['12', '"test"', 'text.txt']));

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Strings with parameters
« Reply #2 on: January 04, 2019, 06:26:52 pm »
Or in this case


Code: Pascal  [Select][+][-]
  1. ShowMessageFmt('There were found %s occurrences of %s in file %s', ['12', '"test"', 'text.txt']);

But GetMem has shown you the string formatting routine you need.

See here.

« Last Edit: January 04, 2019, 06:36:48 pm by howardpc »

Vodnik

  • Full Member
  • ***
  • Posts: 210
Re: Strings with parameters [SOLVED]
« Reply #3 on: January 04, 2019, 06:36:47 pm »
Thank's a lot, Getmem and Howardpc!
Placeholder - that's the key word, giving useful search results
« Last Edit: January 04, 2019, 07:10:21 pm by Vodnik »

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: Strings with parameters
« Reply #4 on: January 04, 2019, 10:31:18 pm »
It seems like you're need named parameters (as presented in example), but please take in account, that

  * Format relies on exact order (and type) of parameters.
  * TParams (this is what is used for SQL) works with named parameters, but intended to work with SQL.
  * There are some template engines, which of course work with named parameters.

But the last two seems like an overkill for your task (as presented in example) and I'd rather go with either Format or your own simple Search/Replace - based code.
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Strings with parameters
« Reply #5 on: January 04, 2019, 10:56:24 pm »
printf and sprint I think will do the same for you.
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Strings with parameters
« Reply #6 on: January 04, 2019, 11:59:05 pm »
even a proper writeln or writestr would do. Even easier.
And there is a template engine for story tellers (great fun)
« Last Edit: January 05, 2019, 12:01:47 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: Strings with parameters
« Reply #7 on: January 05, 2019, 05:54:06 pm »
printf and sprint I think will do the same for you.
But they require you to use a C library and they also can't check for the correct amount of arguments, instead they'll simply access whatever memory is there if too few arguments are passed.
So if in Pascal use the power of the Format() function.

 

TinyPortal © 2005-2018