Download presentation
Presentation is loading. Please wait.
Published byAlannah Shanon Bradford Modified over 8 years ago
1
Strings Chapter 7 CSCI 1302
2
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
3
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
4
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
5
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
6
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
7
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);
8
CSCI 1302 – Strings8 Immutable Strings A String object is immutable, its contents cannot be changed String s = “Java”; s = “HTML”;
9
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
10
CSCI 1302 – Strings10 Canonical Strings s1 == s is false s2 == s is true s == s3 is true
11
CSCI 1302 – Strings11 String Length Use length() to get length of a string String message = “Java”; message.length(); // returns 4
12
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]
13
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
14
CSCI 1302 – Strings14 Extracting Substrings Returns a substring of the original string public String substring(int begin, int end); public String substring(int begin);
15
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
16
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.