OPaC:Documents: Class OPString


Class OPString is the standard class used to represent and manipulate strings in OPaC. Internally, strings can be represented both with ISO Latin-1 characters, or with 16-bit Unicode characters. Unicode should be preferred wherever possible, even if OPaC provides a very primitive conversion between the two representations.

Strings with arguments

When a string with multiple arguments must be resolved, don't use sprintf and its friends, since this might be difficult to localise, since the order of the arguments might be locale specific. OPString provides an easy way to resolve a text with arguments :

OPStringRef message;

Count num  = 10;
Real  size = 430.6; 

message = OPString::MakeArguments ("%2%KB in %1% files") (num) (size);
// => "430.6KB in 10 files"

message = OPString::MakeArguments ("%1% fichiers, %2% KB") (num) (size);
// => "10 fichiers, 430.6 KB"

The member function OPString::MakeArguments takes a string with placeholders numbered from %1% to %n%, in an arbitrary order. It returns a temporary object of type OPString::ArgDesc which implements operator () and does the real argument substitution in the original string.

The arguments can be numeric (Int64, Card64 or Real) or textual (OPStringRef). The numeric arguments can be formatted arbitrarily. The format specification is the same as for class OPConvert.

You may also replace a specific argument in a string by calling its ReplaceArg method. This looks for the %n% substring, removes it and inserts the argument at its position.