Strings Chapter 7 CSCI 1302
CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String Length and Individual Characters –String Concatenation –Extracting Substrings –String Comparisons –String Conversions
CSCI 1302 – Strings3 Outline –Finding a Character or a Substring –Conversions between Strings and Arrays –Converting Characters and Numeric Values to Strings The Character Class The StringBuffer Class –Constructing a String Buffer –Modifying Strings in the Buffer –Important Methods
CSCI 1302 – Strings4 Outline The StringTokenizer class The Scanner class (JDK 1.5 feature) Implementing MyInput Using Scanner Command-Line Arguments –Passing Strings to the main Method –Processing Command-Line Arguments
CSCI 1302 – Strings5 Introduction Sequence of characters Often represented as an array of characters Is an object in Java String – efficient for storing and processing StringBuffer – used for flexible strings that can be modified StringTokenizer – utility class
CSCI 1302 – Strings6 The String class Models a sequence of characters Total of eleven constructors Over forty methods for working with strings Methods include examining individual characters in a sequence, comparing strings, searching substrings, extracting substrings, and creating a copy of a string with a change of case
CSCI 1302 – Strings7 Constructing String s Strings can be created from a string literal or an array of characters String newString = new String(“Message”); Shorthand initializer String newString = “Message”; Array of characters char[] charArray = {‘H’,’i’,’!’}; String newString = new String(charArray);
CSCI 1302 – Strings8 Immutable Strings A String object is immutable, its contents cannot be changed String s = “Java”; s = “HTML”;
CSCI 1302 – Strings9 Canonical Strings JVM stores two String objects in the same object if they were created with the same string literal using shorthand initialization This is a canonical string Use the intern() method to return the canonical string
CSCI 1302 – Strings10 Canonical Strings s1 == s is false s2 == s is true s == s3 is true
CSCI 1302 – Strings11 String Length Use length() to get length of a string String message = “Java”; message.length(); // returns 4
CSCI 1302 – Strings12 Individual Characters Use charAt(index) to retrieve a specific character Index starts at 0 Don’t use array index notation message[2]
CSCI 1302 – Strings13 String Concatenation Use the concat(String s) method or the + symbol to concatenate two strings String s1 = “Hello ”; String s2 = “World”; String s3 = s1.concat(s2); String s3 = s1 + s2; Hello World
CSCI 1302 – Strings14 Extracting Substrings Returns a substring of the original string public String substring(int begin, int end); public String substring(int begin);
CSCI 1302 – Strings15 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”; String s1 = “Welcome to” + s0; String s2 = Welcome to Java”; System.out.println(“s1==s2 is “ + (s1==s2)); // false System.out.println(“s1.equals(s2) is “ + (s1.equals(s2))); // true
CSCI 1302 – Strings16 String Comparisons Can also use the compareTo(String s) method Don’t use >, >=, <, or <= s1 = “abc”; s2 = “abg”; s3 = “abc”; s4 = “aba”; s1.compareTo(s2); // returns -4 s1.compareTo(s3); // returns 0 s1.compareTo(s4); // returns 2