Strings Chapter 7 CSCI 1302. CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.

Slides:



Advertisements
Similar presentations
1 StringBuffer & StringTokenizer Classes Chapter 5 - Other String Classes.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text I/O.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings and Text.
Programming 2 CS112- Lab 2 Java
String class  Construct a string  String str = new String(“welcome”);  Char[] charr = {‘G’, ‘o’, ‘o’, ‘d’};  String mes = new String(charr);  A full.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 8 Strings and Text.
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text.
String & Composition. Agenda This keyword. String class. String operations. Composition.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 8 Strings and Text.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 8 Strings 1.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;
Chapter 7: Characters, Strings, and the StringBuilder.
Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
1 The String Class F Constructing a String: F Obtaining String length and Retrieving Individual Characters in a string F String Concatenation (concat)
CHAPTER 8 File Input Output Part 1: String. The String Class  Constructing a String: String message = "Welcome to Java“; String message = new String("Welcome.
Chapter 5 Programming with Objects and Classes OO Programming Concepts OO Programming Concepts Declaring and Creating Objects Declaring and Creating Objects.
1 Chapter 9 Strings and Text I/O. 2 Objectives F To use the String class to process fixed strings. F To use the Character class to process a single character.
Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Strings and ArrayLists April 23, 2012 ASFA AP CS.
17-Feb-16 String and StringBuilder Part I: String.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings Chapter.
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
Chapter 9 Strings and Text I/O
String and StringBuffer classes
Strings.
String and String Buffers
Primitive Types Vs. Reference Types, Strings, Enumerations
Modern Programming Tools And Techniques-I Lecture 11: String Handling
String and StringBuilder
Chapter 8 Strings and Text I/O
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
String and StringBuilder
Chapter 9 Strings and Text I/O
String and StringBuilder
16 Strings.
Chapter 9 Strings.
Lecture 07 String Jaeki Song.
String and StringBuilder
String.
CS2011 Introduction to Programming I Strings
Chapter 9 Strings and Text I/O
Chapter 10 Thinking in Objects Part 2
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
In Java, strings are objects that belong to class java.lang.String .
Strings in Java Strings in Java are also a reference type.
Unit-2 Objects and Classes
Presentation transcript:

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