Presentation is loading. Please wait.

Presentation is loading. Please wait.

Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,

Similar presentations


Presentation on theme: "Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,"— Presentation transcript:

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++; }


Download ppt "Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,"

Similar presentations


Ads by Google