设为首页 加入收藏

TOP

3.2 Library string Type (4)
2013-10-07 16:19:07 来源: 作者: 【 】 浏览:65
Tags:3.2 Library string Type

Comparing strings

The string class defines several operators that compare strings. These operators work by comparing the characters of the strings. The comparisons are case-sensitive—upper- and lowercase versions of a letter are different characters.

The equality operators (== and !=) test whether two strings are equal or unequal, respectively. Two strings are equal if they are the same length and contain the same characters. The relational operators <, <=, >, >= test whether one string is less than, less than or equal to, greater than, or greater than or equal to another. These operators use the same strategy as a (case-sensitive) dictionary:
1. If two strings have different lengths and if every character in the shorter string is equal to the corresponding character of the longer string, then the shorter string is less than the longer one.

2. If any characters at corresponding positions in the two strings differ, then the result of the string comparison is the result of comparing the first character at which the strings differ.

As an example, consider the following strings:

  1. string str = "Hello";   
  2. string phrase = "Hello World";   
  3. string slang = "Hiya";  

Using rule 1, we see that str is less than phrase. By applying rule 2, we see that slang is greater than both str and phrase.

Assignment for strings

In general, the library types strive to make it as easy to use a library type as it is to use a built-in type. To this end, most of the library types support assignment. In the case of strings, we can assign one string object to another:

  1. string st1(10, ’c’), st2; // st1 is cccccccccc; st2 is an empty string   
  2. st1 = st2; // assignment: replace contents of st1 with a copy of st2   
  3. // both st1 and st2 are now the empty string  

Adding Two strings

Adding two strings yields a new string that is the concatenation of the lefthand followed by the right-hand operand. That is, when we use the plus operator (+) on strings, the result is a new string whose characters are a copy of those in the left-hand operand followed by those from the right-hand operand. The compound assignment operator (+=) (§ 1.4.1, p. 12) appends the right-hand operand to the left-hand string:

  1. string s1 = "hello, ", s2 = "world\n";   
  2. string s3 = s1 + s2; // s3 is hello, world\n   
  3. s1 += s2; // equivalent tos1 = s1 + s2  

Adding Literals and strings

As we saw in § 2.1.2 (p. 35), we can use one type where another type is expected if there is a conversion fromthe given type to the expected type. The string library lets us convert both character literals and character string literals (§ 2.1.3, p. 39) to strings. Because we can use these literals where a string is expected, we can rewrite the previous program as follows:

  1. string s1 = "hello", s2 = "world"// no punctuation in s1 or s2   
  2. string s3 = s1 + ", " + s2 + ’\n’;  

When we mix strings and string or character literals, at least one operand to each + operator must be of string type:

  1. string s4 = s1 + ", "// ok: adding a string and a literal   
  2. string s5 = "hello" + ", "// error: no string operand   
  3. string s6 = s1 + ", " + "world"// ok: each + has a string operand   
  4. string s7 = "hello" + ", " + s2; // error: can’t add string literals  

The initializations of s4 and s5 involve only a single operation each, so it is easy to see whether the initialization is legal. The initialization of s6 may appear surprising, but it works in much the same way as when we chain together input or output expressions (§ 1.2, p. 7). This initialization groups as

  1. string s6 = (s1 + ", ") + "world";  

The subexpression s1 + ", " returns a string, which forms the left-hand operand of the second + operator. It is as if we had written

  1. string tmp = s1 + ", "// ok: + has a string operand   
  2. s6 = tmp + "world"// ok: + has a string operand  

On the other hand, the initialization of s7 is illegal, which we can see if we parenthesize the expression:

  1. string s7 = ("hello" + ", ") + s2; // error: can’t add string literals  

Now it should be easy to see that the first subexpression adds two string literals. There is no way to do so, and so the statement is in error.

For historical reasons, and for compatibility with C, string literals are not standard library strings. It is important to remember that these types differ when you use string literals and library strings.

EXERCISES SECTION 3.2.2

Exercise 3.2: Write a programto read the standard input a line at a time. Modify your program to read a word at a time.

Exercise 3.3: Explain how whitespace characters are handled in the string input operator and in the getline function.

Exercise 3.4: Write a program to read two strings and report whether the strings are equal. If not, report which of the two is larger. Now, change the program to report whether the strings have the same length, and if not, report which is longer.

Exercise 3.5: Write a program to read strings from the standard input, concatenating what is read into one large string. Print the concatenated string. Next, change the program to separate adjacent input strings by a space.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇3.2 Library string Type (2) 下一篇3.2 Library string Type (5)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·用 Python 进行数据 (2025-12-25 15:49:09)
·如何学习Python数据 (2025-12-25 15:49:07)
·利用Python进行数据 (2025-12-25 15:49:04)
·Java 学习线路图是怎 (2025-12-25 15:19:15)
·关于 Java 学习,有 (2025-12-25 15:19:12)