Download presentation
Presentation is loading. Please wait.
Published byAnnabelle Kerry Riley Modified over 9 years ago
1
Characters and Strings
2
Characters New primitive char char letter; letter = ‘a’; char letter2 = ‘C’; Because computers can only represent numbers, Characters have ASCII values
3
Characters
4
These values can be used in comparisons and in calculations: char letter = ‘a’ + ‘c’; char letter = (char) 70; int valueOfX = (int) ‘X’; ‘a’ < ‘b’
5
Strings Strings are special objects A String is a sequence of characters treated as a single value String s = new String(“xyz”); Its special because you can create them many different ways: String s = “xyz”; String s = inputBox.getString( ); s += “ABC”;
6
Strings Strings are special objects A String is a sequence of characters treated as a single value String s = new String(“xyz”); Its special because you can create them many different ways: String s = new String(“xyz”); String s = new String(inputBox.getString( ) ); s = new String( s + “ABC”);
7
Strings Common String methods length( ) - returns the length of the String charAt(int i) – returns char at position I CompareTo(String s) – 0 if equal, negative if this String is s Substring(int beginIndex, int endIndex) – returns a new String equals(String s) – true or false equalsIgnoreCase(String s) – true or false Know these!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
8
Strings Example: // count the number of letter a’s in a string int count = 0; char a = ‘a’; String s = inputBox.getString(); for (int i = 0; I < s.length( ); i++){ if (s.charAt( i ) == a) count++; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.