Formatting a string in delphi - delphi

I want to update a text like "Updating report (1 0f 5)". I thought format function will help me to do that. I want something like this
string := Format('Updating report ( %d of %d, [1], [2])', loop, count );
but it is not possible. I have an option to have loop and count stored in a string and concatenate everything. But is there any other way to achieve what i want?

Your syntax is wrong. The second parameter to the Format is an open array containing the arguments. So you need to wrap your list of arguments in what is known as an open array constructor.
An open array constructor is a sequence of expressions separated by commas and enclosed in brackets.
So, write the code like this:
str := Format('Updating report (%d of %d)', [loop, count]);

Related

DUNIT X , pass large number of strings

I need to pass a lot of string values to a test procedure, the string parameters are
transferred as a commatext stringlist, code goes like below
[test]
[testcase(test1,'xxxx,yyyy,zzz, ........')]
procedure Test_transmitmany strings(S1, S2, S3, .... Sx String);
if my stringlist gets more than 255 char's I get the error below
[dcc64 Error] Unit_TClass.test.pas(197): E2056 String literals may have at most 255 elements
What is an elegant method to pass many strings to a test case?
I'm also not happy with writing the large stringlist in the testcase definition, looks pretty ugly.
Break the string up into multiple lines with no more than 255 characters on any one line. Then the compiler won't complain.
[testcase(test1,'xxxx,yyyy,zzz,'
+ ' ........')]

Get an array of numbers between A and B?

I'm trying to generate an array of values between A and B, but not finding any easy functions to do so.
GenerateArray(1,5) -> {1,2,3,4,5}
B1=8
B2=11
GenerateArray(B1,B2) -> {8,9,10,11}
Any help would be appreciated. Thanks!
All you need is the sequence function.
=SEQUENCE(B2-B1+1,1,B1,1)
B2-B1+1 will give you the number of rows needed.
https://support.google.com/docs/answer/9368244?hl=en
There is no specific spreadsheet function for that, but you can use Custom Functions.
This does involve some JavaScript and Apps Script, so you may want to look at the tutorials if you want to learn more about it.
Your example
In your Spreadsheet, open the script editor.
Paste this code:
function GenerateArray(a, b) {
// Using a simple JavaScript array to build the output
let output = [];
// For loop to go through all the numbers adding them to the output
for (let i = a; i <= b; i++){
output.push(i)
}
// Building the output as a string to add the curly brackets
output = "{" + output.toString() + "}"
// Returning final output
return output
}
Save the script.
Run it as if it was a normal spreadsheet function.
This is what it looks like in action:
Note that the function does not return a JavaScript array, which are denoted by square brackets. It returns a plain text "string". If you returned a JavaScript array from the function instead of a string, then the result would expand into adjacent columns.
So if you deleted this line:
output = "{" + output.toString() + "}"
The result you would get would be:
Yet for that you might just be better off using the SEQUENCE formula:
=SEQUENCE(1, 10)
Equivalent with native spreadsheet functions
="{" & JOIN(",",SEQUENCE(1,10)) & "}"
This will do the same as the custom function above but only using native spreadsheet functions. It makes use of JOIN to string together numbers from SEQUENCE with a comma in between them (though you can change this), and then concatenates curly braces on it with the & operator.
References and Further Reading
Spreadsheet Custom Functions
Apps Script Tutorials
For Loop
toString()
SEQUENCE
JOIN

Delphi - Formating a multiple %s string with one argument

When I try to format a string such as '%s%s' using a line of code like so:
format('%s%s', [x]);
I get an exception because you can't have multiple '%s' without using an array with the same amount of arguments such as:
format('%s%s', [x,x]);
However, I don't know how many '%s' I will have to format and therefore I don't know how long the array would have to be. I also only want '%s' assigned to only 1 value.
Is there a way in which you can use multiple '%s' and assign them all to the same index?
As described in the documentation you can use an index specifier to identify the argument by a zero based index. The index specifier is written immediately after the % and is followed by a :.
Your example would be:
Format('%0:s%0:s', [x])
MyStr := StringReplace('%s%s', '%s', x, [rfreplaceALL]);

string.format variable number of arguments

Luas string.format is pretty straight forward, if you know what to format.
However, I stuck at writing a function which takes a wildcard-string to format, and a variable number of arguments to put into that blank string.
Example:
str = " %5s %3s %6s %6s",
val = {"ttyS1", "232", "9600", "230400"}
Formatting that by hand is pretty easy:
string.format( str, val[1], val[2], val[3], val[4] )
Which is the same as:
string.format(" %5s %3s %6s %6s", "ttyS1, "232", "9600","230400")
But what if I wan't to have a fifth or sixth argument?
For example:
string.format(" %1s %2s %3s %4s %5s %6s %7s %", ... )
How can I implement a string.format with an variable number of arguments?
I want to avoid appending the values one by one because of performance issues.
The application runs on embedded MCUs.
Generate arbitrary number of repeats of whatever format you want with string.rep if format is the same for all arguments. Or fill table with all formats and use table.concat. Remember that you don't need to specify index of argument in format if you don't want to reorder them.
If you just need to concatenate strings together separated by space, use more suitable tool: table.concat(table_of_strings, ' ').
You can create a table using varargs:
function foo(fmt, ...)
local t = {...}
return t[6] -- might be nil
end
Ps, don't use # on the table if you expect the argument list might contain nil. Instead use select("#", ...).

Delphi search for text after a defined character

I have a string
String :='this is my string | yes';
I need delphi to get whatever the text is after the |
So something like:
getTextAftertoCharacter('|',String);
This should return "yes" in the example above.
Has Delphi get a function for this?
I'm not aware of a single function that does this. I think the easiest way would be to use Pos and then Copy:
answer := Copy(str, Pos('|', str) + 1, Length(str));
Start character is at Pos('|', str) + 1, amount of characters to copy is actually Length(str)-Pos('|', str), but passing a greater value to Copy also works.
Note: this will return the whole contents of str if there is no '|'. Check for Pos('|', str) being non-zero if you need different behavior.
You can use Controls.GetLongHint():
GetLongHint(String);
That's because the separator of a two part hint is a |. In your example it would return ' yes', with the leading space. If | is not found, the function returns the String.
I'd use then following code:
Trim(RightStr(Text, Length(Text)-LastDelimiter('|', Text)));
It'll locate the last delimiter in the string and I used Trim to get rid of the whitespace. LastDelimiter can take more than one delimiter which may be usefull. If no delimiter is found it will return the whole string.

Resources