Anda di halaman 1dari 9

Strings & Regular Expressions

String Class

Strings

System.String is a class specifically designed to store a string and


allow a large number of operations on the string.
Method

Purpose

Compare

Compares two strings

Concat

Combine Separate Instances into a single instance

CopyTo

Copies a specific number of characters from the


selected index to a entirely new instance of array

IndexOf

Locates the first occurrence of a given substring or


character in the string

Insert

Inserts a string instance into another string instance


at a specified index

Join

Builds a new string by combining an array of strings

LastIndexOf Same as IndexOf but finds the last occurrence

String Class (Cond.)


Method

Purpose

Replace

Replaces occurrences of a given character or substring


in the string with another character or substring

Split

Splits the string into an array of substrings, the breaks


occurring wherever a given character occurs

SubString

Retrieves the substring starting at a specified position in


the string

ToUpper

Converts string to uppercase

ToLower

Converts string to lowercase

Trim

Removes leading and trailing whitespace

Building Strings
As you have seen, String is an extremely powerful class that
implements a large number of very useful methods.
it is actually an immutable data type, which means that once you
initialize a string object, that string object can never change.
string greetingText = "Hello from all the guys at Wrox Press. ";
greetingText += "We do hope you enjoy this book as much as we
enjoyed writing it.";

StringBuilder
StringBuilder, which takes an initial string and capacity as its
parameters. StringBuilder sb = new StringBuilder("Hello"); ( OR )
StringBuilder sb = new StringBuilder(20);
Apart from the Length and Capacity properties, there is a readonly MaxCapacity property that indicates the limit to which a
given StringBuilder instance is allowed to grow.

StringBuilder (Contd.)
StringBuilder sb = new StringBuilder("Hello");
sb.Capacity = 100;
The following table lists the main StringBuilder methods.
Method

Purpose

Append

Appends a string to the current string

Insert

Inserts a substring into the current string

Remove

Removes characters from the current string

Replace

Replaces all occurrences of a character by another


character or a substring with another substring in
the current string

ToString

Returns the current string cast to a System.String


object

Regular Expressions
Regular Expressions are used to match the string patterns
we should use the namespace System.Text.RegularExpressions
The regular expressions language is designed specifically for string
processing. It contains two features:
o A set of escape codes for identifying specific types of characters.
o A system for grouping parts of substrings and intermediate
results during a search operation.
Following table contains the regular expression characters

Symbol

Meaning

Matches the beginning of the string

Matches the end of the string

Matches any single character except new line character


(\n)

Symbol

Meaning

Matches zero or more occurrence of preceding characters

Matches one or more occurrence of preceding character

Matches zero or one occurrence of preceding character

\s

Matches Any Whitesapce character

\S

Matches Any Non Whitesapce character

\b

Word Boundary

\B

Any position that is not a Word Boundary

[]

Matches any one character

Indicates Range

RegEx

Mathes() Method
MatchCollection
Match
()

Metho
d

Match
GroupCollection
Group
CaptureCollection
Capture

THANK YOU

Anda mungkin juga menyukai