String Class in Java java.lang Class String java.lang.Object java.lang.String java.lang.Object We do not have to import the String class since it comes.

Slides:



Advertisements
Similar presentations
Strings Testing for equality with strings.
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.
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.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
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.
AP Computer Science TOPICS TO DISCUSS.equals() == instanceof operator compareTo Interfaces Abstract Classes.
Java Strings in 10 minutes
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.
CIS 234: Strings. What Is a String? series of characters enclosed in double quotes characters can include letters (lower case and upper case), digits,
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
Fundamental Programming Structures in Java: Strings.
28-Jun-15 String and StringBuilder Part I: String.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.
Class T{ public static int id; public static int num = 5; public int n; public boolean c; public static void setNumberOfBicycles(int val) { num=val; //
Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types.
Strings.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
String Class. Objectives and Goals Identify 2 types of Strings: Literal & Symbolic. Learn about String constructors and commonly used methods Learn several.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Working with string In java Four classes are provided to work with String:1) String 2)String Buffer 3)String Tokenizer 4)String Builder Note: An object.
Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers.
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.
Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,
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.
String Definition A String is a set of characters that behaves as a single unit. The characters in a String include upper-case and lower-case letters,
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
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.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering products.
Strings Mr. Smith AP Computer Science A. What are Strings? Name some of the characteristics of strings: A string is a sequence of characters, such as.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
1 Java Strings Dr. Randy M. Kaplan. 2 Strings – 1 Characters are a fundamental data type in Java It is common to assemble characters into units called.
Strings and ArrayLists April 23, 2012 ASFA AP CS.
CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
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:
String Class in Java java.lang Class String java.lang.Object java.lang.String java.lang.Object We do not have to import the String class since it comes.
17-Feb-16 String and StringBuilder Part I: String.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
The Methods and What You Need to Know for the AP Exam
String and StringBuffer classes
The String Class.
Strings, StringBuilder, and Character
Programming in Java Text Books :
String String Builder.
String Handling in JAVA
Primitive Types Vs. Reference Types, Strings, Enumerations
String Objects & its Methods
Modern Programming Tools And Techniques-I Lecture 11: String Handling
Java Strings Slides provided by the University of Washington Computer Science & Engineering department.
MSIS 655 Advanced Business Applications Programming
תרגול מס' 3 עבודה עם מחרוזות (Strings) מתודות (Methods) העברת פרמטרים
Part a: Fundamentals & Class String
Exposure Java 2015 Pre-AP®CS Edition Chapter 12 Slides String Methods
The compareTo interface
String and StringBuilder
Strings in Java.
In Java, strings are objects that belong to class java.lang.String .
Pre-AP® Computer Science Quiz
String Methods Strings have actions known as method. We will review a few of the methods associated with strings that are a part of the built in Java.
Presentation transcript:

String Class in Java java.lang Class String java.lang.Object java.lang.String java.lang.Object We do not have to import the String class since it comes from java.lang. An object of the String class represents a string of characters. “abcde”;

Two ways to create a String object Create a String literal: String str = "abc"; With the key word new since it is an object String str = new String(“abc”);

