Unit-2 Objects and Classes

Slides:



Advertisements
Similar presentations
Java
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
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 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.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text I/O.
Arrays, A1 COMP 401, Fall 2014 Lecture 4 8/28/2014.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
String class  Construct a string  String str = new String(“welcome”);  Char[] charr = {‘G’, ‘o’, ‘o’, ‘d’};  String mes = new String(charr);  A full.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Primitive Types Java offers a number of primitive types eg.) int, short, long double, float char A variable which is declared as a primitive type stores.
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, 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.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Screen Scraping Application Introducing String Processing.
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.
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.
String String Builder. System.String string is the alias for System.String A string is an object of class string in the System namespace representing.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering products.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
Lab String Concatenation String s3 = s1.concat(s2); String s3 = s1 + s2; s1 + s2 + s3 + s4 + s5 same as (((s1.concat(s2)).concat(s3)).concat(s4)).concat(s5);
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.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
The String Class.
Strings, Characters and Regular Expressions
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Strings.
JavaScript Objects.
String and String Buffers
Chapter 10 Thinking in Objects
String String Builder.
Primitive Types Vs. Reference Types, Strings, Enumerations
Advanced Programming Behnam Hatami Fall 2017.
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Advanced String handling
Part a: Fundamentals & Class String
Chapter 9 Strings and Text I/O
Exposure Java 2015 Pre-AP®CS Edition Chapter 12 Slides String Methods
String class and its objects
Java – String Handling.
Coding Concepts (Data- Types)
16 Strings.
Chapter 9 Strings.
Lecture 07 String Jaeki Song.
String.
Strings in Java.
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
Switch, Strings, and ArrayLists in Java
In Java, strings are objects that belong to class java.lang.String .
Strings in Java Strings in Java are also a reference type.
Pre-AP® Computer Science Quiz
What We Want To Do User enters: Mary Smith
Presentation transcript:

Unit-2 Objects and Classes String Class

String Class A string is a sequence of characters. In many languages, strings are treated as an array of characters, but in Java a string is an object. The String class has 11 constructors and more than 40 methods for manipulating strings.

Constructing a String

Immutable Strings and Interned Strings

String Comparisons

String Comparisons(Cont.) Using equals Function:

String Comparisons(Cont.) Using compare To Function:

Program(Equal to & Compare TO)

Program(regionMatches() )

Program (endWith)

Program (startWith)

String Length, Characters, and Combining Strings The String class provides the methods for obtaining length, retrieving individual characters, and concatenating strings.

Program

Obtaining Substrings You can also obtain a substring from a string using the substring method in the String class.

Example

Converting, Replacing, and Splitting Strings The String class provides the methods for converting, replacing, and splitting strings, as shown in Figure.

Program

Example(Split () )

String Matching

Finding a Character or a Substring in a String The String class provides several overloaded indexOf and lastIndexOf methods to find a character or a substring in a string.

Program

Conversion between Strings and Arrays

Formatting Strings

Converting Characters and Numeric Values to Strings There are several overloaded versions of the valueOf method that can be used to convert a character and numeric values to strings with different parameter types, char, double, long, int, and float.