Concatenation

From BR Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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

Advanced

For increased speed of program execution, and shorter syntax you may incorporate the following methods.

Appending

To append string2$ to string1$ means to join string2$ to the end of string1$.

To append to the end of a String you should (for maximum speed of code execution) use

X$(inf:0)="append this to end"

OR

X$(inf:inf)="append this to the end"

Here, inf denotes infinity.

So X$(inf:inf) means "the substring of X$ starting at infinity". This is particularly useful when you don't know how long your string is and do not want to calculate its length.

see also: prepend


Prepending

To prepend string1$ to string2$ means to join string1$ to the beginning of string2$.

To append to the beginning of a string you should use

X$(0:0)="append this to front"

or alternately

X$(1:0)="append this to front"

For example,

00010 dim result$*255 ! dimension long enough to fit the result
00020 let result$ = " and this is the end"
00030 let string_to_prepend$ = "this is the front"
00040 let result$(0:0) = string_to_prepend$
00050 print result$

The output of the program above will be:

this is the front and this is the end