Object-Oriented Programming (From Problem Solving to JAVA) (Charles River Media Programming)

10.3 String as an Array

A string value can be thought of as a sequence of data items of type character and implemented as a special array of characters. Recall that type character is a primitive type. This section discusses additional string operators for string manipulation. Figure 10.1 illustrates the structure and contents of string variable message.

Figure 10.1: Structure of string variable message.

Strings are special variables, they are immutable. After a string has been given an assigned string value, it cannot be changed.

10.3.1 Length of a String

Every string has a different number of characters. The string operator length gets the number of characters in a string variable. This operator is normally used in an assignment statement, and the target variable should be of type integer. The general syntax for the assignment statement with the length operator follows.

set 〈 int_var 〉 = length of 〈 str_var 〉

For example, to get the number of characters in the string variable message and assign this to the integer variable num, the complete statement is:

set num = length of message

10.3.2 Retrieving a Character of a String

To get a copy of a character located at some specified relative position of the string, the operator charat is used. The relative position of a character is known as the index. The integer value of the index starts at 0 and can be up to the value given by length - 1. Figure 10.1 shows the index value of the characters in a string.

The charat operator is normally used in an assignment statement, and the target variable should be of type character. The general syntax for the assignment statement with the charat operator follows.

set 〈 char_var 〉 = charat 〈 index 〉 of 〈 str_var 〉

For example, to get the character at the index value 7 in string variable message and assign this to character variable llchar, the portion of code is:

variable character llchar ... set llchar = charat 7 of message

When this statement executes, the value of variable llchar becomes 'W', which is the character at index position 7 of the string variable message.

10.3.3 Finding the Position of a Character

To find the position of a character within a string is finding the value of the index for the given character. The indexof operator searches the string from left to right. This operator is also used in an assignment statement. The general structure of this statement with the indexof operator follows.

set 〈 int_var 〉 = indexof 〈 char_var 〉 of 〈 str_var 〉

For example, to get the index value in string variable message for the character 'r' and assign this to integer variable num, the complete statement is:

variable integer num ... set num = indexof 'r' of message

When this statement executes, the value of variable num becomes 9. If the starting index value is known, it can be included after the character in the assignment statement. If the character indicated does not exist in the string, then the value assigned is - 1.

10.3.4 Retrieving a Substring from a String

A substring is part of a string. To retrieve a substring from a string, the index position of the character that starts the substring is needed. By default, the end of the substring is also the end of the string. The substring operator is used in an assignment statement and gets a substring that starts at a given index position up to the end of the string. The variable that receives this value is a string variable. The general structure of the assignment statement with the substring operator follows.

set 〈 str_var1 〉 = substring 〈 index 〉 of 〈 str_var2 〉

For example, to get the substring value that starts at index position 7 in string variable message and assign this to string variable yystr, the portion of code is:

variable character yystr integer num = 7 ... set yystr = substring num of message

When this statement executes, the value of variable yystr becomes "World!" Two index values are used for substrings that have start and end index positions. For example, to retrieve the substring located at index positions 8 to 10 of string variable message, the portion of code with the assignment statement should be as follows.

variable character yystr integer num1 = 8 integer num2 = 10 ... set yystr = substring num1 num2 of message

When this statement executes, the value of variable yystr becomes "orl".

Категории