Lecture Set 6 The String and DateTime Data Types Part B – Characters and Strings String Properties and Methods.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Advertisements

Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Constants and Data Types Constants Data Types Reading for this class: L&L,
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Chapter 5 new The Do…Loop Statement
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Objectives You should be able to describe: Data Types
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Characters In Java, single characters are represented.
CS0004: Introduction to Programming Variables – 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.
Input, Output, and Processing
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Chapter 5 The String and DateTime Data Types. Class 5: String & DateTime Understand the use of characters and strings Call members of the String class.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.
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.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
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.
Object-Oriented Application Development Using VB.NET 1 Chapter 4 VB.NET Programming with Supplied Classes.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Chapter 23 The String Section (String Manipulation) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Tutorial 8: Manipulating Strings1 Tutorial 8 Manipulating Strings.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
IS 350 Strings and Dates. Slide 2 Objectives Understand character-encoding systems and use the Char data type Use the String data type Call members of.
© 2010 Lawrenceville Press Slide 1 Chapter 5 The Do…Loop Statement  Loop structure that executes a set of statements as long as a condition is true. 
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Topics Designing a Program Input, Processing, and Output
Strings, Characters and Regular Expressions
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
University of Central Florida COP 3330 Object Oriented Programming
Multiple variables can be created in one declaration
VB Math All of your favorite mathematical operators are available in VB: + Addition - Subtraction * Multiplication / Division \ Integer Division Mod Modulo.
Data Types and Variables Part D – Common Methods (ToString and Parse)
Lecture 4 Using Classes Richard Gesick.
Variables and Arithmetic Operations
Chapter 7: Strings and Characters
Object Oriented Programming
Introduction to C++ Programming
Representation and Manipulation
Basic String Operations
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2 Primitive Data Types and Operations
Switch, Strings, and ArrayLists in Java
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

Lecture Set 6 The String and DateTime Data Types Part B – Characters and Strings String Properties and Methods

Slide 2 Objectives Understand more advanced uses of characters and strings (more than their representations, but lots of methods defined on strings) Understand character encoding systems and the char data type Use the String data type Call members of the String class to manipulate string data Use strings and dates to create a form letter 7/23/2016 3:17 AM

Slide 3 Introduction to Character Encodings The English character set is relatively small Letters, digits, and a few special characters International character sets are more complex German has umlauts French has grave accents Many other diacritical marks exist Some languages have entirely unique character sets 7/23/2016 3:17 AM

Slide 4 The ASCII Character Set Early computers used different character sets making data communication between computers difficult or impossible ASCII was created in 1961 to supply a standard character set shared among computers An encoding scheme is a way of uniquely representing every character in a language with a unique binary value ASCII is but one encoding scheme 7/23/2016 3:17 AM

Slide 5 The Unicode Character Set ASCII cannot effectively represent the characters of international languages Unicode (“Unification code”) is an international character set, which stores a character in an unsigned 2-byte value Each unique character is called a code point 65,536 characters (code points) are possible The first 128 code points store ASCII characters The second 128 code points store special characters The remaining code points store symbols and diacritical marks Unicode allows us to represent multi-lingual character encoding systems 7/23/2016 3:17 AM

Slide 6 Introduction to the char Data Type The char data type stores a single Unicode character in one 16-bit (2-byte) value The same syntax is used to declare a char as any other data type char is a primitive data type char and string are not the same char c = “A”; // conversion error string s = ‘A’; // conversion error 7/23/2016 3:17 AM

Slide 7 Conversions to the Char Data Type The System.Convert.ToChar method converts a String or an Integer to a Char The Integer value corresponds to the Unicode code point For example, the letter “A” has an integer value 65. What is the value of “Z”; of “0”; of Examples: LetterA = System.Convert.ToChar(65); LetterA = System.Convert.ToChar("A"); In these examples the result is the storing of the Unicode character A in the variable LetterA How is this handled in Java? 7/23/2016 3:17 AM

Slide 8 More on the String Data Type A string is a class with an indexer [index], a length property and lots of methods (see text for details) NOTE: Strings created from the string class are immutable That is they are of fixed length and value If you reassign a string value to a string variable, the original string is deallocated and a new one is created The Text property of a TextBox stores a string, for example The String data type is a reference type 7/23/2016 3:17 AM

Slide 9 Storing Data in a String (VB) 7/23/2016 3:17 AM

Slide 10 Declaring a string The syntax to declare a string is the same as the syntax to declare any other variable Multiple variables can be declared in the same statement Double quotation marks surround literal initialization values 7/23/2016 3:17 AM

Slide 11 Declaring a string (Example) Declare and initialize variables having a data type of String private string prefix; private string firstName, lastName; private string currentName = "Joe Smith“; 7/23/2016 3:17 AM

Slide 12 Strings and Assignment Statements Strings can be assigned just as numeric variables can be assigned Double quotation marks surround literal values The data types of both sides of an assignment statement must be the same Examples: string personName; string currentName = "Joe Smith“; personName = currentName; personName = txtName.Text; 7/23/2016 3:17 AM

Slide 13 Strings and Type Conversion Numeric data must often be converted to a String Call the ToString method on any data type to convert a value to a string For numeric data types, a format specifier can be passed as an argument Example to convert a value and format it as currency: double dataValue = ; string formattedOutputValue; formattedOutputValue = dataValue.ToString("C"); // Currency 7/23/2016 3:17 AM

