Strings.

Slides:



Advertisements
Similar presentations
ACM/JETT Workshop - August 4-5, Classes, Objects, Equality and Cloning.
Advertisements

Strings Testing for equality with 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.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Java Strings in 10 minutes
©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.
COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
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.
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, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
28-Jun-15 String and StringBuilder Part I: String.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Third Edition
Chapter 2 types, variables, methods Pages Horstmann.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
String Escape Sequences
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 C++ to Java A whirlwind tour of Java for C++ programmers.
Strings Carol Yarbrough AP Computer Science Instructor Alabama School of Fine Arts.
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,
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.
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.
Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,
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.
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.
ICT Introduction to Programming Chapter 4 – Control Structures I.
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.
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:
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
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.
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
String class.
Chapter 3 Branching Statements
Multiple variables can be created in one declaration
Programming in Java Text Books :
String Objects & its Methods
String and StringBuilder
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
String and StringBuilder
CMSC 202 Java Primer 2.
String and StringBuilder
String and StringBuilder
String methods 26-Apr-19.
Strings in Java.
Introduction to Programming
Each object belongs to a class
In Java, strings are objects that belong to class java.lang.String .
Strings in Java Strings in Java are also a reference type.
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:

Strings

An object of type String is a sequence of characters An object of type String is a sequence of characters. All string literals are implemented as instances of this class. A string literal consists of zero or more characters, including escape sequences, surrounded by double quotes. The quotes are not part of the String object. String objects are immutable, which means that there are no methods to change them after they’ve been constructed.

String length The length method counts the number of characters in a string. This method can be applied to any object of type String. Example: String greeting = “Hello, World!”; int n = greeting.length(); After the instructions in the length method are executed, n is set to ?????

When you apply the toUpperCase method to a String object, the method creates another String object that contains the characters of the original string, with lowercase letters converted to uppercase. The toLowerCase method works the same way, but converts all uppercase letters to lowercase. Example: String river = “Mississippi”; String bigRiver = river.toUpperCase(); MISSSISSIPPI String littleRiver = river.toLowerCase(); misssissippi

Constructing String Objects A string can be initialized like a primitive type: String s = “abc”; Or String s = new String(“abc”); You can reassign a String reference: String s = “John”; or String s = new String(“John”); s = “Harry”; s = new String(“Harry”); This is consistent with the immutable feature of String objects. “John” has not been changed; he has been discarded. It is also OK to reassign s as follows: s = s + “Smith”; so now s refers to “Harry Smith”.

Concatenation The concatenation operator, +, which operates on String objects. Given two strings operands lhs, rhs, lhs + rhs produces a single String consisting of lhs followed by rhs. lhsrhs

Comparison of String Objects There are two ways to compare String objects: Use the equals method that is inherited from the Object class and overridden to do the correct thing: if (string1.equals(string2))… This will return true if string1 and string2 are identical strings, false otherwise.

Use the compareTo method. The String class implements Comparable, which means that the compareTo method is provided in String. This method compares strings in dictionary order: If string1.compareTo(string2) < 0, then string1 precedes string2 in the dictionary. If string1.compareTo(string2) > 0, then string1 follows string2 in the dictionary. If string1.compareTo(string2) == 0, then string1 and string2 are identical. Be aware that Java is case-sensitive. So if s1 is “cat” and s2 is “Cat”, s1.equals(s2) will return false.

Characters are compared according to their positions in the ASCII chart. All you need to know is that all digits precede all capital letters, which precede all lowercase letters. “5” comes before “R”, which comes before “a”.

String substring(int startIndex) Returns a new string that is a substring of this string. The substring starts with the character at startIndex and extends to the end of the string. The first character is at index zero. The method throws a StringIndexOutOfBoundsException if startIndex is negative or larger than the length of the string.

String substring(int startIndex, int endIndex) Returns a new string that is a substring of this string. The substring starts at index startIndex and extends to the character at endIndex -1. startIndex is the first character that you want; endIndex is the first character that you don’t want. The method throws a StringIndexOutOfBoundsException if startIndex is negative, or endIndex is larger than the length of the string, or startIndex is larger than endIndex.

int indexOf(String str) Returns the index of the first occurrence of str within this string. If str is not a substring of this string, -1 is returned. This method throws a NullPinterExecption if str is null.

Consider these declarations: Integer intOb = new Integer(3); Object ob = new Integer(4); Double doubleOb = new Double(3.0); Which of the following will not cause an error? if ((Integer) ob.compareTo(intOb) < 0) … if (ob.compareTo(intOb) < 0)… if(intOb.compareTo(doubOb) < 0)… if (intOb.compareTo(ob) < 0) … if (intOb.compareTo((Integer) ob) < 0)… Answer is e

Refer to these declarations: Integer k = new Integer(8); Integer m = new Integer(4); Which test will not generate an error? if (k.intValue() == m.intValue())… if ((k.intValue()).equals(m.intValue()))… if ((k.toString()).equals(m.toString()))… I only II only III only I and III only I, II, and III Answer is d

Consider these declarations: String s1 = “crab”; String s2 = new String(“crab”); String s3 = s1; Which expression involving these strings evaluates to true? s1 ==s2 s1.equals(s2) s3.equals(s2) I only II only II and III only I and III only I, II, and III Answer is c

Suppose that strA = “TOMATO”, strB = “tomato” and strC = “tom”. Given that “A” comes before “a” in dictionary order, which is true? strA.compareTo(strB) < 0 && strB.compareTo(strC) < 0 strB.compareTo(strA) < 0 || strC.compareTo(strA) < 0 strC.compareTo(strA) < 0 && strA.compareTo(strB) < 0 !(strA.equals(strB)) && strC.compareTo(strB) < 0 !(strA.equals(strB)) && strC.compareTo(strA) < 0 Answer is D

This question refers to the following declaration: String line = “Some more silly stuff on strings!”; //the words are separated by a single space What string will str refer to after execution of the following? int x = line.indexOf(“m”); String str = line.substring(10,15) + line.substring(25, 25 + x); “sillyst” “sillystr” “silly st” “silly str” “silliystrin” Answer is A

Consider the following two lines of code. String s1 = “testing” + “123”; String s2 = new String(“testing 123”); s1 and s2 are both references to the same String object The line declaring s2 is legal Java; the line declaring s1 will produce a syntax error S1 and s2 are both references to different String objects S1 and s2 will compare “equal” None of the above. Answer is c