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.

Slides:



Advertisements
Similar presentations
 2002 Prentice Hall. All rights reserved. 1 Chapter 15 – Strings, Characters and Regular Expressions Outline 15.1Introduction 15.2 Fundamentals of Characters.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
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 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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and 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.
©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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Fundamental Programming Structures in Java: Strings.
28-Jun-15 String and StringBuilder Part I: String.
 2006 Pearson Education, Inc. All rights reserved Strings, Characters and Regular Expressions.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
 Pearson Education, Inc. All rights reserved Strings, Characters and Regular Expressions.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 12 More.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Array String String Builder. Arrays Arrays are collections of several elements of the same type E.g. 100 integers, 20 strings, 125 students, 12 dates,
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Chapter 7: Characters, Strings, and the StringBuilder.
CHAPTER 9 Text Processing and More about Wrapper Classes Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011.
Characters, Strings, And The string Class Chapter 10.
Strings Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering products.
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.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
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:
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.
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
Strings, and the string Class. C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character The C-string.
 2009 Pearson Education, Inc. All rights reserved. 1 Ch.18 Strings, Characters [and Regular Expressions] Many slides modified by Prof. L. Lilien (even.
Lecture 10 Dr. Eng. Ibrahim El-Nahry Strings, Characters and Regular Expressions.
String and StringBuffer classes
The String Class.
Strings, Characters and Regular Expressions
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Strings, Characters and Regular Expressions
Strings and Characters
String String Builder.
Primitive Types Vs. Reference Types, Strings, Enumerations
Standard Version of Starting Out with C++, 4th Edition
Lecture 4 Using Classes Richard Gesick.
String and StringBuilder
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
String and StringBuilder
10.1 Character Testing.
String and StringBuilder
Strings and Characters
String and StringBuilder
Standard Version of Starting Out with C++, 4th Edition
Visual Programming COMP-315
String Manipulation.
Unit-2 Objects and Classes
Presentation transcript:

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 a series of characters. These characters can be uppercase letters, lowercase letters, digits and various special characters. Character constants are established according to the Unicode character set. String is a reference type. The keyword null represents a null, not an empty string (which is a string object that is of length 0 and contains no characters). The string.Empty should be used if you need a string with no characters.

StringConstructor.cs Class string provides 8 constructors. Below is the use of 3 constructors: Assign a string literal to string reference originalString. The string constructor can take a char array and two int arguments for starting position and length. Copy a reference to another string literal. The string constructor can take a character array as an argument. The string constructor can take as arguments a character and an int specifying the number of times to repeat that character in the string.

String Properties Property Length allows you to determine the number of characters in a string. The string indexer treats a string as an array of chars and returns each character at a specific position in the string. As with arrays, the first element of a string is considered to be at position 0. Attempting to access a character that is outside a string’s bounds i.e., an index less than 0 or an index greater than or equal to the string’s length) results in an IndexOutOfRangeException. The string method CopyTo copies a specified number of characters from a string into a char array. StringMethods.cs

Comparing Strings Method Equals tests any two objects for equality (i.e., checks whether the objects contain identical contents). The string class’s Equals method uses a lexicographical comparison—comparing the integer Unicode values of character in each string. The overloaded string equality operator == also uses a lexicographical comparison to compare two strings. String comparisons are case-sensitive. Method CompareTo returns: 0 if the strings are equal A negative value if the calling string is less than the argument string A positive value if the calling string is greater than the argument. Example: StringCompare.cs

Searching Strings StartWith and EndWith string s = “started”; if ( s.StartsWith(“st”) ) … if ( s.EndsWith(“ed”) ) … IndexOf locates the first occurrence of a character or substring in a string and returns its index, or -1 if it is not found. LastIndexOf like IndexOf, but searches from the end of the string. IndexOfAny and LastIndexOfAny take an array of characters as the first argument and return the index of the first occurrence of any of the characters in the array.

Substring and Concat Substring methods which create a new string by copying part of an existing string. s.Substr(20); s.Substr(0, 6); Like the + operator, the static method Concat of class string concatenates two strings and returns a new string. string strNew = String.Concat(str1, str2); String strNew = str1 + str2; string2 = string1.Replace(‘e’, ‘E’); string2 = string1.ToUpper() and string2 = string1.ToLower() string2 = string1.Trim(); Examples: StringMethods2.cs

StringBuilder Objects of class string are immutable. Class StringBuilder is used to create and manipulate dynamic string information—i.e., mutable strings. StringBuilder is much more efficient for working with large numbers of strings than creating individual immutable strings.

StringBuilder constructors The no-parameter StringBuilder constructor creates an empty StringBuilder with a default capacity of 16 characters. Given a single int argument, the StringBuilder constructor creates an empty StringBuilder that has the initial capacity specified in the int argument. Given a single string argument, the StringBuilder constructor creates a StringBuilder containing the characters of the string argument. Its initial capacity is the smallest power of two greater than or equal to the number of characters in the argument string, with a minimum of 16.

StringBuilder features Class StringBuilder provides the Length and Capacity properties. Method EnsureCapacity doubles the StringBuilder instance’s current capacity. If this doubled value is greater than the value that the programmer wishes to ensure, that value becomes the new capacity. Otherwise, EnsureCapacity alters the capacity to make it equal to the requested number. When a StringBuilder exceeds its capacity, it grows in the same manner as if method EnsureCapacity had been called. If Length is set to a value less than the number of characters in the StringBuilder, the contents of the StringBuilder are truncated.

Append and AppendFormat Class StringBuilder provides 19 overloaded Append methods that allow various types of values to be added to the end of a StringBuilder. The Framework Class Library provides versions for each of the simple types and for character arrays, strings and objects. Examples: StringBuilderAppends.cs AppendFormat converts a string to a specified format, then appends it to the StringBuilder. Examples: StringBuilderAppendFormat.cs

Append and AppendFormat Formats have the form {X[,Y][:FormatString]}. X is the number of the argument to be formatted, counting from zero. Y is an optional argument, which can be positive or negative, indicating how many characters should be in the result. A positive integer aligns the string to the right; a negative integer aligns it to the left. The optional FormatString applies a particular format to the argument—currency, decimal or scientific, among others. One version of AppendFormat takes a string specifying the format and an array of objects to serve as the arguments to the format string.

Other Methods of StringBuilder Insert inserts its second argument into the StringBuilder in front of the character in the position specified by the first argument. Remove takes two arguments—the index at which to begin deletion and the number of characters to delete. Replace searches for a specified string or character and substitutes another string or character in its place.

char Methods char is an alias for the struct Char. Char method IsDigit determines whether a character is defined as a digit. IsLetter determines whether a character is a letter. IsLetterOrDigit determines whether a character is a letter or a digit. IsLower determines whether a character is a lowercase letter. IsUpper determines whether a character is an uppercase letter. ToUpper returns a character’s uppercase equivalent, or the original argument if there is no uppercase equivalent.

char Methods ToLower returns a character lowercase equivalent, or the original argument if there is no lowercase equivalent. IsPunctuation determines whether a character is a punctuation mark, such as "!", ":" or ")". IsSymbol determines whether a character is a symbol, such as "+", "=" or "^". Static method IsWhiteSpace. Public instance methods: ToString, Equals, and CompareTo.