This document discusses strings and regular expressions in .NET. It provides details on the System.String class and how strings are immutable. The StringBuilder class is introduced as a mutable alternative for string manipulation. Various string formatting options like standard numeric and custom format strings are covered. Finally, an overview of regular expressions is given, explaining how they provide a powerful way to parse and process text using pattern matching.
2. System.String
A string is a sequential collection of Unicode characters
that is used to represent text.
A String object is a sequential collection of
System.Char objects that represent a string.
The value of the String object is the content of the
sequential collection, and that value is immutable.
A String object is called immutable (read-only) because
its value cannot be modified once it has been created.
3. Some Methods in System.String
Name Description
Clone Returns a reference to this instance of String.
Compares two specified String objects and returns
Compare(String,
an integer that indicates their relative position in the
String)
sort order.
Creates the string representation of a specified
Concat(Object)
object.
Creates a new instance of String with the same
Copy
value as a specified String.
Returns a value indicating whether the specified
Contains
String object occurs within this string.
Determines whether this instance and a specified
Equals(Object) object, which must also be a String object, have the
same value.
Replaces the format item in a specified string with
Format(String,
4. Building Strings
String is an extremely powerful class that implements a
large number of very useful methods.
However, the String class has a short coming that
makes it very inefficient for making repeated
modifications to given string.
For example, when you write fallowing code, the
compiler actually creates a new string object to hold the
new sequence of characters, and that new object is
assigned to b. The string "h" is then eligible for garbage
collection.
Example:
string b = "h";
b += "ello";
5. StringBuilder Class
Represents a mutable string of characters. This class
cannot be inherited.
Namespace is System.Test
This class represents a string-like object whose value is
a mutable sequence of characters.
The value is said to be mutable because it can be
modified once it has been created by appending,
removing, replacing, or inserting characters.
For Example:
StringBuilder text=new StringBuilder (“….String …”,size);//size is an
integer
Text.AppendForamt(“New String….”);
This code sets an initial capacity of size for StringBuilder
6. StringBuilder Members
Name Description
Append() Appends a string to the current string.
AppendFormat() Appends a string that has been worked out from a format
specifier
Insert() Inserts a sub string into the current string
Remove() Removes characters from the current string.
Replace() Replaces all occurrences of a character with another
character or a substring with another substring in the
current string.
ToString() Returns the current string cast to a System.String object.
7. Format Strings
Standard numeric format strings are used to format
common numeric types.
A standard numeric format string takes the form Axx,
where A is an alphabetic character called the format
specifier, and xx is an optional integer called the
precision specifier.
The precision specifier ranges from 0 to 99 and affects
the number of digits in the result.
Any numeric format string that contains more than one
alphabetic character, including white space, is
interpreted as a custom numeric format string.
8. Format Strings
SPECIFIE APPLIES TO MEANING EXAMPLE
R
C Numeric types Locale-specific $4834.50(US)
monetary value £4834.50(UK)
D Integer types General integer 4834
only
E Numeric types Scientific notation 4.384E+003
F Numeric types Fixed decimal 4834.50
G Numeric types General number 4834.5
N Numeric types Common locale- 4,384.50(UK/USA)
specific format for 4 384,50(continental
numbers Europe
p Numeric types Percentage notation 432,000.00%
X Integer types Hexadecimal format 1120(if you want to
only display 0x1120, you will
10. Regular Expressions
Regular expressions provide a powerful, flexible, and
efficient method for processing text.
The extensive pattern-matching notation of regular
expressions enables you to quickly parse large amounts
of text to find specific character patterns; to validate text
to ensure that it matches a predefined pattern (such as
an e-mail address); to extract, edit, replace, or delete text
substrings; and to add the extracted strings to a
collection in order to generate a report.
For many applications that deal with strings or that parse
large blocks of text, regular expressions are an
indispensable tool.
11. Regular Expressions
Regular expressions are part of those small technology
areas that are incredibly useful in a wide range of
programs, yet rarely used among developers.
You can think regular expressions as a mini-
programming language with one specific scope: to locate
substrings within a large string expression.
12. How Regular Expressions Work
The centerpiece of text processing with regular
expressions is the regular expression engine, which is
represented by the
System.Text.RegularExpressions.Regex object in the
.NET Framework.
At a minimum, processing text using regular expressions
requires that the regular expression engine be provided
with the following two items of information:
The regular expression pattern to identify in the text.
The text to parse for the regular expression pattern.