Strings and regular expressions

Содержание

Слайд 2

What can we use ?

2. Formatting expressions — using a couple of

What can we use ? 2. Formatting expressions — using a couple
useful interfaces, IFormatProvider and IFormattable, and by implementing these interfaces on our own classes, we can actually define our own formatting sequences, so we can display the values of our classes in whatever way we specify.

There are two types of string: String and StringBuilder

Building strings — If you’re performing repeated modifications on a string befor displaying it or passing it to some method, the String class can be very inefficient. For this kind of situation, another class, System.Text.StringBuilder is more suitable, since it has been designed exactly for this situation.

3. Regular expressions — .NET offers some classes that deal with the situation in which you need to identify or extract substrings that satisfy certain fairly sophisticated criteri. We can use some classes from System.Text.RegularExpressions, which are designed specifically to perform this kind of processing.

Слайд 3

System.String

System.String is a class that is specifically designed to store a

System.String System.String is a class that is specifically designed to store a
string, and allow a large number of operations on the string.

Each string object is an immutable sequence of Unicode characters.
IT means that methods that appear to change the string actually return a modified copy; the original string remains intact in memory until it is garbage-collected.

1. An object of type System.String is created and initialized to hold the text. The .NET runtime allocates just enough memory to hold this text (32 chars)

2. We create a new string instance, with just enough memory allocated to store the combined text (55 chars). The old String object is now unreferensed.

Слайд 4

Declaration of string
// Simple declaration
string MyString = "Hello
World";// Strings can include esca
pe

Declaration of string // Simple declaration string MyString = "Hello World";// Strings
characte
r
s, such as \n or \t, which //
begin with a backslash charact
er (\)strin
g

Слайд 5

Manipulating Strings

The string class provides a host of methods for comparing, searching,

Manipulating Strings The string class provides a host of methods for comparing,
and manipulating strings, the most important of whic

Слайд 6

Manipulating Strings

int result;//c
o
mpare two strings, case sen
sitiv
eresult = s
tring
.
C
ompare(s1, s2);// compar
e
two

Manipulating Strings int result;//c o mpare two strings, case sen sitiv eresult
strings, ignore caseres
ult =
string.Compare(s1, s2, true);// insert the word "e
xcellent"st
ring
s10 = s
3

Слайд 7

Splitting Strings

// create some
strings to work withstring s1 = "On
e,Two
,Three Libe
rty A
s
s
ociates,

Splitting Strings // create some strings to work withstring s1 = "On
Inc.";// consta
n
ts for the space and comma character
scons
t char Space = ' ';const char Comma = ',';//
array of de
limiters to split the sentence withchar[] delimiter
s = new char[] { Space
, Com
ma };//

The result is array of strings

Слайд 8

string object is an immutable

Do you know when we do like that

string object is an immutable Do you know when we do like
we are assigning it again and again?

Here we are defining a string called returnNumber and after that, in the loop we are concatenating the old one with the new.

It's really like assigning 999 new strings !!!

The problem is that the string type is not designed for this kind of operation. What
you want is to create a new string by appending a formatted string each time through the loop. The class you need is StringBuilder.

Слайд 9

Dynamic Strings (class StringBuilder)

The System.Text.StringBuilder class is used for creating and modifying

Dynamic Strings (class StringBuilder) The System.Text.StringBuilder class is used for creating and
strings.

Unlike String, StringBuilder is mutable.

The processing you can do on a StringBuilder is limited to substitutions and appending or removing text from strings. However, it works in a much more efficient way.

When you construct a string, just enough memory gets allocated to hold the string. The StringBuilder, however, normally allocates more memory than needed

Слайд 10

StringBuilder

Then, on calling the Append() method, the remaining text is placed in

StringBuilder Then, on calling the Append() method, the remaining text is placed
the empty space, without the need for more memory allocation.

Capacity

Normally, we have to use StringBuilder to perform any manipulation of strings, and String to store or display the final result.

Слайд 11

StringBuilder members

The StringBuilder class has two main properties:
Length - indicates the length

StringBuilder members The StringBuilder class has two main properties: Length - indicates
of the string that it actually contains;
Capacity - indicates the maximum length of the string in the memory allocation.

Str
ingBuilder sb = new StringBuilder(“Hello”); //Len
gth = 5StringBuilder sb = new StringBuilder(20
); //Capacity = 20//we can set Capa

Слайд 12

StringBuilder members

The following table lists the main StringBuilder methods.

StringBuilder members The following table lists the main StringBuilder methods.

Слайд 13

Regular Expressions

Regular expressions are Patterns that can be used to match strings.
Regular expressions

Regular Expressions Regular expressions are Patterns that can be used to match
are a powerful language for describing and manipulating text.
Regular expression is applied to a string—that is, to a set of characters. Often, that string is an text document.

With regular expressions, you can perform high-level operations on strings. For example, you can:

Identify all repeated words in a string (for example, “The
computer books books” to “The computer books”)

Convert all words to title case (for example, “this is a Title” to “This Is A Title”)

Convert all words to title case (for example, “this is a Title” to “This Is A Title”)

Separate the various elements of a URI (for example, given http://www.wrox.com, extract the protocol, computer name, file name, and so on)

Слайд 14

Regular Expressions
using System.Text;using
S
ystem.T
ext.RegularExpressions;string text = Consol
e.Rea
dLine();str
ing r
e
Task: write a C# console

Regular Expressions using System.Text;using S ystem.T ext.RegularExpressions;string text = Consol e.Rea dLine();str
application that takes some text as an input, and determines if the text is an email address.

As you can see, a regular expression consists of two types of characters: literals and metacharacters.
A literal is a character you wish to match in the target string.
A metacharacter is a special symbol that acts as a command to the regular expression parser. The parser is the engine responsible for understanding the regular expression.

Слайд 15

Text: Anna Jones and
a
friend
went to lunch Regex: went Matches:

Text: Anna Jones and a friend went to lunch Regex: went Matches:
Anna
Jones
and a frie
nd we
n
// to match the 'a' char
a
cter fo
llowed by any two characters. Text: abc
def a
nt cow Rege
x:
a
// matches 'a' followed
b
y two w
ord characters. Text: abc anaconda ant c
ow ap
ple Regex:
a\w
\
// matches any three dig
i
ts in a
row Text: 123 12 843 8472 Regex: \d\d
\d M
atches: 123
12 8
4
//matches any three char
where t
he first character is 'd‘ or 'a' Text: a
bc de
f ant cow R
egex:

Слайд 16

//matches any three char
a
cters w
here the second character is 'a', 'b', 'c'

//matches any three char a cters w here the second character is

or 'd
'. Text:
abc
p
//matches any of the cha
r
acters
from 'a' to 'z' or any digit from //'0' to
'9' f
ollowed by
two w
o
// matches the character
'a' fol
lowed by zero or more word// characters. Te
xt:
Anna Jones
and
a

Слайд 17

Metacharacters and their Description

Metacharacters and their Description

Слайд 18

Examples
using System;using Sy
s
tem.Text.RegularE
xpressions;// First we
see the input string.
string input = "/con
t

Examples using System;using Sy s tem.Text.RegularE xpressions;// First we see the input
Имя файла: Strings-and-regular-expressions-.pptx
Количество просмотров: 180
Количество скачиваний: 0