How to check if a string contains a substring in Delphi? - delphi

String content = "Jane";
String container = 'A.Sven,G.Jane,Jack'; // This is the string which i need to be searched with string content
boolean containerContainsContent = StringUtils.containsIgnoreCase(container, content); // I used to write like this in java
I am new to Delphi. Is there a contains command in Delphi or any other command which performs the same operation?

You can use the functions in StrUtils in Delphi
uses
StrUtils;
..
if ContainsText('A.Sven,G.Jane,Jack', 'Jane') then
...
ContainsText returns true if the subtext is found, without case-sensitivity, in the given text
In StrUtils you'll also find handy functions like StartsText, EndsText and ReplaceText

You might also find helpful the Contains Function in System.SysUtils as below.
uses
Sysytem.SysUtils;
....
txt := 'This is a string variable';
if txt.contains('str') then
....

Related

Why does Gambas give me an error after I Dim a variable after a function call?

I'm playing around with gambas.
This code gives me the error "unexpected dim in FMain.class:6"
Public Sub Form_Open()
Print "this won't work"
Dim nickname As String = "gambas"
Print "Your new name is " & nickname
End
This code doesn't, and runs fine:
Public Sub Form_Open()
Dim nickname As String = "gambas"
Print "Your new name is " & nickname
End
Does gambas have requirements where variables are declared like pascal? I can't find any mention of it in the documentation. Thanks.
Gambas requires all DIM statements to be placed before any executable code inside a function or Subroutine (emphasis mine):
http://gambaswiki.org/wiki/lang/dim
All DIM declarations must be in the FUNCTION or SUB before the first executable command.
So change your code to this:
Public Sub Form_Open()
Dim nickname As String = "gambas"
Print "this will work"
Print "Your new name is " & nickname
End
Gambas' requirement for forward declaration of all local variables is very old-school. Sometimes it does make it easier to make self-documenting code and it incentivizes making functions short, but if a function has many intermediate short-lived local variables that cannot be immediately initialized (e.g. inside nested loops inside a function) then it hinders readability. YMMV.
This is not required anymore since Gambas 3.12.
But I suggest to continue declaring variables at the top function. It makes the code far more readable two years later.

What is the Delphi equivalent for LPLONG?

