Strings and ArrayLists April 23, 2012 ASFA AP CS.

Slides:



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

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.
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.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
1 Strings and String Operations Overview l Creating String Objects l Substring methods l The Concatenation Operator l Strings are Immutable l Other Methods.
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.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
28-Jun-15 String and StringBuilder Part I: String.
The String Class. Objectives: Learn about literal strings Learn about String constructors Learn about commonly used methods Understand immutability of.
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
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; //
1 Programming Section 11 James King 12 August 2003.
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.
Strings.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
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.
From C++ to Java A whirlwind tour of Java for C++ programmers.
Strings Carol Yarbrough AP Computer Science Instructor Alabama School of Fine Arts.
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.
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.
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.
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.
ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”
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.
17-Mar-16 Characters and Strings. 2 Characters In Java, a char is a primitive type that can hold one single character A character can be: A letter or.
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. For example,
String. 2 Objectives Discuss string handling –System.String class –System.Text.StringBuilder class.
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
Java: Base Types All information has a type or class designation
The String Class.
Java: Base Types All information has a type or class designation
Java Variables and Types
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
String class.
String String Builder.
String Class.
Strings Chapter 6.
Primitive Types Vs. Reference Types, Strings, Enumerations
EE422C - Software Design and Implementation II
String Objects & its Methods
String and StringBuilder
Chapter 7: Strings and Characters
Object Oriented Programming
String and StringBuilder
String and StringBuilder
16 Strings.
Lecture 10 Strings CSE /26/2018.
String and StringBuilder
CS2011 Introduction to Programming I Strings
String methods 26-Apr-19.
Switch, Strings, and ArrayLists in Java
String Class.
In Java, strings are objects that belong to class java.lang.String .
Strings in Java Strings in Java are also a reference type.
Unit-2 Objects and Classes
Presentation transcript:

Strings and ArrayLists April 23, 2012 ASFA AP CS

String class facts An object of the String class represents a string of characters. The String class belongs to the java.lang package, which does not require an import statement. Like other classes, String has constructors and methods. Unlike other classes, String has two operators, + and += (used for concatenation). Strings are immutable

Strings Strings are objects, and thus references String s;// no string String s = new String("Hi");// string String s = "Hi";// string String s = "";// string

Literal Strings are anonymous objects of the String class are defined by enclosing text in double quotes. “This is a literal String” don’t have to be constructed. can be assigned to String variables. can be passed to methods and constructors as parameters. have methods you can call.

Literal String examples //assign a literal to a String variable String name = “Robert”; //assign literals with escape sequences String twoLine = “first \nsecond line”; String quote = " He said, \" Yes\"."; String bksl = “This is backslash: \\”; //calling a method on a literal String char firstInitial = “Robert”.charAt(0); //calling a method on a String variable char firstInitial = name.charAt(0);

Immutability Once created, a string cannot be changed: none of its methods changes the string. Such objects are called immutable. Immutable objects are convenient because several references can point to the same object safely: there is no danger of changing an object through one reference without the others being aware of the change.

Empty Strings An empty String has no characters. It’s length is 0. Not the same as an uninitialized String. String word1 = ""; String word2 = new String(); private String errorMsg; errorMsg is null Empty strings

Changing String Values Strings have no methods to let you change an existing character in a string. – To change a string you have to reassign the variable to a new string. – example: String greeting = “Hello”; greeting = greeting.substr(0,4) + “!”; Result: greeting is now “Hell!”

Comparing String Values Because String variables hold memory addresses, you cannot make a simple comparison of the contents The String class provides a number of methods – equals() method – equalsIgnoreCase() method – compareTo() method

Comparing String Values equals() method- Evaluates the contents of two String objects to determine if they are equivalent – This method returns true if the two String objects have identical contents – Can take either a variable String object or a literal string as its argument s1.equals(s2); s1.equals(“Hello”);

Comparing String Values compareTo() method- Used to compare two Strings – Returns zero only if the two Strings hold the same value – If there is any difference between the Strings, a negative number is returned if the calling object is “less than” the argument – A positive number is returned if the calling object is “more than” the argument – Strings are considered “less than” or “more than” each other based on their Unicode values

String Equality String s1 = new String("Hi"); String s2 = new String("Hi"); System.out.println( s1 == s2 ); // false System.out.println( s1.equals(s2) ); // true String reference "Hi" s1 String reference s2

Methods — substring “lev" “mutable" "" (empty string) ”television".substring (2,5); “immutable".substring (2); “bob".substring (3); Returns: television i k television i String subs = word.substring (i, k); – returns the substring of chars in positions from i to k-1 String subs = word.substring (i); – returns the substring from the i-th char to the end Returns a new String by copying characters from an existing String.

Methods — Concatenation String word1 = “re”, word2 = “think”; word3 = “ing”; int num = 2; String result = word1 + word2; //concatenates word1 and word2 “rethink“ String result = word1.concat (word2); //the same as word1 + word2 “rethink“ result += word3; //concatenates word3 to result “rethinking” result += num; //converts num to String //and concatenates it to result “rethinking2”

Methods — Find (indexOf) String name =“President George Washington"; date.indexOf (‘P'); 0 date.indexOf (‘e'); 2 date.indexOf (“George"); 10 date.indexOf (‘e', 3); 6 date.indexOf (“Bob"); - 1 date.lastIndexOf (‘e'); 15 Returns: (not found) (starts searching at position 3)

Some Other String Methods

Object.toString() Every class implements toString( ) because it is defined by Object. The default implementation of toString( ) is seldom sufficient. Most classes override toString( ) and provide string representations. Fortunately, this is easy to do.