Delphi has something like javascript "template literals"? - delphi

Template literals are string literals allowing use multi-line strings, eg.:
const MYSTRING = `string text line 1
string text line 2
string text line 3`;
on Delphi my actual approach is:
const MYSTRING = 'string text line 1 '+
'string text line 2 '+
'string text line 3';
has Delphi something like javascript "template literals"?

There is no such thing in Delphi. Your current approach using the + operator is the best you can do.

If you want to include variables and values which is the primary use of JS template literals, you can also consider the Format function:
http://docwiki.embarcadero.com/Libraries/Tokyo/en/System.SysUtils.Format

There are no multi-code-line constants in Delphi. You can either use the + operator to concatenate multiple lines with the sLineBreak system constant like this:
MYSTRING = 'line 1' + sLineBreak + 'line 2' + sLineBreak + 'line 3';
(You can format that across multiple lines if you want) or if you don't need cross-platform code you can do this:
MYSTRING = 'line 1'#13#10'line 2'#13#10'line 3';
When using literal characters, you don't have to use the + operator.

Related

Delphi 5 Use Pos / PosIgnoreCase for whole words only

I know Delphi 5 is really old but I have no other choice for now because my employer doesn't want to change, so I am stuck with old functions etc.
I would like to know if there was a way to get the position of the whole words I am looking for:
I have a list of words (if, then, else, and etc) named KEYWORDS, and for each word in it I have to check in every .pas file if this word contains some uppercase characters.
On my code, I am reading each line, and for each line, I am using this to find if I find any word in the list and if it has some uppercase characters:
if(PosIgnoreCase(KEYWORDS[I], S) <> Pos(KEYWORDS[I], S)) //Then the keyword has some uppercases in this line and I must raise an error
My problem is that if I use some words that contains the keywords ( for example "MODIFICATION") this will detect the uppercase IF in it and raise an error
I tried using if(PosIgnoreCase(' ' + KEYWORDS[I] + ' ', S) <> Pos(' ' + KEYWORDS[I] + ' ', S))
but there may be some parentheses or other characters instead of the spaces so I would like to avoid making a new condition for each character.
Is there a clean way to do it ? I found myself struggling quite often with the lack of functions in Delphi 5
Sorry if my question is somewhat confusing, english is not my first language.
Thank you for your time.
Update (from comments):
My list of keywords only contains the reserved keywords on Delphi

How to use Alt Codes?

I need to build a string which contains Alt Codes, specifically Alt-16 (Arrow symbol). I have a line of text (aka a string). I append a Carriage return, then want an ARROW symbol and the new line of text. This line will then be passed to PPT. If I manually go into PPT, into a text box, I can hit Alt+16, and get the arrow symbol. This is what I programatically want to do.
Alt symbols found here.
Here is what I am trying, but it gives me a totally different symbol.
line := line + #13 + Chr(VK_MENU) + #16 + NewLine;
How do I build a string with ALT Codes as part of the string?
Not that those characters are not called Alt-codes or Alt-characters. The Alt-codes are just a way to type some special character, but they can't be used as such in a string.
You can just type in that character using the Alt-code, or copy it from the alt codes website. You can use the character as-is in the string. The code below would work fine:
// Show it
ShowMessage('►');
// Or use it in your string:
line := line + #13 + '►' + NewLine;

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.

Using of unicode with a TStringGrid

I`d like to define a string constant with mixed greek and cyrillic symbols. Something like this
const
some_const = 'cyrillic symbols' + $03C9;
where $03C9 is the lower case letter omega from there.
Maybe, I`ll should make some_const a variable in a datamodule and initialize it.
//datamodule
var
some_const = 'cyrillic symbols' + some_function_to_make_string($03C9);
So, what is a correct function for some_function_to_make_string(code : Word)?
Can I use this some_const with a TStringGrid.Cells[aCol, aRow]?

Removing '#$A' from Delphi string

I am modifying a delphi app.In it I'm getting a text from a combo box. The problem is that when I save the text in the table, it contains a carriage return. In debug mode it shows like this.
newStr := 'Projector Ex320u-st Short Throw '#$A'1024 X 768 2700lm'
Then I have put
newStr := StringReplace(newStr,'#$A','',[rfReplaceAll]);
to remove the '#$A' thing. But this doesn't remove it.
Is there any other way to do this..
Thanks
Remove the quotes around the #$A:
newStr := StringReplace(newStr,#$A,'',[rfReplaceAll]);
The # tells delphi that you are specifying a character by its numerical code.
The $ says you are specifying in Hexadecimal.
The A is the value.
With the quotes you are searching for the presence of the #$A characters in the string, which aren't found, so nothing is replaced.
Adapted from http://www.delphipages.com/forum/showthread.php?t=195756
The '#' denotes an ASCII character followed by a byte value (0..255).
The $A is hexadecimal which equals 10 and $D is hexadecimal which equals 13.
#$A and #$D (or #10 and #13) are ASCII line feed and carriage return characters respectively.
Line feed = ASCII character $A (hex) or 10 (dec): #$A or #10
Carriage return = ASCII character $D (hex) or 13 (dec): #$D or #13
So if you wanted to add 'Ok' and another line:
Memo.Lines.Add('Ok' + #13#10)
or
Memo.Lines.Add('Ok' + #$D#$A)
To remove the control characters (and white spaces) from the beginning
and end of a string:
MyString := Trim(MyString)
Why doesn't Pos() find them?
That is how Delphi displays control characters
to you, if you were to do Pos(#13, MyString) or Pos(#10, MyString) then it
would return the position.

Resources