Chapter 7: Strings and Characters

Slides:



Advertisements
Similar presentations
Java
Advertisements

Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
String Pemrograman Berbasis Obyek Oleh Tita Karlita.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Strings in Java 1. strings in java are handled by two classes String &
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 Working with String Duo Wei CS110A_ Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized.
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.
Chapter 10 Review. Write a method that returns true is s1 and s2 end with the same character; otherwise return false. Sample Answer: public boolean lastChar(String.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Programming 2 CS112- Lab 2 Java
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
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,
Fundamental Programming Structures in Java: Strings.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
Strings Reading for this Lecture, L&L, 3.2. Strings String is basically just a collection of characters. Thus, the string “Martyn” could be thought of.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Strings.
The java.lang Package chapter 8 Java Certification Study Group February 2, 1998 Seth Ladd.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Programming in Java Dr. M. Ahmadzadeh. Course Outline (subject to tiny changes) I will cover the following but not necessarily in this order. –Strings.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Chapter 7: Characters, Strings, and the StringBuilder.
1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.
String Handling StringBuffer class character class StringTokenizer class.
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)
String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
17-Feb-16 String and StringBuilder Part I: String.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. For example,
String and StringBuffer classes
String and Lists Dr. José M. Reyes Álamo.
Strings, Characters and Regular Expressions
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
String class.
EKT 472: Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
Programming in Java Text Books :
String and String Buffers
Primitive Types Vs. Reference Types, Strings, Enumerations
Modern Programming Tools And Techniques-I Lecture 11: String Handling
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Part a: Fundamentals & Class String
String and Lists Dr. José M. Reyes Álamo.
Java – String Handling.
Lecture 07 String Jaeki Song.
Topics Basic String Operations String Slicing
Topics Basic String Operations String Slicing
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 .
Topics Basic String Operations String Slicing
Presentation transcript:

Chapter 7: Strings and Characters Object-Oriented Program Development Using Java: A Class-Centered Approach

String Processing Strings are manipulated using: Standard String class methods Character-at-a-time methods provided by the Character class

String Equality Never use the equality operator, == Use: Compares values stored in reference variables used to access strings Use: compareTo() equals()

Major String methods String (String str) Constructor: creates a new string object with the same characters as str. char charAt (int index) Returns the character at the specified index. int compareTo (String str) Returns an integer indicating if this string is lexically before (a negative return value), equal to (returns 0) or lexically after (returns a positive integer), the string str.

Major String methods String concat (String str) Returns a new string object consisting of this string concatenated with str. boolean equals (String str) Returns true if this string contains the same characters as str (including case) and false otherwise. boolean equalsIgnoreCase (String str) Returns true if this string contains the same characters as str (without regard to case) and false otherwise.

Major String methods (cont.) int length (String str) Returns the number of characters in this string. String replace (char oldChar, char newChar) Returns new string identical to original except every character oldChar is replaced with the character newChar String substring (int offset, int endIndex) Returns new string that is a subset of this string starting at index offset and extending through endIndex-1.

Major String methods (last slide –finally! String toLowerCase( ) Returns new string identical to original except all uppercase letters are converted to their lowercase equivalent. String toUpperCase( ) Returns new string identical to original except all lowercase letters are converted to their uppercase equivalent. .

The StringBuffer Class Implemented as a mutable sequence of characters Can be modified Does not provide a set of methods for: Comparing strings Locating characters and substrings within a string Created and stored in a buffer having a set character capacity

The StringBuffer Class (continued) String class Preferred class for displaying or comparing strings that tend to remain constant StringBuffer class Should be used for applications that require: Individual character additions Modifications Multiple text edits

Common Programming Errors Breaking a string across two or more lines Not remembering that the first character in a string is located at position 0, not 1 Not remembering that the number returned by the length() method is one more than the position number of the string’s last character Using the == operator to compare two strings Rather than the compareTo() or equals() methods

Common Programming Errors (continued) Not specifying one position beyond the desired character to be extracted using the substring() method

Summary Strings constructed from the String class are immutable More commonly used for constructing strings for input and output purposes StringBuffer class Used when characters within a string need to be replaced, inserted, or deleted on a regular basis

Summary (continued) Strings can be manipulated using either: Methods of the class they are objects of Using general-purpose string and character methods