String. 2 Objectives Discuss string handling –System.String class –System.Text.StringBuilder class.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
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.
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.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
28-Jun-15 String and StringBuilder Part I: String.
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.
JavaScript, Third Edition
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Working with Strings Lecture 2 Hartmut Kaiser
Primitive Types Java offers a number of primitive types eg.) int, short, long double, float char A variable which is declared as a primitive type stores.
The Fundamentals of C++ Chapter 2: Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Strings Carol Yarbrough AP Computer Science Instructor Alabama School of Fine Arts.
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Chapter 7: Characters, Strings, and the StringBuilder.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
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.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
Strings and ArrayLists April 23, 2012 ASFA AP CS.
Strings in Python. Computers store text as strings GATTACA >>> s = "GATTACA" s Each of these are characters.
Introduction to Strings CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style.
Text Files and String Processing
CSC Java Programming, Fall, 2008 Week 3: Objects, Classes, Strings, Text I/O, September 11.
17-Feb-16 String and StringBuilder Part I: String.
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
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.
Mr. Crone.  Use print() to print to the terminal window  print() is equivalent to println() in Java Example) print(“Hello1”) print(“Hello2”) # Prints:
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
The String Class.
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Working with Strings Lecture 2 Hartmut Kaiser
University of Central Florida COP 3330 Object Oriented Programming
String and String Buffers
String String Builder.
Java Review: Reference Types
Arrays in C.
Advanced Programming Behnam Hatami Fall 2017.
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
String and StringBuilder
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
String and StringBuilder
Working with Strings Lecture 2 Hartmut Kaiser
Week 9 – Lesson 1 Arrays – Character Strings
CMSC 202 Java Primer 2.
String and StringBuilder
16 Strings.
Python Primer 1: Types and Operators
elementary programming
String and StringBuilder
String.
Final Jim Brucker.
Strings & Arrays CSCI-1302 Lakshmish Ramaswamy.
Java Programming Language
Topics Introduction to File Input and Output
String methods 26-Apr-19.
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
String Class.
String Manipulation.
Review of Java Fundamentals
Strings in Java Strings in Java are also a reference type.
Unit-2 Objects and Classes
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

String

2 Objectives Discuss string handling –System.String class –System.Text.StringBuilder class

3 String class Framework Class Library provides System.String class –represents immutable sequence of characters namespace System { public sealed class String... {... } String class

4 equivalent String alias C# provides string as convenient alias for System.String string s; System.String t;

5 String creation String is a reference type –created with new –constructors available for char and char[] char[] t = new char[5] { 'h', 'e', 'l', 'l', 'o' }; string a = new string(t); string b = new string(t, 0, 2); string c = new string('z', 3); entire array first 2 characters given character repeated 3 times helloa heb zzzc

6 String literals String literals automatically converted into string objects –convenient shorthand for common case string s = "hello"; string object created hellos

7 Escape sequence Can include escape sequence in string –precede special characters with \ string a = "hello\n"; string b = "\""; string c = "C:\\WINDOWS";... linefeed double quote backslash

8 Verbatim string literal Can create verbatim string literal –precede –helps reduce need for escape sequences string path verbatim

9 String length String provides read only Length property string s = "hello"; int l = s.Length; length will be 5

10 String indexer String provides read only indexer –indices start at zero string s = "hello"; char c = s[4]; retrieve 'o'

11 String comparison String provides several ways to perform comparison –all have value semantics string a = "bike"; string b = "bit"; if (a.CompareTo(b) < 0)... if (a.Equals(b))... if (a == b)... if (a != b)... three way comparison, used for ordering equality inequality

12 String concatenation String concatenation supported –operator + –operator += string a = "holly"; string b = "wood"; string c = a + b; operator + string d = "tom"; d += "cat"; operator +=

13 String immutability Strings are immutable –no way to modify contents Operations which seem to modify string do not –actually create new object to represent new value string d = "tom"; d += "cat"; create new object, assign to reference tomd tomcat

14 String inefficiency Strings can be inefficient when used for concatenation –may create and destroy many intermediate objects string text = ""; for (int i = 0; i < 10; i++) { text += Console.ReadLine(); } may create new object each iteration

15 StringBuilder class Framework Class Library provides StringBuilder class –in System.Text namespace –represents mutable sequence of characters namespace System.Text { public sealed class StringBuilder {... } StringBuilder class

16 StringBuilder mutability StringBuilder provides mutator methods –change contents of existing object –can be more efficient than string for concatenation StringBuilder text = new StringBuilder(); text.Append(" hello "); text.Remove(12, 4); text.Insert(12, "/bode"); text[16] = 'y'; modify hello text

17 StringBuilder internally managed capacity StringBuilder adjusts capacity to accommodate contents –no need for user interaction capacity increased as needed StringBuilder text = new StringBuilder(); text.Append(" "); text.Append("hello");... text.Append(" ");

18 StringBuilder user managed capacity StringBuilder allows user some control over capacity –initial capacity during construction –read/write Capacity property Explicit management of capacity can be efficient –if final length of string is known ahead of time StringBuilder a = new StringBuilder(100); a.Capacity = 200; capacity of 100 capacity of 200

19 StringBuilder typical usage StringBuilder typically used to create desired sequence –result is then converted to string using ToString string Create() { StringBuilder text = new StringBuilder(); text.Append(" "); text.Append("hello");... return text.ToString(); } create convert

20 Summary.NET Framework Class Library has two string classes –String –StringBuilder String is primary class –offers most services –is most convenient to use StringBuilder is more specialized –targeted toward creating string from pieces