Retrieving Individual characters in a String Do not use message[o] Use message charAt(index) Index starts from o Indices 01234 67891011121314 message 不 不 message. charAt(o) message length( is 15 message.charAt(14) iang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 16 Retrieving Individual Characters in a String Do not use message[0] Use message.charAt(index) Index starts from 0 W e l c o m e t o J a v a 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 message Indices message.charAt(0) message.length() is 15 message.charAt(14)
String concatenation String s3= sl concat(s2)i String S3=Sl S2 s1+s2+s3+s4+s5 same as ((s1concat(s2 ).concat(s3).concat(S4 ) concat(s5) Liang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 17 String Concatenation String s3 = s1.concat(s2); String s3 = s1 + s2; s1 + s2 + s3 + s4 + s5 same as (((s1.concat(s2)).concat(s3)).concat(s4)).concat(s5);
Extracting Substrings You can extract a single character from a string using the charAt method. You can also extract a substring from a string using the substring method in the String class String sl ="Welcome to Java String s2= sl substring(0, 11)+HTMl"i Indices 01234567891011121314 message weicometol message substring(o, Il) message. substring (I1) Liang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 18 Extracting Substrings You can extract a single character from a string using the charAt method. You can also extract a substring from a string using the substring method in the String class. String s1 = "Welcome to Java"; String s2 = s1.substring(0, 11) + "HTML"; W e l c o m e t o J a v a 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 message Indices message.substring(0, 11) message.substring(11)