Hi, Readers.
Today I would like to share another mini tip about Business Central, how to extract characters from a string.
For example, we want to know what the fourth letter of a customer’s name is, or want to add a space between each character. How to do it? Of course, we can use String Functions to deal with it, combining some methods can also solve this problem. More details: How to use String Functions in AL (StrPos, CopyStr, PadStr, StrLen, LowerCase, SelectStr…). But is there an easier way to do this?
In this post, I want to introduce another interesting way, we can apply indexing with []. Let us first look at two detailed examples.
Get the fourth letter of a customer’s name:
Add a space between each character:
Or display each character vertically in a message window.
How is this done? As you may have noticed, I used the method below.
Rec.Name[i]
Using this [], you can convert the string into a single-dimensional array (starting from 1). But what was obtained was a number.
For exmaple,
Adatum Corporation:
65 100 97 116 117 109 32 67 111 114 112 111 114 97 116 105 111 110
They are ASCII Characters. Here are some examples I found in the MS Learn (Docs).
character set (0 – 127): 7 bit
Code | Character | Code | Character | Code | Character | Code | Character |
---|---|---|---|---|---|---|---|
0 | | 32 | [space] | 64 | @ | 96 | ` |
1 | | 33 | ! | 65 | A | 97 | a |
2 | | 34 | “ | 66 | B | 98 | b |
3 | | 35 | # | 67 | C | 99 | c |
4 | | 36 | $ | 68 | D | 100 | d |
5 | | 37 | % | 69 | E | 101 | e |
6 | | 38 | & | 70 | F | 102 | f |
7 | | 39 | ‘ | 71 | G | 103 | g |
8 | * * | 40 | ( | 72 | H | 104 | h |
9 | * * | 41 | ) | 73 | I | 105 | i |
10 | * * | 42 | * | 74 | J | 106 | j |
11 | | 43 | + | 75 | K | 107 | k |
12 | | 44 | , | 76 | L | 108 | l |
13 | * * | 45 | – | 77 | M | 109 | m |
14 | | 46 | . | 78 | N | 110 | n |
15 | | 47 | / | 79 | O | 111 | o |
16 | | 48 | 0 | 80 | P | 112 | p |
17 | | 49 | 1 | 81 | Q | 113 | q |
18 | | 50 | 2 | 82 | R | 114 | r |
19 | | 51 | 3 | 83 | S | 115 | s |
20 | | 52 | 4 | 84 | T | 116 | t |
21 | | 53 | 5 | 85 | U | 117 | u |
22 | | 54 | 6 | 86 | V | 118 | v |
23 | | 55 | 7 | 87 | W | 119 | w |
24 | | 56 | 8 | 88 | X | 120 | x |
25 | | 57 | 9 | 89 | Y | 121 | y |
26 | | 58 | : | 90 | Z | 122 | z |
27 | | 59 | ; | 91 | [ | 123 | { |
28 | | 60 | < | 92 | \ | 124 | | |
29 | | 61 | = | 93 | ] | 125 | } |
30 | | 62 | > | 94 | ^ | 126 | ~ |
31 | | 63 | ? | 95 | _ | 127 | |
Then you can simply convert the ASCII Characters back using the Char Data Type. We have briefly discussed similar processing in How to get the next or previous letter in the alphabet.
That’s all.
This also works for string variables such as Code data type and Text data type.
But if it is used in Integer data type and Decimal data type, the following error will be prompted.
Cannot apply indexing with [] to an expression of type ‘Integer’ALAL0143
In addition to common letters and numbers, other special characters, including emoji, Hiragana or kanji, can also be obtained.
Give it a try!!!😁
PS: Business Central 2023 wave 1 (BC22): Iterating with foreach on Text variables
END
Hope this will help.
Thanks for reading.
ZHU
コメント