Hi, Readers.
Today I would like to share another mini tip about Business Central, how to quickly get Longest Common Substring in AL.
In computer science, a longest common substring of two or more strings is a longest string that is a substring of all of them. For example, the strings “ABABC”, “BABCA” and “ABCBA” have only one longest common substring, viz. “ABC” of length 3. Other common substrings are “A”, “AB”, “B”, “BA”, “BC” and “C”. More details in wiki: Longest common substring
This isn’t that simple if we write it from scratch in AL, so is there any easy way? Yes. There is a standard method.
codeunit 1251 “Record Match Mgt.” -> procedure GetLongestCommonSubstring
Let’s look at a simple example. I create a test page, enter two strings and get the longest common substring.
Test01:
Test02:
Source code: Github
page 50200 "Get Longest Common Substring"
{
PageType = Card;
ApplicationArea = All;
UsageCategory = Administration;
layout
{
area(Content)
{
group(Input)
{
field(FirstString; FirstString)
{
Caption = 'First String';
ApplicationArea = All;
trigger OnValidate()
begin
if FirstString = '' then
LongestCommonSubstring := '';
GetLongestCommonSubstring();
end;
}
field(SecondString; SecondString)
{
Caption = 'Second String';
ApplicationArea = All;
trigger OnValidate()
begin
if SecondString = '' then
LongestCommonSubstring := '';
GetLongestCommonSubstring();
end;
}
}
group(Result)
{
field(LongestCommonSubstring; LongestCommonSubstring)
{
Caption = 'Longest Common Substring';
ApplicationArea = All;
Editable = false;
}
}
}
}
var
FirstString: Text;
SecondString: Text;
LongestCommonSubstring: Text;
local procedure GetLongestCommonSubstring()
var
RecordMatchMgt: Codeunit "Record Match Mgt.";
begin
if (FirstString <> '') and (SecondString <> '') then
LongestCommonSubstring := RecordMatchMgt.GetLongestCommonSubstring(FirstString, SecondString);
end;
}
It’s very simple, so what should you do if you want to input 3 strings? Because this is an intersection, we only need to execute procedure GetLongestCommonSubstring once more. For example,
Test:
Great, give it a try!!!😁
Source code: Github
page 50200 "Get Longest Common Substring"
{
PageType = Card;
ApplicationArea = All;
UsageCategory = Administration;
layout
{
area(Content)
{
group(Input)
{
field(FirstString; FirstString)
{
Caption = 'First String';
ApplicationArea = All;
trigger OnValidate()
begin
if FirstString = '' then
LongestCommonSubstring := '';
GetLongestCommonSubstring();
end;
}
field(SecondString; SecondString)
{
Caption = 'Second String';
ApplicationArea = All;
trigger OnValidate()
begin
if SecondString = '' then
LongestCommonSubstring := '';
GetLongestCommonSubstring();
end;
}
field(ThirdString; ThirdString)
{
Caption = 'Third String';
ApplicationArea = All;
trigger OnValidate()
begin
if SecondString = '' then
LongestCommonSubstring := '';
GetLongestCommonSubstring();
end;
}
}
group(Result)
{
field(LongestCommonSubstring; LongestCommonSubstring)
{
Caption = 'Longest Common Substring';
ApplicationArea = All;
Editable = false;
}
}
}
}
var
FirstString: Text;
SecondString: Text;
ThirdString: Text;
LongestCommonSubstring: Text;
local procedure GetLongestCommonSubstring()
var
RecordMatchMgt: Codeunit "Record Match Mgt.";
begin
if (FirstString <> '') and (SecondString <> '') and (ThirdString <> '') then
LongestCommonSubstring := RecordMatchMgt.GetLongestCommonSubstring(RecordMatchMgt.GetLongestCommonSubstring(FirstString, SecondString), ThirdString);
end;
}
END
Hope this will help.
Thanks for reading.
ZHU
コメント