19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

340 Chapter 9 Strings<br />

string index range<br />

Caution<br />

Attempting <strong>to</strong> access characters in a string s out of bounds is a <strong>com</strong>mon programming<br />

error. To avoid it, make sure that you do not use an index beyond<br />

s.length() – 1. For example, s.charAt(s.length()) would cause a<br />

StringIndexOutOfBoundsException.<br />

You can use the concat method <strong>to</strong> concatenate two strings. The statement shown below, for<br />

example, concatenates strings s1 and s2 in<strong>to</strong> s3:<br />

s1.concat(s2)<br />

String s3 = s1.concat(s2);<br />

Because string concatenation is heavily used in programming, <strong>Java</strong> provides a convenient way<br />

<strong>to</strong> ac<strong>com</strong>plish it. You can use the plus (+) opera<strong>to</strong>r <strong>to</strong> concatenate two strings, so the previous<br />

statement is equivalent <strong>to</strong><br />

s1 + s2<br />

String s3 = s1 + s2;<br />

The following code <strong>com</strong>bines the strings message, " and ", and "HTML" in<strong>to</strong> one string:<br />

String myString = message + " and " + "HTML";<br />

Recall that the + opera<strong>to</strong>r can also concatenate a number with a string. In this case, the number<br />

is converted in<strong>to</strong> a string and then concatenated. Note that at least one of the operands<br />

must be a string in order for concatenation <strong>to</strong> take place.<br />

9.2.5 Obtaining Substrings<br />

You can obtain a single character from a string using the charAt method, as shown in Figure 9.3.<br />

You can also obtain a substring from a string using the substring method in the String class,<br />

as shown in Figure 9.5.<br />

For example,<br />

String message = "Wel<strong>com</strong>e <strong>to</strong> <strong>Java</strong>".substring(0, 11) + "HTML";<br />

The string message now be<strong>com</strong>es Wel<strong>com</strong>e <strong>to</strong> HTML.<br />

java.lang.String<br />

+substring(beginIndex: int):<br />

String<br />

+substring(beginIndex: int,<br />

endIndex: int): String<br />

Returns this string’s substring that begins with the character at the<br />

specified beginIndex and extends <strong>to</strong> the end of the string, as<br />

shown in Figure 9.6.<br />

Returns this string’s substring that begins at the specified<br />

beginIndex and extends <strong>to</strong> the character at index endIndex – 1,<br />

as shown in Figure 9.6. Note that the character at endIndex is not<br />

part of the substring.<br />

FIGURE 9.5<br />

The String class contains the methods for obtaining substrings.<br />

Indices<br />

Message<br />

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14<br />

W e l c o m e t o J a v a<br />

message.substring(0, 11)<br />

message.substring(11)<br />

FIGURE 9.6<br />

The substring method obtains a substring from a string.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!