Object-Oriented Programming (From Problem Solving to JAVA) (Charles River Media Programming)
10.5 Joining Two or More Strings
Two or more strings can be joined one after the other to build a larger string. The operation of joining two strings together is called concatenation. The concat operator is used to join two strings. This operator must be part of an assignment statement and appears between two variables. The following presents the general syntax of the assignment statement with the concat operator, variables var_2 and var_3 are joined the second after the first. The result of the concat operation is assigned to variable var_1.
set 〈 var_1 〉 = 〈 var_2 〉 concat 〈 var_3 〉
For example, the following statement joins together the string variable message, the string constant " and ", and the string variable s2. The resulting string is assigned to string variable s3.
string s3 ... set s3 = message concat " and " concat s2
When this statement executes, the value of string variable s3 becomes:
"Hello, World! and Hi, everyone"
The flexibility of the operation that joins two or more strings is that a string variable can be concatenated to a variable of a different primitive type. For example, in the following statement string, variable s3 is joined with a blank space and joined with the integer variable j and assigned to string variable s4.
integer j string s4 . . . set s4 = s3 concat " " concat j
The numeric value of variable j is converted automatically to a string and then joined with the other string values. The blank space was joined between variables s3 and j for appearance when the string variable s4 is displayed on the console.
Категории