html5-img
1 / 20

The Java String Type

The Java String Type. CSC 1401 : Introduction to Programming with Java Week 7 – Lecture 1 Wanda M. Kunkle. Data Types in Java. Recall from week 2 that there are two main kinds of types in Java: Primitive type Stores a single piece of data of a specific type

Download Presentation

The Java String Type

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. The Java String Type CSC 1401: Introduction to Programming with Java Week 7 – Lecture 1 Wanda M. Kunkle

  2. Data Types in Java • Recall from week 2 that there are two main kinds of types in Java: • Primitive type • Stores a single piece of data of a specific type • Some primitive types and the kinds of data they store are: • int – a 32-bit integer (A bit is a 0 or 1.) • float – a 32-bit floating point (decimal) number • double – a 64-bit floating point (decimal) number • char – a single character (e.g., ‘Y’)

  3. Data Types in Java • Class type • Stores multiple pieces of data and provides methods for manipulating that data • Some class types are: • String – stores and manipulates a string of characters contained between double quotation marks, e.g., “Susan” • input – stores and manipulates text entered at the keyboard • Variables that are class types are called objects.

  4. Declaring String Objects • We declare String variables (technically, objects ) basically the same way that we declare variables of other types. • Examples: • String first_name, last_name, full_name; • We assign values to String objects basically the same way that we assign values to variables of other types. • Examples: • first_name = “Darth”; • last_name = “Vader”; • full_name = first_name + “ ” + last_name ;

  5. Inputting Strings • Strings • Use the readname() method to input a string consisting of letters, digits, and underscores. • The method reads until it encounters a character that is not a letter, digit, or underscore. • For example, given the input “Ah! I see.” the statement:String name = in.readname();would store “Ah” in name. • Use the readword() method to input a string consisting of nonblank characters. • The method reads until it encounters a blank character (Blank characters include the space and newline characters.). • For example, given the input “Ah! I see.” the statement:String word = in.readword();would store “Ah!” in word.

  6. Inputting Strings • Strings • Use the readline() method to input a string consisting of nonblank as well as blank characters. • The method reads until it encounters the newline character (which it reads but does not store). • For example, given the input “Ah! I see.” the statement:String line = in.readline();would store “Ah! I see.” in line. • Use the readln() method to input a string and skip it. • The method reads until it encounters the newline character; it skips all characters, including the newline. • For example, the statement:in.readln();would read and skip any input string ending in newline.

  7. String Methods • String methods can be used to manipulate the values stored in String objects. • Java’s creators at Sun Microsystems provided many such methods, viewable at: • http://java.sun.com/javase/6/docs/api/index.html • We will look at a small subset of these methods.

  8. String Methods • length() • Returns the length of the String object • Example: • output out = new output();String first_name = “Darth”;int name_length = first_name.length();out.writeln(name_length); // Displays 5

  9. String Methods • toUpperCase() • Returns a string with same characters as the calling String object, but converted to uppercase • Example: • output out = new output();String first_name = “Darth”;out.writeln(first_name.toUpperCase()); // Displays // DARTH

  10. String Methods • toLowerCase() • Returns a string with same characters as the calling String object, but converted to lowercase • Example: • output out = new output();String first_name = “Darth”;out.writeln(first_name.toLowerCase()); // Displays // darth • Question: • Do you think the original string has been altered? • How could we go about checking this?

  11. Sample Program • Now let’s look at a sample program that demonstrates using these 3 methods: • StringTypeDemo1.java

  12. String Methods • equals(otherString) • Returns true if the calling String object and otherString are equal; otherwise, returns false • Example: • output out = new output();String response = “yes”;if (response.equals(“Yes”)) out.writeln(“The responses are equivalent.”);else out.writeln(“The responses are NOT equivalent.”);// What do you think is displayed?

  13. String Methods • equalsIgnoreCase(otherString) • Returns true if the calling String object and otherString are equal when case is ignored; otherwise, returns false • Example: • output out = new output();String response = “yes”;if (response.equals(“YES”)) out.writeln(“The responses are equivalent.”);else out.writeln(“The responses are NOT equivalent.”);// What do you think is displayed?

  14. Sample Program • Now let’s look at a sample program that demonstrates using one of these methods (Aside: You’ve seen it before.): • examAverager.java

  15. String Methods • substring(start) • Returns the substring of the calling String object from position start to the end of the calling object • Example: • String street_name, street_address;street_address = “3141 Chestnut Street”;street_name = street_address.substring(5);

  16. String Methods • substring(start, end) • Returns the substring of the calling String object from position start through, but not including, position end of the calling object • Example: • String street_number, street_address;street_address = “3141 Chestnut Street”;street_number = street_address.substring(0, 4);

  17. String Methods • indexOf(aString) • Returns the position of the first occurrence of aString in the calling String object; if aString is not found, returns -1 • Example: • String street_number, street_name, street_address;int spacePosition;street_address = “3141 Chestnut Street”;// Use the location of the space to break apart the street addressspacePosition = street_address.indexOf(" ");street_name = street_address.substring(spacePosition + 1);street_number = street_address.substring(0, spacePosition);

  18. String Methods • indexOf(aString, start) • Returns the position of the first occurrence of aString in the calling String object that occurs at or after position start; if aString is not found, returns -1 • Example: • String street_address;int periodPosition;street_address = “3141 Chestnut Street”;// Locate a period, if presentperiodPosition = street_address.indexOf(“.”, 0);// What value does the above code return?

  19. Sample Program • Now let’s look at a sample program that demonstrates using at least 3 of these methods: • StringTypeDemo2.java

  20. String Methods • charAt(position) • Returns the character in the calling String object at position • Example: • String street_address;char spaceChar;street_address = “3141 Chestnut Street”;spaceChar = street_address.charAt(4);

More Related