23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

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

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

f<strong>in</strong>ite, we usually assume that the size of the alphabet σ, denoted with |σ|, is a fixed<br />

constant.<br />

Str<strong>in</strong>g operations come <strong>in</strong> two flavors: those that modify the str<strong>in</strong>g they act on <strong>and</strong><br />

those that simply return <strong>in</strong>formation about the str<strong>in</strong>g without actually modify<strong>in</strong>g it.<br />

<strong>Java</strong> makes this dist<strong>in</strong>ction precise by def<strong>in</strong><strong>in</strong>g the Str<strong>in</strong>g class to represent<br />

immutable str<strong>in</strong>gs, which cannot be modified, <strong>and</strong> the Str<strong>in</strong>gBuffer class to<br />

represent mutable str<strong>in</strong>gs, which can be modified.<br />

12.1.1 The <strong>Java</strong> Str<strong>in</strong>g Class<br />

The ma<strong>in</strong> operations of the <strong>Java</strong> Str<strong>in</strong>g class are listed below:<br />

length():<br />

Return the length, n, of S.<br />

charAt(i):<br />

Return the character at <strong>in</strong>dex i <strong>in</strong> S.<br />

startsWith(Q):<br />

Determ<strong>in</strong>e if Q is a prefix of S.<br />

endsWith(Q):<br />

Determ<strong>in</strong>e if Q is a suffix of S.<br />

substr<strong>in</strong>g(i,j):<br />

Return the substr<strong>in</strong>g S[i,j].<br />

concat(Q):<br />

Return the concatenation of S <strong>and</strong> Q, that is, S+Q.<br />

equals(Q):<br />

Determ<strong>in</strong>e if Q is equal to S.<br />

<strong>in</strong>dexOf(Q):<br />

If Q is a substr<strong>in</strong>g of S, return the <strong>in</strong>dex of the beg<strong>in</strong>n<strong>in</strong>g of the first<br />

occurrence of Q <strong>in</strong> S, else return −1.<br />

This collection forms the typical operations for immutable str<strong>in</strong>gs.<br />

745

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

Saved successfully!

Ooh no, something went wrong!