1 / 13

Characters and Strings

Characters and Strings. Characters and Strings. String class. Defined in java.lang.String String aGreeting = new String(“Hello”); OR String aGreeting = “Hello”;. Comparing Strings. String variable name is a reference to a location in memory String aGreeting = “Hello”;

ellie
Download Presentation

Characters and Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Characters and Strings Characters and Strings

  2. String class • Defined in java.lang.String • String aGreeting = new String(“Hello”); OR • String aGreeting = “Hello”;

  3. Comparing Strings • String variable name is a reference to a location in memory String aGreeting = “Hello”; aGreeting = “Bonjour”; aGreeting holds an address that points to “Hello” aGreeting holds a new address that points to “Bonjour” The garbage collector will discard “Hello” Strings and other objects that can’t be changed are known as immutable

  4. The equals() method • Evaluates the contents of two String objects to see if they are the same • Method returns boolean true if equivalent if(aName.equals(anotherName))

  5. The equalsIgnoreCase() method • Evaluates the contents of two String objects to see if they are the same • Ignores the case of the letters aName = “DEBBIE”; anotherName = “Debbie”; if(aName.equalsIgnoreCase(anotherName)) Returns true

  6. The compareTo() method • Returns: 0 if strings are equal negative #  calling “less than” argument positive # I calling “greater than” argument • Example: aName = “Roger”; aName.compareTo(“Robert”) 5 • For alphabetical order: if(aWord.compareTo(anotherWord) < 0) aWord = hamster; anotherWord = iguana Returns true

  7. Other String Methods • toUpperCase() aWord = “something”; aWord = aWord.toUpperCase(); • toLowerCase() • indexOf() String myName = “Stacy”; myName.indexOf(‘a’); myName.indexOf(‘q’); • charAt() myName.charAt(0); Results?

  8. Other String Methods • endsWith() myName.startsWith(“Sta”) returns True • replace() String yourName = “Annette”; String goofyName = yourName.replace(‘n’, ‘X’); AXXette

  9. toString() Method • Converts primitive type to a String • How do we know that toString() is an overloaded method? int someInt = 4; theString = Integer.toString(someInt); “4”

  10. subString() Method • subString(start position, end position) String[] dayOfWeek = “Monday”, “Tuesday”, “Wednesday”, ”Thursday”, ”Friday”}; String sentence; int x; for(x = 0; x < dayOfWeek.length; ++x) { sentence = “The abbreviation for “ + dayOfWeek[x] + “ is “ + dayOfWeek[x].substring(0,3); System.out.println(sentence); }

  11. Converting Strings to Integers • To convert a String to an integer, use the Integer class • parseInt() is a method of the Integer class anInt = Integer.parseInt(“649”);

  12. Converting Strings to Doubles • To convert a String to a double, use the Double class • double dvalue = Double.parseDouble(“147.82”) • valueOf()and doubleValue() are methods of the Double class String stringValue = new String(“147.82”); Double tempValue = Double.valueOf(stringValue); double doubleValue = tempvalue.doubleValue();

  13. Example of Input import java.swing.x; public class NumIn { public static void main(String[ ] args) { String inputString; int inputNumber; inputString = JOptionPane.showInputDialog(null, “Enter the number”); inputNumber = Integer.parseInt(inputString); if (inputNumber > 100) JOptionPane.showMessageDialog(null, “A surchage will apply”); System.exit(0); } }

More Related