Slide 14 Converting Strings to Numeric Types The methods of the System.Convert class convert strings to numeric data types An exception will occur if the value cannot be converted Example to convert the Text property of a text box: int integerDataValue; float floatDataValue; integerDataValue = System.Convert.ToInt32(txtInput.Text); floatDataValue = System.Convert.ToFloat(txtInput.Text); 7/23/2016 3:17 AM

Slide 15 String Operations Strings can be appended together (concatenated) The plus (+) operator is the concatenation operator Example: string firstName = "John“; string lastName = "Brown“; string fullName; fullName = firstName + " " + LastName; 7/23/2016 3:17 AM

Slide 16 Using Control Characters (Example) Embed a carriage return in a string The string John and Brown will appear on separate lines txtOutput.Text = "John" + “\n” + "Brown“; 7/23/2016 3:17 AM

Slide 17 Working with Multiline Text Boxes Text appears on multiple lines when the MultiLine property is set to True Scroll bars appear based on the following settings for the ScrollBars textbox property: None – no scroll bars appear Horizontal – a horizontal scroll bar appears across the bottom of the text box Vertical – a vertical scroll bar appears along the right side of the text box The WordWrap property defines whether lines wrap on word boundaries 7/23/2016 3:17 AM

Slide 18 Effect of the WordWrap Property 7/23/2016 3:17 AM

Slide 19 String Manipulation Methods (Introduction) Determine the length of a string ( Length ) Convert the case of a string ( ToUpper, ToLower ) Work with parts of a string ( IndexOf, Substring, Insert, Remove, Replace ) Delete leading and trailing spaces ( Trim ) Some of these are a bit tricky to use – you have to read the interface descriptions in the text carefully 7/23/2016 3:17 AM

Slide 20 Determining Length of a String ( Length ) The Length method of the String class returns the length of a string The method is one-based so a string with one character has a length of one An empty string has a length of zero Example (The string is 14 characters long): string fullName = "Mr. Ed (Jones)“; string lengthOfString; lengthOfString = fullName.Length(); 7/23/2016 3:17 AM

Slide 21 Converting the Case of a String ( ToUpper, ToLower ) The ToUpper method converts all characters to upper case The ToLower method converts all characters to lower case 7/23/2016 3:17 AM

Slide 22 Converting the Case of a String (Example) string currentString = "John Brown“; string upperString, LowerString; upperString = currentString.ToUpper(); lowerString = currentString.ToLower(); Example Output: JOHN BROWN john brown 7/23/2016 3:17 AM

Slide 23 The IndexOf Method (Syntax) Powerful, but tricky Headers … public int IndexOf(string value) public int IndexOf(string value, int startIndex) public int IndexOf (string value, int startIndex, int count) 7/23/2016 3:17 AM

Slide 24 The IndexOf Method (Syntax, continued) The IndexOf method searches for a substring within a string The method returns the character position of the first character in the string where the substring was found The method returns -1 if the substring was not found The first argument named value contains the substring The optional second argument contains the starting position in the string where the search begins The optional third argument contains the number of characters to search 7/23/2016 3:17 AM

Slide 25 The IndexOf Method (Examples) Search for a substring within a string int position; string states = "Arizona California Nevada“; position = states.IndexOf("California") // 8 position = states.IndexOf("California", 10) // –1 position = states.IndexOf("Arizona", 0, 5) // –1 7/23/2016 3:17 AM

Slide 26 The Substring Method (Syntax) The Substring method selects one or more characters from a string public string Substring(int startIndex) public string Substring(int startIndex, int length) The startIndex argument contains the character position where searching will begin The length argument contains the number of characters to search How can we have two functions with same name? How does the compiler tell them apart? What would the Intellisense descriptions look like? 7/23/2016 3:17 AM

Slide 27 The Substring Method (Example) Select the numeric parts of a Social Security number string SSN = " “; string Part1, Part2, Part3; Part1 = SSN.Substring(0, 3); // 555 Part2 = SSN.Substring(4, 2); // 12 Part3 = SSN.Substring(7, 4); // /23/2016 3:17 AM

Slide 28 Operation of the Substring Method 7/23/2016 3:17 AM

Slide 29 The Insert Method (Syntax) The Insert method inserts one string into another string public string Insert(int startIndex, string value) The startIndex argument contains the character position in the existing string where the new string will be inserted The value is 0-based value contains the string to be inserted 7/23/2016 3:17 AM

Slide 30 The Insert Method (Example) Insert dashes into a Social Security number string SSN = " “; SSN = SSN.Insert(5, "-"); SSN = SSN.Insert(3, "-"); 7/23/2016 3:17 AM

Slide 31 Operation of the Insert Method 7/23/2016 3:17 AM

Slide 32 The Remove Method The Remove method deletes one or more characters from a string public string Remove(int startIndex); public string Remove(int startIndex, int count); The first method removes all characters from startIndex to the end of the string The second method removes count characters starting at startIndex 7/23/2016 3:17 AM

Slide 33 The Remove Method (Example) Remove the area code from a telephone number string Telephone = " “; string TelephoneNoAreaCode; TelephoneNoAreaCode = Telephone.Remove(0, 4); 7/23/2016 3:17 AM

Slide 34 Operation of the Remove Method 7/23/2016 3:17 AM

Slide 35 The Replace Method The Replace method replaces one character with another character or one string pattern with another string pattern public string Replace(char oldChar, char newChar); public string Replace(string oldVal, string newVal) In the first method, the character in oldChar is replaced by newChar In the second method, the string in oldVal is replaced by the string in newVal 7/23/2016 3:17 AM

Slide 36 The Replace Method (Example) Replace a space with no character (nothing) string inputString As String = "A B C D E“; inputString = inputString.Replace(" ", ""); 7/23/2016 3:17 AM