individual characters inside a string are stored as char
play

Individual characters inside a String are stored - PDF document

char : A primitive type representing single characters. Individual characters inside a String are stored as char values. Literal char values are surrounded


  1. ���������� � char : A primitive type representing single characters. ��������������� � Individual characters inside a String are stored as char values. � Literal char values are surrounded with apostrophe (single-quote) marks, such as 'a' or '4' or '\n' or '\'' Readings: 4.4 � Like any other type, you can create variables, parameters, and returns of type char . char letter = 'S'; System.out.println(letter); // S � � ���� charAt ������ ��������������� � text processing : Examining, editing, formatting text. � The characters of a string can be accessed using the String object's charAt method. � Text processing often involves for loops that examine the � Recall that string indices start at 0. characters of a string one by one. String word = console.next(); char firstLetter = word.charAt(0) ; � You can use charAt to search for or count occurrences of if (firstLetter == 'c') { a particular character in a string. System.out.println("C is for cookie!"); } � � ������������������������ ��������� char � � char values can be concatenated with strings. // Returns the count of occurrences of c in s. char initial = 'P'; public static int count(String s, char c) { System.out.println(initial + ". Diddy"); int count = 0; for (int i = 0; i < s.length(); i++) { � You can compare char values with relational operators. if ( s.charAt(i) == c) { � 'a' < 'b' and 'Q' != 'q' count++; � Caution : You cannot use these operators on a String ! } } � Example: return count; // print the alphabet for (char c = 'a'; c <= 'z'; c++) { } System.out.print(c); } � count("mississippi", 'i') returns 4 � � 1

  2. char !�"� String String ������ char ������������ � 'h' is a char � Recall the String methods char c = 'h'; Method name Description � char values are primitive ; you cannot call methods on them charAt( index ) returns the character at the given index � can't say c.length() or c.toUpperCase() indexOf( str ) returns the index where the start of the given string appears in this string (-1 if not found) length() � "h" is a String returns the number of characters in this string substring( index1 , index2 ) returns the characters in this string from String s = "h"; index1 up to, but not including, index2 � Strings are objects; they contain methods that can be called toLowerCase() returns a new string with all lowercase letters � can say s.length() 1 toUpperCase() returns a new string with all uppercase letters � can say s.toUpperCase() "H" � can say s.charAt(0) ' h' # String ������ char ������������ String ������ char ������������ � Write a method named pigLatinWord that accepts a � Write a method printName that accepts a full String as a parameter and outputs that word in simplified name as a parameter, and prints the last name Pig Latin, by placing the word's first letter at the end followed by a comma, followed by the first name followed by the suffix ay . and middle initial. � pigLatinWord("hello") prints ello-hay � pigLatinWord("goodbye") prints oodbye-gay printName("Marty Notfunny Stepp"); � Write methods named encode and decode that accept a String as a parameter and outputs that String with each would output: of its letters increased or decreased by 1. � encode("hello") prints ifmmp Stepp, Marty N. � decode("ifmmp") prints hello $ �% printName ��&��������'����������� (�������������������������������������� public static void printName(String fullName) { � Objects (such as String , Point , and Color ) should be int firstBlankIndex = fullName.indexOf(" "); compared for equality by calling a method named equals . String upToMiddleInitial = fullName.substring(0, firstBlankIndex + 2); String middleAndLastName = fullName.substring(firstBlankIndex + 1, fullName.length()); int secondBlankIndex = middleAndLastName.indexOf(" "); � Example: // Notice that "secondBlankIndex" is used with "middleAndLastName" and NOT Scanner console = new Scanner(System.in); // "fullName". If you said // System.out.print("What is your name? "); // fullName.substring(secondBlankIndex + 1, fullName.length()) // String name = console.next(); // you wouldn't get the last name properly. Make sure you understand if ( name.equals("Barney") ) { // why. String lastName = middleAndLastName.substring(secondBlankIndex + 1, System.out.println("I love you, you love me,"); middleAndLastName.length()); System.out.println("We're a happy family!"); System.out.println(lastName + ", " + upToMiddleInitial + "."); } } �� �� 2

  3. )�������������*�+������� == , -�����'.���������������/��������������� � Relational operators such as < and == only behave � The == operator compares whether two variables contain correctly on primitive values. the same value. � The == operator on String s often evaluates to false even when the String s have the same letters in them. � Question: What do object variables contain? Example: WRONG! � Answer: Object variables contain addresses. String name = console.next(); � Using == checks if two object variables have the same if ( name == "Barney" ) { System.out.println("I love you, you love me,"); address (i.e. that they refer to the same object). System.out.println("We're a happy family!"); } � This example code will compile, but it will never print the message, even if the user does type Barney . �� �� ������� (�������������������������������������� The equals method compares whether two objects have the same � � There are more methods of a String object that can be state as each other. used in <test> conditions. What does the following print? � Method Description Point p1 = new Point(3, 8); equals( str ) Point p2 = new Point(3, 8); whether this string contains exactly the Point p3 = p2; p1 x : y : same characters as the other string 3 8 if (p1 == p2) { equalsIgnoreCase( str ) whether this string contains the same System.out.println("1"); } characters as the other, ignoring upper- x : y : p2 if (p1.equals(p2)) { 3 8 vs. lowercase differences System.out.println("2"); } startsWith( str ) whether this string contains the other's if (p2 == p3) { p3 characters at its start System.out.println("3"); } endsWith( str ) if (p2.equals(p3)) { whether this string contains the other's System.out.println("4"); characters at its end } �� �� ��������������������������� while ����� � Hypothetical examples, assuming the existence of various String variables: if ( title.endsWith("M.D.") ) { � System.out.println("What's your number?"); } if ( fullName.startsWith("Marty") ) { � System.out.println("When's your 13th birthday?"); } Readings: 5.1 if ( lastName.equalsIgnoreCase("lumBerg") ) { � System.out.println("I need your TPS reports!"); } if ( name.toLowerCase().indexOf("sr.") >= 0 ) { � System.out.println("You must be old!"); } � �# 3

Recommend


More recommend