String is a Class: When an object is created it creates an Object Reference When an object is created a reference to that object is stored in memory. public class Reference { public static void main(String[]args) { Reference r = new Reference(); System.out.println(r); } 10-3 memory location for object

Storing String objects Java has 2 types of memory for Strings. 1. Objects stored in a Pool: String s = “Sun”; 2. Objects are stored in the Heap : created with the word new. String s = new String(“Sun”); 10-4

10-5 String s1 = "Sun"; String s2 = s1; String s3 = “Sun”; String s4 = new String(“Sun“); String s5 = new String (“Sun”); s1 s2 s4 s5 "Sun" String reference There is only one reference created. All objects share the same reference to the object “Sun”. String reference “Sun" s3 These are created as new objects and do not have the same reference. Every object created is unique and has its own reference. When created this way, Java looks in the pool to see if there is already a String with the same name. If so points to it and has the same reference as other objects with that content. POOL When created WITH THE WORD NEW objects go on the heap. Each object is brand new and has its own reference. It not shared. HEAP

10-6 String s1 = "Hello"; // String literal String s2 = "Hello"; // String literal String s3 = s1; // same reference String s4 = new String("Hello"); // String object String s5 = new String("Hello"); // String object

Why does this matter? String comparison: There are three ways to compare String objects: 1. By equals() method (inherited from Object) 2. By == operator 3. By compareTo() method 10-7

By equals() method: Equals() method compares the original content of the String. Does it have the same content. public boolean equals(Object another) public boolean equalsIgnoreCase(String another) 10-8

Equality using equals(Object o) String n = "Computer"; String s = "Computer"; String t = new String("Computer"); String u = new String("Computer"); String v = new String (“computer”); boolean b = n.equals(s); true boolean b = n.equals(t); true boolean b = n.equals(u); true boolean b = n.equalsIgnoreCase(v); true 10-9

By == operator String n = "Computer"; String s = "Computer"; String t = new String("Computer"); String u = new String("Computer"); n s “Computer" String reference POOL t u String reference “Computer" HEAP

== equality String n = "Computer"; String s = "Computer"; String t = new String("Computer"); String u = new String("Computer"); n == s true n == t false t == u false 10-11

10-12 If you create a String using new it creates a new String and will not reference the same object. String s1 = new String(“Sun”); String s2 = new String(“Sun”); String Reference Sun System.out.println(s1 == s2) ; // false two references created System.out.println(s1.equals(s2)); // true says same thing Sun

compareTo method parameters int compareTo(Object o) // Object o is the object to be compared or int compareTo(String anotherString) // String to be compared What is returned 1.if the two strings are equal to each other returns 0. 2.if argument is > than String return value < 0 (negative number) 3.if the argument is 0 (positive number ).

Lexicographic order: Lexicographic order is a generalization of alphabetical order. In this ordering, numbers come before letters and capital letters come before lower case letters. Lexicographic comparison is like alphabetizing the Strings. numbers uppercase lower case

Lexicographical Upper case characters are regarded as less than lower case characters. "APPLE".compareTo("apple") returns a negative integer. -32  Argument is greater than String A ascii code of 65 < a ascii code of 97 “apple”.compareTo(“APPLE”) returns positive 32  argument less than String a ascii code of 97 > A ascii code of 65 “apple”.compareTo(“apple”) returns 0 equal to each other 1. if the two strings are equal to each other returns if argument is > than String return value < 0 (negative number) 3. if the argument is 0 (positive number 10-15

String str1 = "Abc"; String str2 = "abc"; String str3 = "year"; String str4 = "table"; String str5 = "abc"; System.out.println(str1.compareTo(str2)); System.out.println(str2.compareTo(str1)); System.out.println(str3.compareTo(str4)); System.out.println(str5.compareTo(str2 )); “ABC to “abc argument is > so negative -32 “abc to “Abc” argument is < so positive 32 “year” to “”table” argument is < so positive 5 “abc” to “abc” equal returns Return Value : 1.if the two strings are equal to each other returns 0. 2.if argument is greater than String return value < 0 (negative number) 3.if the argument is less than the string return > 0 (positive number)

String Class in Java java.lang Class String java.lang.Object java.lang.String java.lang.Object We do not have to import the String class since it comes from java.lang. An object of the String class represents a string of characters. “abcde”;

10-18 Empty Strings An empty string has no characters; Its contents are “null”. String s1 = “"; String s2 = new String(); Empty strings

String indexes String is a string of characters. Each letter in the string has its own index location and can be accessed by it. index location c o m p u t e r The length of the String is how many letters it contains: 8 The ending index of the String is length – 1; 7

10-20 Index locations Strings have index locations from 0 to length-1 strawberry // 10 letters in the word // index from 0 to 9 length() = 10 length()-1 = 9 Index starts at 0 and goes to

10-21 String Methods: Page 78 There are many ways to manipulate Strings. Look on page 78 those tested on AP You always use the period to separate the object from the method. s1.length();

10-22 Methods — length() int length (); returns an int Returns the number of characters in the string 6 4 int lenF = f.length(); int lenW = w.length(); Returns: String f = “Flower”; String w = “Wind”;

10-23 Methods — substring String s2 = s.substring (i, k); returns the substring of chars in positions from i to k - 1 String s3 = s.substring (i); returns the substring from i char to the end String s = “strawberry”; raw rawberry String s2 = s.substring (2,5); start at 2 end at 4 String s3 = s.substring (2); start at 2 thru end Returns: strawberry i k strawberry i Strings have index locations from 0 to length-1

String n = "Computer"; String one = n.substring(0,7); String two = n.substring(1,6); String three = n.substring(2,5); String four = n.substring(4); String five = n.substring(3); String six = n.substring(1,n.length()-2); String seven = six.substring(0, n.length()/2); c o m p u t e r s.substring(i, k); returns the substring of chars in positions from i to k - 1 s.substring(i); returns the substring from i char to the end

Given a string, return a new string made of 3 copies of the last 2 chars of the original string. The string length will be at least 2. extraEnd("Hello") → "lololo" extraEnd("ab") → "ababab" extraEnd("Hi") → "HiHiHi“ public String extraEnd(String str) { String end = str.substring(str.length()-2); return end+end+end; } 10-25

Given a string of even length, return the first half. So the string "WooHoo" yields "Woo". firstHalf("WooHoo") → "Woo" firstHalf("HelloThere") → "Hello" firstHalf("abcdef") → "abc“ public String firstHalf(String str) { String firstHalf = str.substring(0,str.length()/2); return firstHalf; } 10-26

10-27 Methods — Concatenation String s1 = “obi”; String s2 = “wan”; String result = s1 + s2; obiwan String result = s1.concat (s2); the same as s1 + s2 obiwan

10-28 Methods — Find (indexOf) String date ="July 5, :28:19 PM"; date.indexOf ('J'); 0 date.indexOf ('2'); 8 date.indexOf ("2012"); 8 date.indexOf ('2', 9); 11 date.indexOf ("2020"); - 1 date.lastIndexOf ('2'); 15 Returns: (not found) (starts searching at position 9) Index of return the index location of the first occurrence of the character requested.