teach-ict.com logo

THE education site for computer science and ICT

String handling

3. String copy and concatenate

In addition to storing strings, programs can be written to do various things to the text inside them. You can add, remove, edit, or search the text using various commands. We will discuss several of these commands in the next few pages.

Copy

This function copies one string into another, over-writing any previous content. The exact command for doing this varies from language to language. For example in 'C' the command is strcpy(destination, source).

There is also an easier way to copy one string into another. Just use the assignment '=' operator, like this :-

                  MyString = SecondString

This pseudocode copies the content of SecondString into the MyString variable, replacing whatever was in MyString.

Concatenation

Concatenation means to link things together. You can do this with strings using the '+' operator.

Like this :-

               Line 1: FirstName = input("Enter your first name")
               Line 2: Surname = input("Enter your surname")
               Line 3: Message = "Your name is  " + FirstName + " " + Surname
               Line 4: print Message
             

The first two lines are inputs for the user to enter their first name and their surname.

Line 3 uses the concatenate operator to form a message and store it in Message. Notice the " " in the middle of it to insert a space between the first and surname.

The Message string is then displayed by Line 4.

Challenge see if you can find out one extra fact on this topic that we haven't already told you

Click on this link: What is string concatenation?