Tuesday, 14 August 2007

Operators and Assignments (Part 7)

Strings

String Objects

A String variable an object of the class String. You declare a String variable in much the same way as you define a variable of one of the primitive types. You can initialize it in the declaration:
String myString = “My string”;

You can store another string in a String variable, once you have declared it:
myString = “Another string”;

Notice that although it is a class, you do not need to use the “new” keyword to create instantiate an object.

String Operations

There are many kinds of operations that can be performed on strings, string concatenation, string comparison, searching strings for characters, searching for sub-strings, etc.

String Concatenation
You have already used this operation to join strings together. The + operator is used to achieve this. For example,
myString = “This string 1 ” + “This is string 2”

If either of the arguments are String objects, then all other arguments will be converted to String objects.

No comments: