Concatenation

From BR Wiki
Revision as of 10:51, 9 January 2012 by Mikhail.zheleznov (talk | contribs) (edit)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Stringconcatenation is the operation of joining two character strings end-to-end. For example, the strings "snow" and "ball" may be concatenated to give "snowball".

In BR, the operator responsible for string concatenation is the ampersand &. So, the "snowball" example above may be carried out as follows:

00010 let string1$ = "snow"
00020 let string2$ = "ball"
00030 let result$ = string1$ & string2$
00040 print result$ ! this will print snowball

You may concatenate as many strings as you like. Note that if you are storing the concatenated result into a string variable, then this variable needs to be dimensioned long enough to fit the combined length of all the concatenated strings. Consider the following example:

00010 let string1$ = "snow"
00020 let string2$ = "ball"
00030 let string3$ = " effect of concatenating many strings"
00040 let result$ = string1$ & string2$ & string3$ ! this will result in error

The above example results in an error, because the default length of the string result$ is 18 characters, as any BR string. The combined length of string1$, string2$, and string3$ is 45 characters. Note that the error does not result from concatenation. string1$ & string2$ & string3$ works fine. The error occurs when we try to assign a 45 character value to an 18 character string result$. In order to correct the error, we need to dimension result$ to at least 45 characters or more. Below is the corrected example:

00005 dim result$*45
00010 let string1$ = "snow"
00020 let string2$ = "ball"
00030 let string3$ = " effect of concatenating many strings"
00040 let result$ = string1$ & string2$ & string3$ ! this will result in error