CS2011 Introduction to Programming I Strings

Slides:



Advertisements
Similar presentations
Java
Advertisements

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.
Java Strings in 10 minutes
©2004 Brooks/Cole Chapter 7 Strings and Characters.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
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 ,
Fundamental Programming Structures in Java: Strings.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
1 Chapter 2 JAVA FUNDAMENTALS CONT’D. 2 PRIMITIVE DATA TYPES Computer programs operate on data values. Values in Java are classified according to their.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
String Class. Objectives and Goals Identify 2 types of Strings: Literal & Symbolic. Learn about String constructors and commonly used methods Learn several.
Lecture 14 March 23, Exam Results Class Average was 69.4 –Median was 72.5 (which means there were some very low grades) Questions 31, 32, and 33.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text.
String Manipulation Chapter 15 This chapter explains the String facilities. You have already seen some of the main methods of the String class.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Types CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (Chapter 4, Horstmann text)
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
Operations on Strings. 8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation.
Chapter 7: Characters, Strings, and the StringBuilder.
CHAPTER 9 Text Processing and More about Wrapper Classes Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
String and Scanner CS 21a: Introduction to Computing I First Semester,
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
Strings JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Strings Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and.
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
CS 201 California State University, Los Angeles.  Various Mathematical Functions  Characters  Strings.
Chapter 4 Mathematical Functions, Characters, and Strings 1.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
Chapter 4 Mathematical Functions, Characters, and Strings
Strings, Characters and Regular Expressions
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Strings.
String class.
String Handling in JAVA
Java Programming: From Problem Analysis to Program Design, 4e
Primitive Types Vs. Reference Types, Strings, Enumerations
Advanced Programming Behnam Hatami Fall 2017.
Advanced Programming in Java
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
Part a: Fundamentals & Class String
Chapter 2: Basic Elements of Java
CS2011 Introduction to Programming I Arrays (II)
CS2011 Introduction to Programming I Loop Statements (II)
CS2011 Introduction to Programming I Methods (II)
CS2011 Introduction to Programming I Elementary Programming
Introduction to Java Applications
Visual Programming COMP-315
Strings in Java Strings in Java are also a reference type.
Unit-2 Objects and Classes
Presentation transcript:

CS2011 Introduction to Programming I Strings Chengyu Sun California State University, Los Angeles

Class A class can be considered as a user-defined type For example: public class Point { double x; double y; }

Object Objects are instances of a class Type Instances Primitive Type Values Class Objects

Static Method A class may have methods Methods declared as static are associated with the class For example: Math.random() Character.isDigit() <class_name>.<method_name>

Instance Method Methods without static are associated with each object (i.e. instance) of a class, so they are called instance methods

Instance Method Example Class Object Create an object Scanner in = new Scanner( System.in ); int n = in.nextInt(); in.close(); Instance Methods <object_name>.<method_name>

String String is a Java class representing a sequence of character String values (i.e. objects) are text enclosed in double quotes, e.g. String message = "Welcome to Java"; Text processing is a very common task in programming, so it's very important for us to know how to handle Strings.

Some String Instance Methods length() charAt() concat() toUpperCase() toLowerCase() trim() substring()

How To Read API Documentation https://docs.oracle.com/javase/10/docs/api/java/lang/String.html Constructors Fields Methods Static, Instance, Concrete, Deprecated Arguments and return value

Search Inside a String indexOf() lastIndexOf() Example: split a name into first name and last name Handle erroneous input (i.e. no whitespace) Handle leading/trailing whitespaces Handle multiple whitespaces

Read String from Console Scanner methods next() nextLine() Example: read name from input in the SplitName example

Formatting Output double amount = 12618.98; double interestRate = 0.0013; double interest = amount * interestRate; System.out.printf("Interest is $%4.2f", interest);

Format Specifier % 4 .2 f Width, i.e. minimum Number of characters To be written to output Start of a format specifier % 4 .2 f Conversion Character Precision, i.e. number of Digits after the decimal point for floating-point numbers d: decimal integer f: floating-point number c: character s: String Java printf() Quick Reference

String Comparison String s1 = "ab"; String s2 = "abc".substring(0,2); System.out.println(s1); System.out.println(s2); System.out.println(s1 == s2); // true or false??

Value vs. Reference Variables of primitive types hold values int a = 1; int b = a; a = 2; System.out.print(a); //?? System.out.print(b); //?? Point a = new Point(); a.x = 1; Point b = a; a.x = 2; System.out.print(a.x); //?? System.out.print(b.x); //?? Variables of primitive types hold values Variables of class types hold references

Reference An object reference can be thought as the location of the object in memory, or a pointer pointing to the object Create a new object in memory Point a = new Point(); Point b = a; Assign the location of the object to a Copy the location of the object to b

Reference Comparison s1 s1 "ab" "ab" s2 s2 "ab" s1 == s2 s1 == s2 true false

String Comparison By Value equals() equalsIgnoreCase() compareTo() compareToIgnoreCase()

More String Comparisons startsWith() endsWith() contains()

Interned Strings String s1 = "abc"; String s2 = "abc"; Sytem.out.println( s1 == s2 ); // true In order to improve memory efficiency, JVM keeps a "pool" of the most recently used String literals If two String literals are the same, they refer to the same String object in the pool But, what if we change s1, e.g. s1 = "xyz" ??

String Are Immutable The content of a String cannot be changed – when it seems like a String is changed, it's actually a new String being created "xy" s1 s1 "ab" "ab" String s1 = "ab"; s1 = "xy";

String Conversion String s int i String s double d Integer.parseInt(s) Double.parseDouble(s) String s double d d + ""

Example: Hex2Dec2 Convert a 2-digit hexadecimal number to a decimal value Hex Digit Decimal Value 1 - 9 1 – 9 A 10 B 11 C 12 D 13 E 14 F 15

Readings Chapter 4 of the textbook (there will be a quiz next week)