local procedure TestCopyText()
var
SomeText: Text;
begin
SomeText := 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed id luctus sapien.';
//Strange old way
Message(DelStr(SomeText, 21));
//Old way
Message(CopyStr(SomeText, 1, 20));
//Strange modern way
Message(SomeText.Remove(21));
//Modern way
Message(SomeText.Substring(1, 20));
end;
local procedure TestModernMethods()
var
SentenceList: List of [Text];
Sentence: Text;
SomeText: Text;
SearchText: Text;
begin
SomeText := 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed id luctus sapien.';
SearchText := 'sit';
//Search for substring
if SomeText.Contains(SearchText) then
Message('Text contains %1', SearchText);
//Remove spaces in string
Message('Text with removed spaces:\ %1', SomeText.Replace(' ', ''));
//Replace "a" symbol with "|"
Message('Text with replaced "a" to "|" symbol:\ %1', SomeText.Replace('a', '|'));
//Split string to senteces based on "." separator
SentenceList := SomeText.Split('.');
foreach Sentence in SentenceList do
if Sentence <> '' then
Message('Sentence is:\ %1', Sentence);
end;
local procedure TestTableFields()
var
Customer: Record Customer;
begin
if not Customer.FindFirst() then
exit;
if Customer.Name.Contains('Adatum') then
Message('Customer %1 contains %2 in name.', Customer."No.", 'Adatum');
//Customer name in lowercase
Message('Customer name in lowercase:\ %1', Customer.Name.ToLower());
//First 5 symbols of Customer Address
Message('First 5 symbol of Customer Address:\ %1', Customer.Address.Substring(1, 5));
end;
local procedure TestTextBuilder()
var
LineStorage: TextBuilder;
begin
//Insert Line 1
LineStorage.AppendLine('Lorem ipsum dolor sit amet, consectetur adipiscing elit.');
//Insert empty Line 2
LineStorage.AppendLine();
//Insert Line 3
LineStorage.AppendLine('Sed id luctus sapien.');
//Result text will contain line breaks, but Message may not show it in UI
Message(LineStorage.ToText());
end;
local procedure TestTextParse()
var
TypeHelper: Codeunit "Type Helper";
LineStorage: TextBuilder;
ParsedResult: Dictionary of [Integer, Text];
LineList: List of [Text];
LineValue: Text;
ColumnValue: Text;
i: Integer;
begin
//Emulate text file with line breaks and ";" column separator
LineStorage.AppendLine('No_;Name;Balance;Description');
LineStorage.AppendLine('1000;Adatum;231.2;Some Description');
LineStorage.AppendLine('2000;Test;0;It is test');
LineList := LineStorage.ToText().Split(TypeHelper.CRLFSeparator());
//Read data line by line
foreach LineValue in LineList do
//Read column by ";"
foreach ColumnValue in LineValue.Split(';') do begin
//Just demonstration, it's can be whatever you need to do with Lines and Columns
i += 1;
ParsedResult.Add(i, ColumnValue);
end;
end;
local procedure TestReadTextFromBLOB()
var
SalesHeader: Record "Sales Header";
TypeHelper: Codeunit "Type Helper";
TextInStream: InStream;
Result: Text;
Line: Text;
begin
SalesHeader.SetRange("Document Type", SalesHeader."Document Type"::Order);
if not SalesHeader.FindFirst() then
exit;
//Initialize InStream from BLOB, read with same TextEncoding as it was recorded
SalesHeader."Work Description".CreateInStream(TextInStream, TextEncoding::UTF8);
//Loop for each line into InStream, yes, BLOB may contain line breaks
while not TextInStream.EOS() do begin
//Read and store each line to variable
TextInStream.ReadText(Line);
//Accumulate result and restore line break to show same data as it was in BLOB
//Be careful with CRLF separator, this logic will always add additional line break
Result += Line + TypeHelper.CRLFSeparator();
end;
//Result text will contain line breaks, but Message may not show it in UI
Message(Result);
end;
local procedure ExampleTextDataReadWrite()
var
TempBlob: Codeunit "Temp Blob";
TypeHelper: Codeunit "Type Helper";
TextInStream: InStream;
TextOutStream: OutStream;
TextData, Line : Text;
begin
TempBlob.CreateOutStream(TextOutStream, TextEncoding::UTF8);
TextOutStream.WriteText('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed id luctus sapien.');
TempBlob.CreateInStream(TextInStream, TextEncoding::UTF8);
while not TextInStream.EOS() do begin
TextInStream.ReadText(Line);
TextData += Line + TypeHelper.CRLFSeparator();
end;
Message(TextData);
end;
local procedure ExampleShiftJIS()
var
FileInStream: InStream;
TextData, FileName : Text;
begin
TextData := 'クケコサシクケコサ';
WriteTextInCustomEncoding(TextData, 932).CreateInStream(FileInStream);
FileName := 'test.txt';
DownloadFromStream(FileInStream, '', '', '', FileName);
end;
procedure WriteTextInCustomEncoding(TextData: Text; CodePage: Integer) TempBlob: Codeunit "Temp Blob"
var
DotNetEncoding: Codeunit DotNet_Encoding;
DotNetStreamWriter: codeunit DotNet_StreamWriter;
FileOutStream: OutStream;
begin
TempBlob.CreateOutStream(FileOutStream);
DotNetEncoding.Encoding(CodePage);
DotNetStreamWriter.StreamWriter(FileOutStream, DotNetEncoding);
DotNetStreamWriter.Write(TextData);
DotNetStreamWriter.Dispose();
end;
local procedure ExampleUnicodeUTF16()
var
FileInStream: InStream;
TextData, FileName : Text;
begin
TextData := 'クケコサシクケコサ';
WriteTextInCustomEncoding(TextData, 1200).CreateInStream(FileInStream);
FileName := 'test.txt';
DownloadFromStream(FileInStream, '', '', '', FileName);
end;
local procedure ExampleReadShiftJIS()
var
TextData: Text;
FileInStream: InStream;
begin
TextData := 'クケコサシクケコサ';
WriteTextInCustomEncoding(TextData, 932).CreateInStream(FileInStream);
Message(ReadTextInCustomEncoding(FileInStream, 932));
end;
procedure ReadTextInCustomEncoding(var TextDataAsInStream: InStream; CodePage: Integer) Result: Text
var
DotNetEncoding: Codeunit DotNet_Encoding;
DotNetStreamReader: codeunit DotNet_StreamReader;
begin
DotNetEncoding.Encoding(CodePage);
DotNetStreamReader.StreamReader(TextDataAsInStream, DotNetEncoding);
Result := DotNetStreamReader.ReadToEnd();
DotNetStreamReader.Dispose();
end;