C# chap 10


Published on

C#, programs , learn Notes

Published in: Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

C# chap 10

  1. 1. Chapter 10 Immutable Strings string objects are immutable, i.e. we cannot modify the characters contained in them. The string is an alias for the predefined System.String class in the Common Language Runtime (CLR). The build-in operations produce a modified version of the string rather than modifying the string on which the method is called. Compare() – Compares the strings CompareTo() – Compares the current instance with another instance ConCat() – Concatenates two or more strings Copy() – Creates a new string by copying another CopyTo() – Copies a specified number of chars to an array of Unicode chars Equals() – Determines if two strings are equal Join() – Joins an array of strings together PadLeft() – Left-aligns the strings in a field PadRight() – Right-aligns the strings in a field Remove () – Deletes characters from the string Substring() – Extracts a suvbstring ToLower() – Returns a lower-case version of string ToUpper() - Returns a upper-case version of string Trim() – Removes white space from the string TrimEnd() – Removes a string of characters from the end of the string TrimStart() – Removes a string of characters from beginning of the string Mutable Strings Mutalbe strings that are modifiable can be created using StringBuilder class Eg. StringBuilder str1 = new StringBuilder(“abc”); They grow dynamically as more characters are added to them. Mutable strings are also known as dynamic strings. StringBuilder Methods are : - Append() – Appends a string, AppendFormat() – Appends strings using a specific format EnsureCapacity() – Ensures sufficient size, Insert() – Inserts a string at a specified position, Remove() Removes the specified characters Replace () – Replaces all instances of a character with the specified one
  2. 2. StringBuilder Properties :- Capacity – To retrieve or set the number of characters the object can hold, Length – To retrieve or set the length MaxCapacity – To retrieve the maximum capacity of the object, [ ] – To get or set a character at a specified position Regular Expressions - Regular expressions provide a powerful tool for searching and manipulating a large text. - A regular expression may be applied to a text to accomplish tasks such as a) to locate the substrings and locate them b) to modify one or more substrings and return c) to identify substrings that begin with or end with a pattern of characters d) to find all words that begin with a group of characters and end with some other characters. e) To find all the occurrences of a substring patterns - A regular expression also known as a pattern string is a string containing two types of characters Literals, Metacharacters, Literals are characters that search and match in the text. Metacharacters are special characters that give commands to the regular expression parser. Eg of regular Expressions are “bm” – Any word beginning with m, “erb” – Any word ending with er “BxB” – Any x in middle of word. “bms*erb” – Any word beginning with m and ending with er “|,” – Any word separated by a space or comma Here, b, B, s* are metacharacters and m, er, x, comma are literals. The namespace System.Text.RegularExpressions support a number of classes that can be used for searching, matching and modifying a text document. The important classes are Regex, MatchCollection, Match Distinguish between String and StringBuilder class String Class – The string class lies in the namespace System StringBuilder Class – the StringBuilder class lies in the namespace System.Text Immutable strings i.e. a string whose character cannot be modified can be created using the string class Mutable strings can be created using the StringBuilder class. All operations produce a modified version of string rather than modifying the string on which the method is called. All operation modify the original string on which the method is called. Hence these type of strings are also known as dynamic strings.