String class.

Slides:



Advertisements
Similar presentations
Lecture 6 Strings and more I/O COMP1681 / SE15 Introduction to Programming.
Advertisements

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.
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.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
CIS 234: Strings. What Is a String? series of characters enclosed in double quotes characters can include letters (lower case and upper case), digits,
Lecture 5 Strings and more I/O COMP1681 / SE15 Introduction to Programming.
Datalogi A 3: 26/9. Java Concepts chapter 4 Fundamental Data Types int (long and short) double (and float) boolean char String.
Fundamental Programming Structures in Java: Strings.
The Class String. String Constants and Variables  There is no primitive type for strings in Java.  There is a class called String that can be used to.
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.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
String Escape Sequences
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Strings.
Unit 3: Java Data Types Math class and String class.
1 Text processing. 2 text processing: Examining, editing, formatting text.  Text processing often involves for loops that examine the characters of a.
From C++ to Java A whirlwind tour of Java for C++ programmers.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
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.
Chapter 7: Characters, Strings, and the StringBuilder.
1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.
Copyright Curt Hill Variables What are they? Why do we need them?
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Strings in Java. What data types have we seen so far?
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.
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.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CMSC 202 Java Primer. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Java-02 Basic Concepts Review concepts and examine how java handles them.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Introduction to programming in java
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 2 Variables.
Chapter 2 Basic Computation
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Methods Chapter 4: Methods Asserting Java ©Rick Mercer.
String Handling in JAVA
Type Conversion, Constants, and the String Object
Advanced Programming Behnam Hatami Fall 2017.
Advanced Programming in Java
Type Conversion, Constants, and the String Object
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
Examples of Primitive Values
Introduction to C++ Programming
An Introduction to Java – Part I, language basics
CMSC 202 Java Primer 2.
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. Every time.
Building Java Programs
elementary programming
Chapter 2 Programming Basics.
CS2011 Introduction to Programming I Strings
String methods 26-Apr-19.
In this class, we will cover:
Primitive Types and Expressions
Chapter 2 Variables.
Chapter 2: Java Fundamentals cont’d
Strings in Java Strings in Java are also a reference type.
Presentation transcript:

String class

What data types have we seen so far?

Data types we’ve seen so far… integer types: int (also short, char, and byte) real numbers: double and float boolean

Introducing the String Case sensitive (capital S is required) Declaring a String How did we declare variables of the other types (such as int, double, boolean) in the past?

Declarations How have we already declared variables? int i; float f; double x; boolean isAChicken; So how do we declare Strings?

Declaring Strings Declare Declare and initialize (assign) String s; String name = “John”;

The Class String String is NOT a primitive type! The class String is a predefined class in Java that is used to store and process strings Objects of type String are made up of strings of characters that are written within double quotes Any quoted string is a constant of type String "Live long and prosper." A variable of type String can be given the value of a String object String blessing = "Live long and prosper.";

Concatenation of Strings Concatenation: Using the + operator on two strings in order to connect them to form one longer string If greeting is equal to "Hello ", and javaClass is equal to "class", then greeting + javaClass is equal to "Hello class" Any number of strings can be concatenated together When a string is combined with almost any other type of item, the result is a string "The answer is " + 42 evaluates to "The answer is 42"

Output int answer = 2 + 2; System.out.println(answer); //when argument to println int variable answer is //automatically converted to a string and output as “4” System.out.println(“2 plus 2 is “ + answer); //“+” operator here “concatenates” (connects) two strings.

Output int x = 2; int y = 2; System.out.println(x + y); //expresssion is evaluated and “4” is //printed // “+” operator here is the addition operator!!!

Classes, Objects, and Methods A class is the name for a type whose values are objects Objects are entities that store data and take actions Objects of the String class store data consisting of strings of characters The actions that an object can take are called methods Methods perform an action Methods can return (produce) a value of a single type

Classes, Objects, and Methods Recall: Invoking or calling a method: a method is called into action by writing the name of the calling object, followed by a dot, followed by the method name, followed by parentheses This is sometimes referred to as sending a message to the object The parentheses contain the information (if any) needed by the method This information is called an argument (or arguments)

String Methods The String class contains many useful methods for string-processing applications A String method is called by writing a String object, a dot, the name of the method, and a pair of parentheses to enclose any arguments

String methods Useful String method: testing for equality String s = “hello”; s.equals(“help”); s.equals(“Hello”); What type of value does equals(..) return??

String methods Useful String method: testing for equality String s = “hello”; s.equals(“help”); //returns false s.equals(“Hello”); //returns false

String methods The value returned by a method may be stored in a variable of correct type. String s = “hello”; boolean returnValue = s.equals(“help”); //returns false

String methods equals() is case sensitive (i.e., it distinguishes between upper and lower case) “hello” is not the same as “Hello” (Don’t use == to compare two string objects)

String methods Wouldn’t it be nice to have a method that is like equals() but ignores case? “hello” is the same as “Hello” What would be a good name for such a method?

String s = “HeLlo”; s.equalsIgnoreCase(“hello”); //returns true

More String methods length() startsWith() endsWith() toLowerCase() toUpperCase() charAt() substring()

String method: length() What (type of thing) does length() return?

String method: length() What (type of thing) does length() return? An integer int length() Return type is int No arguments

String Methods If a String method returns a value, then it can be placed anywhere that a value of its type can be used String greeting = "Hello"; int count = greeting.length(); //count’s value is 5

String Methods String greeting = "Hello"; System.out.println("Length is " + greeting.length());

String Indexes

More string methods charAt() char charAt ( int index ) Returns the char value at the specified index (0..N-1). String name = “John”; char c = name.charAt(2); //returns ‘h’

More string methods substring() (Two of them.) String substring ( int beginIndex ) Returns a new string that is a substring of this string (from beginIndex to the end of the string). String substring ( int beginIndex, int endIndex ) Returns a new string that is a substring of this string (from beginIndex..endIndex-1).

substring(..) String example = “this is a long string.”; String substr = example.substring(8); //returns “a long string.”

substring(..) String example = “this is a long string.”; String substr = example.substring(1, 4); //returns “his”

More String methods startsWith() and endsWith() Returns true if the string starts with (ends with) the specified string argument. String s1 = “dish”; String s2 = “dishes”; boolean plural = s1.endsWith(“es”);

More String functions: toLowerCase() and toUpperCase() String s1 = “dIsHeS”; String s2 = s1.toLowerCase(); //s2 is “dishes”

And many more!

Escape Sequences A backslash (\) immediately preceding a character (i.e., without any space) denotes an escape sequence or an escape character The character following the backslash does not have its usual meaning Although it is formed using two symbols, it is regarded as a single character

Escape Sequences

String Processing A String object in Java is considered to be immutable, i.e., the characters it contains cannot be changed There is another class in Java called StringBuffer that has methods for editing its string objects However, it is possible to change the value of a String variable by using an assignment statement String name = "Soprano"; name = "Anthony " + name;