16 Strings.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Advertisements

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.
Arrays, A1 COMP 401, Fall 2014 Lecture 4 8/28/2014.
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.
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.
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.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About 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.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Strings Carol Yarbrough AP Computer Science Instructor Alabama School of Fine Arts.
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.
Java Programming Java Basics. Data Types Java has two main categories of data types: –Primitive data types Built in data types Many very similar to C++
Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
Strings and ArrayLists April 23, 2012 ASFA AP CS.
Introduction to Strings CSIS 1595: Fundamentals of Programming and Problem Solving 1.
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.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
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.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
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
Computer Programming ||
Strings, Characters and Regular Expressions
Strings, Characters and Regular Expressions
String class.
Data Types, Identifiers, and Expressions
Programming in Java Text Books :
JavaScript Syntax and Semantics
Expressions and Control Flow in JavaScript
Primitive Types Vs. Reference Types, Strings, Enumerations
String Objects & its Methods
String and StringBuilder
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
Arrays, Casting & User Defined Types
String and StringBuilder
Classes & Objects: Examples
CMSC 202 Java Primer 2.
String and StringBuilder
Arrays .
Arrays ICS2O.
String and StringBuilder
CS2011 Introduction to Programming I Strings
Topics Basic String Operations String Slicing
String methods 26-Apr-19.
Visual Programming COMP-315
Topics Basic String Operations String Slicing
String Class.
In Java, strings are objects that belong to class java.lang.String .
Topics Basic String Operations String Slicing
Unit-2 Objects and Classes
Presentation transcript:

16 Strings

Previously Classes Methods Scoping public private static

Overview String as an Object String as an Array of Characters Equalities Length Index Substring

The Java String Class Java String is the representation of a group of consecutive characters Simplest String is a String Literal String strGreeting = "Hello world!"; You can also create a String from an array of Characters char[] strHelloArray = {'h','e','l','l','o','.'}; String strHelloString = new String(strHelloArray); System.out.println(strHelloString);

Immutable Java String Class is immutable Once created and initialized, cannot be changed on the same reference The only way to change the value of immutable variables is to create a new object and point to that instead

Immutable String strCityName; // define the variable Memory String strCityName; // define the variable strCityName = "Valencia"; System.out.println(strCityName); strCityName = "London"; strCityName = "Madrid"; a L o n d V l e c i R m M r Valencia London Madrid

Immutable By default variables are mutable Can also be made immutable by declaring them final The String class has a number of methods that appear to modify strings Since strings are immutable, what these methods really do is create and return a new string that contains the result of the operation

What is the result of the comparison? Equalities A String may contain the same characters without being the same object String str1stPersonsName = "John"; String str2ndPersonsName = "Peter"; String str3rdPersonsName = new String("John"); String str4rdPersonsName = str1stPersonsName; What is the result of the comparison? str1stPersonsName == str2ndPersonsName str1stPersonsName == str3rdPersonsName str2ndPersonsName == str3rdPersonsName str1stPersonsName == str4thPersonsName false true

Equalities Use the method equals(String) to compare the contents of two strings Use operator == to see if both string objects are the same Not that they contain the same group of characters but they are the same object

What is the result of the equalities? String str1stPersonsName = "John"; String str2ndPersonsName = "Peter"; String str3rdPersonsName = new String("John"); String str4rdPersonsName = str1stPersonsName; What is the result of the equalities? str1stPersonsName.equal(str2ndPersonsName) str1stPersonsName.equal(str3rdPersonsName) str2ndPersonsName.equal(str3rdPersonsName) str1stPersonsName.equal(str4thPersonsName) false true

Properties int length() char charAt(int) Returns number of characters that compose the string String str1stPersonsName = "John"; str1stPersonsName.length() char charAt(int) Returns the character at the specified position in the base string str1stPersonsName.charAt(3)  4  'c'

Searching Strings String substring(...) int indexof(...) Return a sub-string of the base string str1stPersonsName.subString(1, 2); int indexof(...) Returns the index where the specified string exists within the base string str1stPersonsName.indexof("hn"); str1stPersonsName.indexof("hl");  "oh" starting position length  2  -1

Split You can split up strings to get an array of strings using the split method This uses a regular expression to break up a string So you can create an array of words in a sentence by searching for the space character HOWEVER a special character needs to be escaped with a "\" but since "\" is also a special character in Java, you need to escape it again with another "\" ! Any string in Java that uses any of the special characters like, “ \ must preceded by \ to escaped is special meaning and use it as character, e.g. "Valencia \"Spain\" Europe"

Split String strCityNames = "Valencia London Madrid Rome"; String astrCityNames; astrCityNames = strCityNames.split(" "); System.out.println("Num cities " + astrCityNames.length()); for (String strCityName : astrCityNames) { System.out.println(strCityName ); } // end for Num cities 4 Valencia London Madrid Rome