Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.

Slides:



Advertisements
Similar presentations
Java
Advertisements

Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
Strings in Java 1. strings in java are handled by two classes String &
Strings Testing for equality with 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.
Java Programming Strings Chapter 7.
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.
Java Strings in 10 minutes
©2004 Brooks/Cole Chapter 7 Strings and Characters.
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.
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
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.
28-Jun-15 String and StringBuilder Part I: String.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
The java.lang Package chapter 8 Java Certification Study Group February 2, 1998 Seth Ladd.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
String Manipulation Chapter 15 This chapter explains the String facilities. You have already seen some of the main methods of the String class.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
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.
1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
1 The String Class F Constructing a String: F Obtaining String length and Retrieving Individual Characters in a string F String Concatenation (concat)
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.
CHAPTER 8 File Input Output Part 1: String. The String Class  Constructing a String: String message = "Welcome to Java“; String message = new String("Welcome.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
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.
CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings.
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:
17-Feb-16 String and StringBuilder Part I: String.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings Chapter.
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.
Chapter 8 String Manipulation
The Methods and What You Need to Know for the AP Exam
Introduction to programming in java
String and StringBuffer classes
Java String Methods - Codehs
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
String class.
EKT 472: Object Oriented Programming
Programming in Java Text Books :
String Handling in JAVA
Primitive Types Vs. Reference Types, Strings, Enumerations
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Tonga Institute of Higher Education
Lecture 07 String Jaeki Song.
String methods 26-Apr-19.
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 .
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 extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE

2 String Accessors (What does this mean?) message = "Welcome"; message.length() (gives us what?)

3 Retrieving Characters in a String Cannot use message[0] (strings are immutable) Use message.charAt(index) Index starts from 0 Is charAt() a public or private method? Can you write a loop for a string to print out each char?

4 String Comparisons String s1 = new String("Welcome“); String s2 = "Welcome"; if (s1.equals(s2)){ // do s1 and s2 have the same contents? } if (s1 == s2) { // do s1 and s2 have the same reference? }

5 String Comparisons, cont. compareTo(Object object) String s1 = new String("Welcome“); String s2 = "hello"; if (s1.compareTo(s2) > 0) { // is s1 greater than s2? } else if (s1.compareTo(s2) == 0) { // do s1 and s2 have the same contents? } else { // s1 is lexicographically less than s2 }

Strings are Immutable! Immutable means we can’t change them. Can’t do: String str = “hello”; str[3] = ‘b’; So then why does this work? String str = "Hello"; System.out.println(str); //Prints Hello str = "Help!"; System.out.println(str); //Prints Help!

Why can I do this? (Or, isn’t this mutating the string?) String str = "Mississippi"; System.out.println(str); //Prints Mississippi str = str.replace("i", "!"); System.out.println(str); //Prints M!ss!ss!pp!

String methods: charAt(int index) – gets the character at the index within the string and returns the char indexOf(char c)- sees if a character is within a string and returns an int representing the index indexOf(char c, int fromindex) - sees if a character is within a string from fromindex onwards length() – returns number of characters in a string (note that for strings this is a method. What is it for arrays?) substring(int beginindex) – extracts and returns a substring from a string, starting at beginindex and going to the end of the string substring(int beginindex, int endindex) – extracts and returns a substring from beginindex to endindex toLowerCase() – returns a string that is the lower case version of the string toUpperCase() – returns a string that is the upper case version of the string trim() - removes white space (space char) from both ends of a string and returns that string toCharArray() – converts string to character array and returns that array equalsIgnoreCase(String otherstring) – compares string with otherstring and returns Boolean value (true if equals, false if doesn’t) compareToIgnoreCase(String otherstring) – compares string with otherstring, and returns int (1 if string is “greater”, 0 if equal, and -1 if “less than”.

toString() method Printing objects: – If you print an object that doesn’t have a toString() method, you print out the hash representation of the object Not what we want. – All java built-in objects have a toString method implemented. E.g., Color x = new Color(255,0,0); //Color is a built-in java class -RGB System.out.println(x.toString()); Will print out: java.awt.Color[r=255,g=0,b=0] (this is the string that the toString method explicitly created and returned.) System.out.println(x)

toString() The println() method can take an Object as an argument. This version will implicitly call toString() for you and print the result. Meaning, you can do: Color x = new Color( 255, 0, 0 ); System.out.println( x ); Also happens with concatenation, e.g.,: Color x = new Color( 255, 0, 0 ); String str = “This is color: “; str += x; toString() is a method that you should write when you create a class definition. will automatically be used as above.

Wanna Try? (Quickly write toString()) public class StudentInfo { private String first; private String last; private String[] schedule; public StudentInfo(String f, String l, String[] sched) { first = f; last=l; schedule = sched; } //… }

My toString() public String toString() { String str = ""; str += first + "\t"+ last + "\n"; for (int i = 0; i < schedule.length; i++) { str += schedule[i] "\t"; } return(str); } Note the \t and \n These are known as “escape sequences” \t adds a tab \n adds a new line (makes the printout go to the next line.