Concatenation: Difference between revisions

From BR Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 27: Line 27:
[[Category:Operations]]
[[Category:Operations]]
[[Category:String Operations]]
[[Category:String Operations]]
[[Category:Definitions]]
</noinclude>
</noinclude>

Revision as of 00:02, 8 February 2013

String concatenation is the operation of joining two or more 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 concatenating string1$ & string2$ & string3$. 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