I have to access several functions of a DLL written in c from Delphi (currently Delphi7).
I can do it without problems when the parameters are scalar
(thanks to the examples found in this great site!), but I have been stuck for some time when in the parameters there is a pointer to an array of Longs.
This is the definition in the header file of one of the functions:
BOOL __stdcall BdcValida (HANDLE h, LPLONG opcl);
(opcl is an array of longs)
And this is a portion of my Delphi code:
type
TListaOpciones= array of LongInt; //I tried with static array too!
Popcion = ^LongInt; //tried with integer, Cardinal, word...
var
dllFunction: function(h:tHandle; opciones:Popcion):boolean;stdcall;
arrayOPciones:TListaOpciones;
resultado:boolean;
begin
.....
I give values ​​to aHandle and array arrayOPciones
.....
resultado:=dllFunction(aHandle, #arrayopciones[0]);
end;
The error message when executing it is:
"Project xxx raised too many consecutive exceptions: access violation
at 0x000 .."
What is the equivalent in Delhpi for LPLONG? Or am I calling the function in an incorrect way?
Thank you!
LONG maps to Longint, and LPLONG maps to ^Longint. So, you have translated that type correctly.
You have translated BOOL incorrectly though. It should be BOOL or LongBool in Delphi. You can use either, the former is an alias for the latter.
Your error lies in code or detail we can't see. Perhaps you didn't allocate an array. Perhaps the array is incorrectly sized. Perhaps the handle is not valid. Perhaps earlier calls to the DLL failed to check for errors.

F# constructor with <"string", str>

In this article it shows how to use the SqlCommandProvider type. The sample code has this:
use cmd = new SqlCommandProvider<"
SELECT TOP(#topN) FirstName, LastName, SalesYTD
FROM Sales.vSalesPerson
WHERE CountryRegionName = #regionName AND SalesYTD > #salesMoreThan
ORDER BY SalesYTD
" , connectionString>(connectionString)
what does the <... ,...> before the type constructor name mean and why the the
first parameter have to be a string literal? It looks like a generic but it's taking variables not types. The constructor seems to be taking in a connection string already in the <> section.
The angle brackets are the configuration for a type.
In your example, you are defining a type and creating an instance at the same type. It's clearer when the steps are separated.
Define a type.
type SalesPersonQuery = SqlCommandProvider<query, connectionString>
But to actually have an instance of the type you have to create it:
let command = new SalesPersonQuery()
Now you can use the command.Execute() rather then SalesPersonQuery.Execute().
The reason there is a constructor is because later on (at run-time) you can change the connection string to a different then the one provided in the definition, so for instance:
let command = new SalesPersonQuery(differentConnectionString)
You can find that in the documentation in configuration section:
Connection string can be overridden at run-time via constructor optional parameter
First parameter can be a path to a SQL script or a SQL query. I suppose that's the reason it's a string: how else would you like to define a SQL query?
Again, from the documentation:
Command text (sql script) can be either literal or path to *.sql file

Delphi DLL-use functions designed to return a set of values Integer Record

Currently need to use Delphi to write a DLL, so that the main program calls to the specified place in the removable disk in a file,
Main program designed to VC + +, so use Strut way to call the DLL's data as round!!
Current problems encountered when the main program calls my DLL, the incoming group A Record, and other functions has been dealt with, to return the group B Record,
But using Delphi written DLL, can receive group A Record, but returned group B, but always errors!!
The following is the code for the DLL function, would like to ask if anyone encountered such problems can help point to mention turned a deaf ear
Thanks! !
enter code here
library usbdll;
uses
Windows,
SysUtils,
Classes;
{$R *.res}
Type
p_fin=^TSfin;
TSfin = record //A group of Record is the main program calls incoming Record Type
ST_act:Integer;
pathlen:Integer;//Pass true path length, so that I can get to the far left pathlen, then to remove the rear garbled
Id_hand:Integer;
Id_tail:Integer;
path: PWideChar://The reason why the file path Pwidechar do use guidelines because another branch dll is passed to the main program main program <file path + unicode>, is behind the path and dragging a bunch of gibberish characters
Type
p_out=^TRfout;//B Record is set to return to the main program of the Record Type
TRfout= Record
ST_act:Integer;
ST_move:Integer;
Revis:Integer;
Crchk:Integer;
end;
//The following is my comment out.
// The use of the test in two ways, directly back to the group B Record, does not receive group A Record,A group that does not receive Record, when the main program a call, immediately return the relevant data, the results are normal.
(*
function RFoutEt(test:p_out):Boolean;stdcall; //ok Function writing mode
begin
test^.ST_act:=14;
test^.ST_move:=10;
test^.Revis:=12;
test^.Crchk:=8;end;exports RFoutEt;
procedure RFoutE(out Result:TRfout);cdecl; //ok Procedure writing mode
begin
Result.ST_act:=14;
Result.ST_move:=10;
Result.Revis:=12;
Result.Crchk:=8;end;exports RFoutEt;
*)
// Actually, I need to charge the main program to my group A Record datain order to deal with post-op, get really want to move the file to specify the true path,and ultimately return to group B Record.
function RFoutE(ap_sendin:p_fin;num:Integer):TRfout;stdcall; //error
var
str_tmp,str_tmp2,temi_diry:string;
i,copyNum:Integer;
arr: array[0..100] of Char;
begin
//Program by adding the following {} after paragraph, Result is not an empty value is displayed to access illegal address,causing abnormal program termination.
{
StrCopy(arr,Pchar(ap_sendin^.path));
repeat
str_tmp:=temi_diry;//Use the file path string char array A group referred to in the PWidechar removed
str_tmp2:=arr[i];
Inc(i);
until i>=ap_sendin.pathlen;
copyNum:=Prs_Filecopy(temi_diry;ap_sendin^.path);//A group of Record with associated data to complete the move of the specified file
}
Result.ST_act:=4;//The following four lines of words alone are able to return data
Result.ST_move:=0;
Result.Revis:=2;
Result.Crchk:=copyNum;end;
PS. Following is a test using VC + + to try more than one function is normal demand
struct Sfin{
int ST_act;
int pathlen;
int Id_hand;
int Id_tail;
wchar_t *path;
};
struct Rfout{
int ST_act;
int ST_move;
int Revis;
int Crchk;
};
Rfout RFoutE(struct Sfin *a, int num)
{
int ret = 1;
Rfout OutStruct;
copyNum = Prs_Filecopy(temi_diry, inAnow, Anow->path);
ret=1;
if(ret==1){
OutStruct.ST_act =14;
OutStruct.ST_move =10;
OutStruct.Revis = 12;
OutStruct.Crchk = 8;
Anow = freeA(Anow);
}
return OutStruct;
}
There is no standard ABI for larger than machine word sized return values. And Delphi uses a different ABI from any other compiler that I've encountered so you'll have no luck returning large records that way.
You'll need to return the record as an out parameter rather than a function return value. Once you make that change, all will be well.
It also looks like your C++ functions use cdecl rather than stdcall.

Can record constructors make record constants more concise?

I have some tabular data:
Foo Bar
-------------
fooes 42
bars 666
...
So, I declare the entity structure:
type TFoo = record
Foo: string;
Bar: Integer
end;
and the table of entities:
const FOOES = array [M..N] of TFoo = (
// Have to specify the field names for each record...
(Foo: 'fooes'; Bar: 42),
(Foo: 'bars'; Bar: 666)
{ so on }
);
As you see, this looks quite verbose and redundant, and it is because I initialize all of the fields for all of the records. And there is a lot of editing if I copy tabular data prepared elsewhere. I'd prefer to not enumerate all of the fields and stick to the more laconic C style, that is, constants only. And here comes the record constructor...
Can record constructors help me in this case?
Here's an example in C. You'll notice that we don't have to specify the field names in each declaration:
#include <stdio.h>
typedef struct {
char foo[10];
int bar;
} foo;
int main(void) {
/* Look here */
foo FOOES[2] = {{"foo", 42}, {"bar", 666}};
int i = 0;
for (; i < 2; i++) {
printf("%s\t%d\n", FOOES[i].foo, FOOES[i].bar);
}
return 0;
}
A const is just a read-only var which is loaded/mapped within the code, when the executable is launched.
You can create a var record (or a const but overriding the writable const option), then initialize it in the initialization section of the unit.
var FOOES = array [M..N] of TFoo;
....
initialization
SetFooArray(FOOES,['fooes',42,'bar',230]);
...
end.
The custom SetFooArray() function will put all array of const parameters into FOOES.
I use this technique sometimes to initialize computable arrays (e.g. conversion or lookup tables). Sometimes, it does make sense to compute once at startup a huge array, saving some KB of const in the source code, with a few lines of code.
But I'm not sure it will be worth it in your case. The default const declaration is a bit verbose, but not a problem if you use Ctrl+C/Ctrl+V or a find and replace. It is the most standard, is secure if you change later the record layout (whereas the C construction may compile without error), and will create a true constant.
Record constructors are runtime only and so for constants your current solution is the only option.
If you want it done in source then what you have already typed is your answer. You could, of course, put the data in separate arrays and initialize them that way, but that can make your code look messy.
You could also store them in an text file (Foo=Bar format) and read them into a TStringList at run-time (SL.LoadFromFile()). But even with a sorted TStringList it will be far less efficient (MyVariable := SL.Values['Foo1']; for example).
There are a million ways to solve this problem outside of source code. Taking it from the other direction, put the data into Excel and create an Excel macro to build the source and put it into the clipboard to paste into your PAS file. This wouldn't be too difficult and probably easier than formatting the Delphi code within the IDE.

Resources