Convert String to Char array in AL code

Was needed in a forum question.

 procedure ConvertStringToCharArray(var arr: array[10] of char; srcStr: Text) var   i: Integer; begin   for i := 1 to strlen(srcStr) do     arr[i] := srcStr[i]; end;
or simply convert a String Element into a char value. 😉
srcStr: Text;chVal: char// get 1. element of the string and convert it to a charchVal := srcStr[1]; .

 

Convert Tab delimited strings

I had an issue to process a csv file with TAB delimited text lines. The existing code expected text lines with ; as delimiter.

Character TAB has ASCII Code 9.

So i wrote following code:

ConvertTabString(line : Text[250]) : Text[250]ch := 9; // of type Charline := CONVERTSTR(line,FORMAT(ch),';');exit(line);

test code:

// set a line value or load the line from a text file using file.openline := 'ab cd  ef  cd'; line := ConvertTabString(line);MESSAGE(line);result: 'ab;cd;ef;cd'

an interesting thing is, that this code does not work for older nav 2009 builds. maybe a problem with the encoding. for that case i developed a second version.

// text file test.txt contains 1 text line 'ab cd  ef  cd'. with TAB as delimiter.// file : FILE// instr : InStreamfile.OPEN('c:\temp\test.txt');file.CREATEINSTREAM(instr);instr.readtext(line,maxstrlen(line));line := CONVERTSTR(line,FORMAT(ch),';');message(line);file.close;

This can also be used to export the converted lines to a new text file, which can then be imported via dataport/xmlport.

cheers