SlideShare a Scribd company logo
1 of 73
Strings andText
Processing
Processing and ManipulatingText Information
Svetlin Nakov
Telerik Software Academy
academy.telerik.com/
ManagerTechnicalTrainer
www.nakov.com/
Table of Contents
1. What is String?
2. Creating and Using Strings
 Declaring, Creating, Reading and Printing
3. Manipulating Strings
 Comparing, Concatenating, Searching,
Extracting Substrings, Splitting
4. Other String Operations
 Replacing Substrings, Deleting Substrings,
Changing Character Casing,Trimming
2
Table of Contents (2)
5. Building and Modifying Strings
 Why the + Operator is Slow?
 Using the StringBuilder Class
6. Formatting Strings
 Formatting Numbers, Dates
and Currency
7. Cultures and Culture-Sensitive Formatting
 Accessing and Assigning the Current Culture
8. Parsing Numbers and Dates
3
What Is String?
What Is String?
 Strings are sequences of characters
 Each character is a Unicode symbol
 Represented by the string data type in C#
(System.String)
 Example:
string s = "Hello, C#";
H e l l o , C #s
5
The System.String Class
 Strings are represented by System.String
objects in .NET Framework
 String objects contain an immutable (read-only)
sequence of characters
 Strings use Unicode to support multiple
languages and alphabets
 Strings are stored in the dynamic memory
(managed heap)
 System.String is reference type
6
The System.String Class (2)
 String objects are like arrays of characters
(char[])
 Have fixed length (String.Length)
 Elements can be accessed directly by index
 The index is in the range [0...Length-1]
string s = "Hello!";
int len = s.Length; // len = 6
char ch = s[1]; // ch = 'e'
0 1 2 3 4 5
H e l l o !
index =
s[index] =
7
Strings – First Example
static void Main()
{
string s =
"Stand up, stand up, Balkan Superman.";
Console.WriteLine("s = "{0}"", s);
Console.WriteLine("s.Length = {0}", s.Length);
for (int i = 0; i < s.Length; i++)
{
Console.WriteLine("s[{0}] = {1}", i, s[i]);
}
}
8
Strings – First Example
Live Demo
Creating and Using Strings
Declaring, Creating, Reading and Printing
Declaring Strings
string str1;
System.String str2;
String str3;
 Several ways of declaring string variables:
 Using the C# keyword string
 Using the .NET's fully qualified class name
System.String
 The above three declarations are equivalent
11
Creating Strings
 Before initializing a string variable has null
value
 Strings can be initialized by:
 Assigning a string literal to the string variable
 Assigning the value of another string variable
 Assigning the result of operation of type string
12
Creating Strings (2)
 Not initialized variables has value of null
 Assigning a string literal
 Assigning from another string variable
 Assigning from the result of string operation
string s; // s is equal to null
string s = "I am a string literal!";
string s2 = s;
string s = 42.ToString();
13
Reading and Printing Strings
 Reading strings from the console
 Use the method Console.ReadLine()
string s = Console.ReadLine();
Console.Write("Please enter your name: ");
string name = Console.ReadLine();
Console.Write("Hello, {0}! ", name);
Console.WriteLine("Welcome to our party!");
 Printing strings to the console
 Use the methods Write() and WriteLine()
14
Reading and Printing Strings
Live Demo
Comparing, Concatenating, Searching,
Extracting Substrings, Splitting
Manipulating Strings
Comparing Strings
 Several ways to compare two strings:
 Dictionary-based string comparison
 Case-insensitive
 Case-sensitive
int result = string.Compare(str1, str2, true);
// result == 0 if str1 equals str2
// result < 0 if str1 is before str2
// result > 0 if str1 is after str2
string.Compare(str1, str2, false);
17
Comparing Strings (2)
 Equality checking by operator ==
 Performs case-sensitive compare
 Using the case-sensitive Equals() method
 The same effect like the operator ==
if (str1 == str2)
{
…
}
if (str1.Equals(str2))
{
…
}
18
Comparing Strings – Example
 Finding the first string in a lexicographical order
from a given list of strings:
string[] towns = {"Sofia", "Varna", "Plovdiv",
"Pleven", "Bourgas", "Rousse", "Yambol"};
string firstTown = towns[0];
for (int i=1; i<towns.Length; i++)
{
string currentTown = towns[i];
if (String.Compare(currentTown, firstTown) < 0)
{
firstTown = currentTown;
}
}
Console.WriteLine("First town: {0}", firstTown);
19
Live Demo
Comparing Strings
Concatenating Strings
 There are two ways to combine strings:
 Using the Concat() method
 Using the + or the += operators
 Any object can be appended to a string
string str = String.Concat(str1, str2);
string str = str1 + str2 + str3;
string str += str1;
string name = "Peter";
int age = 22;
string s = name + " " + age; //  "Peter 22"
21
Concatenating Strings – Example
string firstName = "Svetlin";
string lastName = "Nakov";
string fullName = firstName + " " + lastName;
Console.WriteLine(fullName);
// Svetlin Nakov
int age = 25;
string nameAndAge =
"Name: " + fullName +
"nAge: " + age;
Console.WriteLine(nameAndAge);
// Name: Svetlin Nakov
// Age: 25
22
Live Demo
Concatenating Strings
Searching in Strings
 Finding a character or substring within given
string
 First occurrence
 First occurrence starting at given position
 Last occurrence
IndexOf(string str)
IndexOf(string str, int startIndex)
LastIndexOf(string)
24
Searching in Strings – Example
string str = "C# Programming Course";
int index = str.IndexOf("C#"); // index = 0
index = str.IndexOf("Course"); // index = 15
index = str.IndexOf("COURSE"); // index = -1
// IndexOf is case-sensetive. -1 means not found
index = str.IndexOf("ram"); // index = 7
index = str.IndexOf("r"); // index = 4
index = str.IndexOf("r", 5); // index = 7
index = str.IndexOf("r", 8); // index = 18
0 1 2 3 4 5 6 7 8 9 10 11 12 13 …
C # P r o g r a m m i n g …
index =
s[index] =
25
Live Demo
Searching
in Strings
Extracting Substrings
 Extracting substrings
 str.Substring(int startIndex, int length)
 str.Substring(int startIndex)
string filename = @"C:PicsRila2009.jpg";
string name = filename.Substring(8, 8);
// name is Rila2009
string filename = @"C:PicsSummer2009.jpg";
string nameAndExtension = filename.Substring(8);
// nameAndExtension is Summer2009.jpg
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
C :  P i c s  R i l a 2 0 0 5 . j p g
27
Live Demo
Extracting Substrings
Splitting Strings
 To split a string by given separator(s) use the
following method:
 Example:
string[] Split(params char[])
string listOfBeers =
"Amstel, Zagorka, Tuborg, Becks.";
string[] beers =
listOfBeers.Split(' ', ',', '.');
Console.WriteLine("Available beers are:");
foreach (string beer in beers)
{
Console.WriteLine(beer);
}
29
Live Demo
Splitting Strings
Other String Operations
Replacing Substrings, Deleting Substrings,
Changing Character Casing,Trimming
Replacing and Deleting Substrings
 Replace(string, string) – replaces all
occurrences of given string with another
 The result is new string (strings are immutable)
 Remove(index, length) – deletes part of a string
and produces new string as result
string cocktail = "Vodka + Martini + Cherry";
string replaced = cocktail.Replace("+", "and");
// Vodka and Martini and Cherry
string price = "$ 1234567";
string lowPrice = price.Remove(2, 3);
// $ 4567
32
Changing Character Casing
 Using method ToLower()
 Using method ToUpper()
string alpha = "aBcDeFg";
string lowerAlpha = alpha.ToLower(); // abcdefg
Console.WriteLine(lowerAlpha);
string alpha = "aBcDeFg";
string upperAlpha = alpha.ToUpper(); // ABCDEFG
Console.WriteLine(upperAlpha);
33
Trimming White Space
 Using Trim()
 Using Trim(chars)
 Using TrimStart() and TrimEnd()
string s = " example of white space ";
string clean = s.Trim();
Console.WriteLine(clean);
string s = " tnHello!!! n";
string clean = s.Trim(' ', ',' ,'!', 'n','t');
Console.WriteLine(clean); // Hello
string s = " C# ";
string clean = s.TrimStart(); // clean = "C# "
34
Other String Operations
Live Demo
Building and Modifying Strings
Using the StringBuilder Class
Constructing Strings
 Strings are immutable!
 Concat(), Replace(), Trim(), ... return new
string, do not modify the old one
 Do not use "+" for strings in a loop!
 It runs very, very inefficiently!
public static string DupChar(char ch, int count)
{
string result = "";
for (int i=0; i<count; i++)
result += ch;
return result;
}
Very bad practice.
Avoid this!
37
Slow Building Strings with +
Live Demo
StringBuilder: How It Works?
 StringBuilder keeps a buffer memory,
allocated in advance
 Most operations use the buffer memory and
do not allocate new objects
H e l l o , C # !StringBuilder:
Length=9
Capacity=15
Capacity
used buffer
(Length)
unused
buffer
39
How the + Operator Performs
String Concatenations?
 Consider the following string concatenation:
 It is equivalent to this code:
 Several new objects are created and left to
the garbage collector for deallocation
 What happens when using + in a loop?
string result = str1 + str2;
StringBuilder sb = new StringBuilder();
sb.Append(str1);
sb.Append(str2);
string result = sb.ToString();
40
The StringBuilder Class
 StringBuilder(int capacity) constructor
allocates in advance buffer of given size
 By default 16 characters are allocated
 Capacity holds the currently allocated space (in
characters)
 this[int index] (indexer in C#) gives access to
the char value at given position
 Length holds the length of the string in the
buffer
41
The StringBuilder Class (2)
 Append(…) appends a string or another object
after the last character in the buffer
 Remove(int startIndex, int length) removes
the characters in given range
 Insert(int index, string str) inserts given
string (or object) at given position
 Replace(string oldStr, string newStr)
replaces all occurrences of a substring
 ToString() converts the StringBuilder to
String
42
Changing the Contents of a
String with StringBuilder
 Use the System.Text.StringBuilder class for
modifiable strings of characters:
 Use StringBuilder if you need to keep adding
characters to a string
public static string ReverseString(string s)
{
StringBuilder sb = new StringBuilder();
for (int i = s.Length-1; i >= 0; i--)
sb.Append(s[i]);
return sb.ToString();
}
43
StringBuilder –
Another Example
 Extracting all capital letters from a string
public static string ExtractCapitals(string s)
{
StringBuilder result = new StringBuilder();
for (int i = 0; i<s.Length; i++)
{
if (Char.IsUpper(s[i]))
{
result.Append(s[i]);
}
}
return result.ToString();
}
44
Using StringBuilder
Live Demo
Formatting Strings
Using ToString() and String.Format()
Method ToString()
 All classes in C# have public virtual method
ToString()
 Returns a human-readable, culture-sensitive
string representing the object
 Most .NET Framework types have own
implementation of ToString()
 int, float, bool, DateTime
int number = 5;
string s = "The number is " + number.ToString();
Console.WriteLine(s); // The number is 5
47
Method ToString(format)
 We can apply specific formatting when
converting objects to string
 ToString(formatString) method
int number = 42;
string s = number.ToString("D5"); // 00042
s = number.ToString("X"); // 2A
// Consider the default culture is Bulgarian
s = number.ToString("C"); // 42,00 лв
double d = 0.375;
s = d.ToString("P2"); // 37,50 %
48
Formatting Strings
 The formatting strings are different for the
different types
 Some formatting strings for numbers:
 D – number (for integer types)
 C – currency (according to current culture)
 E – number in exponential notation
 P – percentage
 X – hexadecimal number
 F – fixed point (for real numbers)
49
Method String.Format()
 Applies templates for formatting strings
 Placeholders are used for dynamic text
 Like Console.WriteLine(…)
string template = "If I were {0}, I would {1}.";
string sentence1 = String.Format(
template, "developer", "know C#");
Console.WriteLine(sentence1);
// If I were developer, I would know C#.
string sentence2 = String.Format(
template, "elephant", "weigh 4500 kg");
Console.WriteLine(sentence2);
// If I were elephant, I would weigh 4500 kg.
50
Composite Formatting
 The placeholders in the composite formatting
strings are specified as follows:
 Examples:
{index[,alignment][:formatString]}
double d = 0.375;
s = String.Format("{0,10:F5}", d);
// s = " 0,37500"
int number = 42;
Console.WriteLine("Dec {0:D} = Hex {1:X}",
number, number);
// Dec 42 = Hex 2A
51
Formatting Dates
 Dates have their own formatting strings
 d, dd – day (with/without leading zero)
 M, MM – month
 yy, yyyy – year (2 or 4 digits)
 h, HH, m, mm, s, ss – hour, minute, second
DateTime now = DateTime.Now;
Console.WriteLine(
"Now is {0:d.MM.yyyy HH:mm:ss}", now);
// Now is 31.11.2009 11:30:32
52
Cultures
 Cultures in .NET specify formatting / parsing
settings specific to country / region / language
 Printing the current culture:
 Changing the current culture:
 Culture-sensitive ToString():
53
Console.WriteLine(System.Threading.
Thread.CurrentThread.CurrentCulture);
System.Threading.Thread.CurrentThread.CurrentCulture =
new CultureInfo("en-CA");
CultureInfo culture = new CultureInfo("fr-CA");
string s = number.ToString("C", culture); // 42,00 $
Parsing Numbers and Dates
 Parsing numbers and dates is culture-sensitive
 Parsing a real number using "." as separator:
 Parsing a date in specific format:
54
string str = "3.14";
Thread.CurrentThread.CurrentCulture =
CultureInfo.InvariantCulture;
float f = float.Parse(str); // f = 3.14
string dateStr = "25.07.2011";
DateTime date = DateTime.ParseExact(dateStr,
"dd.MM.yyyy", CultureInfo.InvariantCulture);
Formatting Strings
Live Demo
Summary
 Strings are immutable sequences of characters
(instances of System.String)
 Declared by the keyword string in C#
 Can be initialized by string literals
 Most important string processing members are:
 Length, this[], Compare(str1, str2),
IndexOf(str), LastIndexOf(str),
Substring(startIndex, length),
Replace(oldStr, newStr),
Remove(startIndex, length), ToLower(),
ToUpper(), Trim()
56
Summary (2)
 Objects can be converted to strings and can be
formatted in different styles (using
ToString() method)
 Strings can be constructed by using
placeholders and formatting strings
(String.Format(…))
57
форум програмиране,форум уеб дизайн
курсове и уроци по програмиране,уеб дизайн – безплатно
програмиранеза деца – безплатни курсове и уроци
безплатен SEO курс -оптимизация за търсачки
уроци по уеб дизайн, HTML,CSS, JavaScript,Photoshop
уроци по програмиранеи уеб дизайн за ученици
ASP.NET MVCкурс – HTML,SQL,C#,.NET,ASP.NETMVC
безплатен курс"Разработка на софтуер в cloud среда"
BG Coder -онлайн състезателна система -online judge
курсове и уроци по програмиране,книги – безплатно отНаков
безплатен курс"Качествен програменкод"
алго академия – състезателно програмиране,състезания
ASP.NET курс -уеб програмиране,бази данни, C#,.NET,ASP.NET
курсове и уроци по програмиране– Телерик академия
курсмобилни приложения с iPhone, Android,WP7,PhoneGap
freeC#book, безплатна книга C#,книга Java,книга C#
Дончо Минков -сайт за програмиране
Николай Костов -блог за програмиране
C#курс,програмиране,безплатно
Strings andText Processing
http://academy.telerik.com
Exercises
1. Describe the strings in C#. What is typical for the
string data type? Describe the most important
methods of the String class.
2. Write a program that reads a string, reverses it and
prints the result at the console.
Example: "sample"  "elpmas".
3. Write a program to check if in a given expression the
brackets are put correctly.
Example of correct expression: ((a+b)/5-d).
Example of incorrect expression: )(a+b)).
59
Exercises (2)
4. Write a program that finds how many times a
substring is contained in a given text (perform case
insensitive search).
Example:The target substring is "in".The text is as
follows:
The result is: 9.
We are living in an yellow submarine. We don't
have anything else. Inside the submarine is very
tight. So we are drinking all the day. We will
move out of it in 5 days.
60
Exercises (3)
5. You are given a text. Write a program that changes
the text in all regions surrounded by the tags
<upcase> and </upcase> to uppercase.The tags
cannot be nested. Example:
The expected result:
We are living in a <upcase>yellow
submarine</upcase>. We don't have
<upcase>anything</upcase> else.
We are living in a YELLOW SUBMARINE. We don't have
ANYTHING else.
61
Exercises (4)
6. Write a program that reads from the console a string
of maximum 20 characters. If the length of the string
is less than 20, the rest of the characters should be
filled with '*'. Print the result string into the console.
7. Write a program that encodes and decodes a string
using given encryption key (cipher).The key consists
of a sequence of characters.The encoding/decoding
is done by performing XOR (exclusive or) operation
over the first letter of the string with the first of the
key, the second – with the second, etc. When the
last key character is reached, the next is the first.
62
Exercises (5)
8. Write a program that extracts from a given text all
sentences containing given word.
Example:The word is "in".The text is:
The expected result is:
Consider that the sentences are separated by "."
and the words – by non-letter symbols.
We are living in a yellow submarine. We don't have
anything else. Inside the submarine is very tight.
So we are drinking all the day. We will move out
of it in 5 days.
We are living in a yellow submarine.
We will move out of it in 5 days.
63
Exercises (6)
9. We are given a string containing a list of forbidden
words and a text containing some of these words.
Write a program that replaces the forbidden words
with asterisks. Example:
Words: "PHP, CLR, Microsoft"
The expected result:
Microsoft announced its next generation PHP
compiler today. It is based on .NET Framework 4.0
and is implemented as a dynamic language in CLR.
********* announced its next generation ***
compiler today. It is based on .NET Framework 4.0
and is implemented as a dynamic language in ***.
64
Exercises (7)
10. Write a program that converts a string to a
sequence of C# Unicode character literals. Use
format strings. Sample input:
Expected output:
11. Write a program that reads a number and prints it
as a decimal number, hexadecimal number,
percentage and in scientific notation. Format the
output aligned right in 15 symbols.
Hi!
u0048u0069u0021
65
Exercises (8)
12. Write a program that parses an URL address given
in the format:
and extracts from it the [protocol], [server]
and [resource] elements. For example from the
URL http://www.devbg.org/forum/index.php
the following information should be extracted:
[protocol] = "http"
[server] = "www.devbg.org"
[resource] = "/forum/index.php"
[protocol]://[server]/[resource]
66
Exercises (9)
13. Write a program that reverses the words in given
sentence.
Example: "C# is not C++, not PHP and not Delphi!"
 "Delphi not and PHP, not C++ not is C#!".
14. A dictionary is stored as a sequence of text lines
containing words and their explanations. Write a
program that enters a word and translates it by
using the dictionary. Sample dictionary:
.NET – platform for applications from Microsoft
CLR – managed execution environment for .NET
namespace – hierarchical organization of classes
67
Exercises (10)
15. Write a program that replaces in a HTML
document given as string all the tags <a
href="…">…</a> with corresponding tags
[URL=…]…/URL]. Sample HTML fragment:
<p>Please visit <a href="http://academy.telerik.
com">our site</a> to choose a training course. Also
visit <a href="www.devbg.org">our forum</a> to
discuss the courses.</p>
<p>Please visit [URL=http://academy.telerik.
com]our site[/URL] to choose a training course.
Also visit [URL=www.devbg.org]our forum[/URL] to
discuss the courses.</p>
68
Exercises (11)
16. Write a program that reads two dates in the
format: day.month.year and calculates the
number of days between them. Example:
17. Write a program that reads a date and time given
in the format: day.month.year
hour:minute:second and prints the date and
time after 6 hours and 30 minutes (in the same
format) along with the day of week in Bulgarian.
Enter the first date: 27.02.2006
Enter the second date: 3.03.2004
Distance: 4 days
69
Exercises (12)
18. Write a program for extracting all email addresses
from given text. All substrings that match the
format <identifier>@<host>…<domain> should
be recognized as emails.
19. Write a program that extracts from a given text all
dates that match the format DD.MM.YYYY. Display
them in the standard date format for Canada.
20. Write a program that extracts from a given text all
palindromes, e.g. "ABBA", "lamal", "exe".
70
Exercises (13)
21. Write a program that reads a string from the
console and prints all different letters in the string
along with information how many times each
letter is found.
22. Write a program that reads a string from the
console and lists all different words in the string
along with information how many times each
word is found.
23. Write a program that reads a string from the
console and replaces all series of consecutive
identical letters with a single one. Example:
"aaaaabbbbbcdddeeeedssaa"  "abcdedsa".
71
Exercises (14)
24. Write a program that reads a list of words,
separated by spaces and prints the list in an
alphabetical order.
25. Write a program that extracts from given HTML
file its title (if available), and its body text without
the HTML tags. Example:
<html>
<head><title>News</title></head>
<body><p><a href="http://academy.telerik.com">Telerik
Academy</a>aims to provide free real-world practical
training for young people who want to turn into
skillful .NET software engineers.</p></body>
</html>
72
FreeTrainings @Telerik Academy
 Fundamentals of C# Programming
Course
 csharpfundamentals.telerik.com
 Telerik Software Academy
 academy.telerik.com
 Telerik Academy @ Facebook
 facebook.com/TelerikAcademy
 Telerik Software Academy Forums
 forums.academy.telerik.com

More Related Content

What's hot (20)

String c
String cString c
String c
 
Strings
StringsStrings
Strings
 
String in c
String in cString in c
String in c
 
String C Programming
String C ProgrammingString C Programming
String C Programming
 
C string
C stringC string
C string
 
Strings in C
Strings in CStrings in C
Strings in C
 
Computer programming 2 Lesson 12
Computer programming 2  Lesson 12Computer programming 2  Lesson 12
Computer programming 2 Lesson 12
 
Implementation Of String Functions In C
Implementation Of String Functions In CImplementation Of String Functions In C
Implementation Of String Functions In C
 
Chapter 22. Lambda Expressions and LINQ
Chapter 22. Lambda Expressions and LINQChapter 22. Lambda Expressions and LINQ
Chapter 22. Lambda Expressions and LINQ
 
String functions in C
String functions in CString functions in C
String functions in C
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
 
string in C
string in Cstring in C
string in C
 
13. Java text processing
13.  Java text processing13.  Java text processing
13. Java text processing
 
09. Java Methods
09. Java Methods09. Java Methods
09. Java Methods
 
C programming - String
C programming - StringC programming - String
C programming - String
 
Strings
StringsStrings
Strings
 
Strings
StringsStrings
Strings
 
16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues
 
Operation on string presentation
Operation on string presentationOperation on string presentation
Operation on string presentation
 
15. Streams Files and Directories
15. Streams Files and Directories 15. Streams Files and Directories
15. Streams Files and Directories
 

Viewers also liked

Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structuresPradipta Mishra
 
Fremeatfeisgiltt2015 fremelinkeddatalocalisers-150603090934-lva1-app6891
Fremeatfeisgiltt2015 fremelinkeddatalocalisers-150603090934-lva1-app6891Fremeatfeisgiltt2015 fremelinkeddatalocalisers-150603090934-lva1-app6891
Fremeatfeisgiltt2015 fremelinkeddatalocalisers-150603090934-lva1-app6891FREMEProjectH2020
 
Freme at feisgiltt 2015 freme use cases
Freme at feisgiltt 2015   freme use casesFreme at feisgiltt 2015   freme use cases
Freme at feisgiltt 2015 freme use casesFREMEProjectH2020
 
Freme general-overview-version-june-2015
Freme general-overview-version-june-2015Freme general-overview-version-june-2015
Freme general-overview-version-june-2015FREMEProjectH2020
 
3 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp013 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp01Abdul Samee
 
Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Abdul Samee
 
3 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp013 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp01Abdul Samee
 
13 recursion-120712074623-phpapp02
13 recursion-120712074623-phpapp0213 recursion-120712074623-phpapp02
13 recursion-120712074623-phpapp02Abdul Samee
 
Input outputdisplaydevices-140819061228-phpapp02
Input outputdisplaydevices-140819061228-phpapp02Input outputdisplaydevices-140819061228-phpapp02
Input outputdisplaydevices-140819061228-phpapp02Abdul Samee
 
arrays-120712074248-phpapp01
arrays-120712074248-phpapp01arrays-120712074248-phpapp01
arrays-120712074248-phpapp01Abdul Samee
 
C sharp part 001
C sharp part 001C sharp part 001
C sharp part 001Ralph Weber
 
Garima Sareen Nagpal - CV
Garima Sareen Nagpal - CVGarima Sareen Nagpal - CV
Garima Sareen Nagpal - CVGarima Sareen
 
Matemáticas de 4º de Primaria. Problemas y ejercicios.
Matemáticas de 4º de Primaria. Problemas y ejercicios.Matemáticas de 4º de Primaria. Problemas y ejercicios.
Matemáticas de 4º de Primaria. Problemas y ejercicios.Alfons
 

Viewers also liked (20)

Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structures
 
Fremeatfeisgiltt2015 fremelinkeddatalocalisers-150603090934-lva1-app6891
Fremeatfeisgiltt2015 fremelinkeddatalocalisers-150603090934-lva1-app6891Fremeatfeisgiltt2015 fremelinkeddatalocalisers-150603090934-lva1-app6891
Fremeatfeisgiltt2015 fremelinkeddatalocalisers-150603090934-lva1-app6891
 
Freme at feisgiltt 2015 freme use cases
Freme at feisgiltt 2015   freme use casesFreme at feisgiltt 2015   freme use cases
Freme at feisgiltt 2015 freme use cases
 
Freme general-overview-version-june-2015
Freme general-overview-version-june-2015Freme general-overview-version-june-2015
Freme general-overview-version-june-2015
 
Linked data tooling XML
Linked data tooling XMLLinked data tooling XML
Linked data tooling XML
 
3 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp013 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp01
 
Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01
 
3 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp013 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp01
 
13 recursion-120712074623-phpapp02
13 recursion-120712074623-phpapp0213 recursion-120712074623-phpapp02
13 recursion-120712074623-phpapp02
 
Input outputdisplaydevices-140819061228-phpapp02
Input outputdisplaydevices-140819061228-phpapp02Input outputdisplaydevices-140819061228-phpapp02
Input outputdisplaydevices-140819061228-phpapp02
 
Sasaki mlkrep-20150710
Sasaki mlkrep-20150710Sasaki mlkrep-20150710
Sasaki mlkrep-20150710
 
arrays-120712074248-phpapp01
arrays-120712074248-phpapp01arrays-120712074248-phpapp01
arrays-120712074248-phpapp01
 
C sharp part 001
C sharp part 001C sharp part 001
C sharp part 001
 
C# String
C# StringC# String
C# String
 
Garima Sareen Nagpal - CV
Garima Sareen Nagpal - CVGarima Sareen Nagpal - CV
Garima Sareen Nagpal - CV
 
C# Strings
C# StringsC# Strings
C# Strings
 
DSA - Lecture 04
DSA - Lecture 04DSA - Lecture 04
DSA - Lecture 04
 
Basic data-structures-v.1.1
Basic data-structures-v.1.1Basic data-structures-v.1.1
Basic data-structures-v.1.1
 
Matemáticas de 4º de Primaria. Problemas y ejercicios.
Matemáticas de 4º de Primaria. Problemas y ejercicios.Matemáticas de 4º de Primaria. Problemas y ejercicios.
Matemáticas de 4º de Primaria. Problemas y ejercicios.
 
Data structures
Data structuresData structures
Data structures
 

Similar to 16 strings-and-text-processing-120712074956-phpapp02

String and string manipulation x
String and string manipulation xString and string manipulation x
String and string manipulation xShahjahan Samoon
 
String and string manipulation
String and string manipulationString and string manipulation
String and string manipulationShahjahan Samoon
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdfssusere19c741
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanKavita Ganesan
 
13 Strings and text processing
13 Strings and text processing13 Strings and text processing
13 Strings and text processingmaznabili
 
Chapter 4 (Part II) - Array and Strings.pdf
Chapter 4 (Part II) - Array and Strings.pdfChapter 4 (Part II) - Array and Strings.pdf
Chapter 4 (Part II) - Array and Strings.pdfKirubelWondwoson1
 
L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)teach4uin
 
String and string buffer
String and string bufferString and string buffer
String and string bufferkamal kotecha
 
06 -working_with_strings
06  -working_with_strings06  -working_with_strings
06 -working_with_stringsHector Garzo
 
24_2-String and String Library.pptx
24_2-String and String Library.pptx24_2-String and String Library.pptx
24_2-String and String Library.pptxGandavadiVenkatesh1
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptxJawadTanvir
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string libraryMomenMostafa
 

Similar to 16 strings-and-text-processing-120712074956-phpapp02 (20)

String and string manipulation x
String and string manipulation xString and string manipulation x
String and string manipulation x
 
String and string manipulation
String and string manipulationString and string manipulation
String and string manipulation
 
Strings v.1.1
Strings v.1.1Strings v.1.1
Strings v.1.1
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita Ganesan
 
strings
stringsstrings
strings
 
13 Strings and text processing
13 Strings and text processing13 Strings and text processing
13 Strings and text processing
 
Chapter 4 (Part II) - Array and Strings.pdf
Chapter 4 (Part II) - Array and Strings.pdfChapter 4 (Part II) - Array and Strings.pdf
Chapter 4 (Part II) - Array and Strings.pdf
 
L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
06 -working_with_strings
06  -working_with_strings06  -working_with_strings
06 -working_with_strings
 
24_2-String and String Library.pptx
24_2-String and String Library.pptx24_2-String and String Library.pptx
24_2-String and String Library.pptx
 
Strings
StringsStrings
Strings
 
Arrays & Strings.pptx
Arrays & Strings.pptxArrays & Strings.pptx
Arrays & Strings.pptx
 
Unitii string
Unitii stringUnitii string
Unitii string
 
Matlab strings
Matlab stringsMatlab strings
Matlab strings
 
PostThis
PostThisPostThis
PostThis
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string library
 
C# overview part 1
C# overview part 1C# overview part 1
C# overview part 1
 

16 strings-and-text-processing-120712074956-phpapp02

  • 1. Strings andText Processing Processing and ManipulatingText Information Svetlin Nakov Telerik Software Academy academy.telerik.com/ ManagerTechnicalTrainer www.nakov.com/
  • 2. Table of Contents 1. What is String? 2. Creating and Using Strings  Declaring, Creating, Reading and Printing 3. Manipulating Strings  Comparing, Concatenating, Searching, Extracting Substrings, Splitting 4. Other String Operations  Replacing Substrings, Deleting Substrings, Changing Character Casing,Trimming 2
  • 3. Table of Contents (2) 5. Building and Modifying Strings  Why the + Operator is Slow?  Using the StringBuilder Class 6. Formatting Strings  Formatting Numbers, Dates and Currency 7. Cultures and Culture-Sensitive Formatting  Accessing and Assigning the Current Culture 8. Parsing Numbers and Dates 3
  • 5. What Is String?  Strings are sequences of characters  Each character is a Unicode symbol  Represented by the string data type in C# (System.String)  Example: string s = "Hello, C#"; H e l l o , C #s 5
  • 6. The System.String Class  Strings are represented by System.String objects in .NET Framework  String objects contain an immutable (read-only) sequence of characters  Strings use Unicode to support multiple languages and alphabets  Strings are stored in the dynamic memory (managed heap)  System.String is reference type 6
  • 7. The System.String Class (2)  String objects are like arrays of characters (char[])  Have fixed length (String.Length)  Elements can be accessed directly by index  The index is in the range [0...Length-1] string s = "Hello!"; int len = s.Length; // len = 6 char ch = s[1]; // ch = 'e' 0 1 2 3 4 5 H e l l o ! index = s[index] = 7
  • 8. Strings – First Example static void Main() { string s = "Stand up, stand up, Balkan Superman."; Console.WriteLine("s = "{0}"", s); Console.WriteLine("s.Length = {0}", s.Length); for (int i = 0; i < s.Length; i++) { Console.WriteLine("s[{0}] = {1}", i, s[i]); } } 8
  • 9. Strings – First Example Live Demo
  • 10. Creating and Using Strings Declaring, Creating, Reading and Printing
  • 11. Declaring Strings string str1; System.String str2; String str3;  Several ways of declaring string variables:  Using the C# keyword string  Using the .NET's fully qualified class name System.String  The above three declarations are equivalent 11
  • 12. Creating Strings  Before initializing a string variable has null value  Strings can be initialized by:  Assigning a string literal to the string variable  Assigning the value of another string variable  Assigning the result of operation of type string 12
  • 13. Creating Strings (2)  Not initialized variables has value of null  Assigning a string literal  Assigning from another string variable  Assigning from the result of string operation string s; // s is equal to null string s = "I am a string literal!"; string s2 = s; string s = 42.ToString(); 13
  • 14. Reading and Printing Strings  Reading strings from the console  Use the method Console.ReadLine() string s = Console.ReadLine(); Console.Write("Please enter your name: "); string name = Console.ReadLine(); Console.Write("Hello, {0}! ", name); Console.WriteLine("Welcome to our party!");  Printing strings to the console  Use the methods Write() and WriteLine() 14
  • 15. Reading and Printing Strings Live Demo
  • 16. Comparing, Concatenating, Searching, Extracting Substrings, Splitting Manipulating Strings
  • 17. Comparing Strings  Several ways to compare two strings:  Dictionary-based string comparison  Case-insensitive  Case-sensitive int result = string.Compare(str1, str2, true); // result == 0 if str1 equals str2 // result < 0 if str1 is before str2 // result > 0 if str1 is after str2 string.Compare(str1, str2, false); 17
  • 18. Comparing Strings (2)  Equality checking by operator ==  Performs case-sensitive compare  Using the case-sensitive Equals() method  The same effect like the operator == if (str1 == str2) { … } if (str1.Equals(str2)) { … } 18
  • 19. Comparing Strings – Example  Finding the first string in a lexicographical order from a given list of strings: string[] towns = {"Sofia", "Varna", "Plovdiv", "Pleven", "Bourgas", "Rousse", "Yambol"}; string firstTown = towns[0]; for (int i=1; i<towns.Length; i++) { string currentTown = towns[i]; if (String.Compare(currentTown, firstTown) < 0) { firstTown = currentTown; } } Console.WriteLine("First town: {0}", firstTown); 19
  • 21. Concatenating Strings  There are two ways to combine strings:  Using the Concat() method  Using the + or the += operators  Any object can be appended to a string string str = String.Concat(str1, str2); string str = str1 + str2 + str3; string str += str1; string name = "Peter"; int age = 22; string s = name + " " + age; //  "Peter 22" 21
  • 22. Concatenating Strings – Example string firstName = "Svetlin"; string lastName = "Nakov"; string fullName = firstName + " " + lastName; Console.WriteLine(fullName); // Svetlin Nakov int age = 25; string nameAndAge = "Name: " + fullName + "nAge: " + age; Console.WriteLine(nameAndAge); // Name: Svetlin Nakov // Age: 25 22
  • 24. Searching in Strings  Finding a character or substring within given string  First occurrence  First occurrence starting at given position  Last occurrence IndexOf(string str) IndexOf(string str, int startIndex) LastIndexOf(string) 24
  • 25. Searching in Strings – Example string str = "C# Programming Course"; int index = str.IndexOf("C#"); // index = 0 index = str.IndexOf("Course"); // index = 15 index = str.IndexOf("COURSE"); // index = -1 // IndexOf is case-sensetive. -1 means not found index = str.IndexOf("ram"); // index = 7 index = str.IndexOf("r"); // index = 4 index = str.IndexOf("r", 5); // index = 7 index = str.IndexOf("r", 8); // index = 18 0 1 2 3 4 5 6 7 8 9 10 11 12 13 … C # P r o g r a m m i n g … index = s[index] = 25
  • 27. Extracting Substrings  Extracting substrings  str.Substring(int startIndex, int length)  str.Substring(int startIndex) string filename = @"C:PicsRila2009.jpg"; string name = filename.Substring(8, 8); // name is Rila2009 string filename = @"C:PicsSummer2009.jpg"; string nameAndExtension = filename.Substring(8); // nameAndExtension is Summer2009.jpg 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 C : P i c s R i l a 2 0 0 5 . j p g 27
  • 29. Splitting Strings  To split a string by given separator(s) use the following method:  Example: string[] Split(params char[]) string listOfBeers = "Amstel, Zagorka, Tuborg, Becks."; string[] beers = listOfBeers.Split(' ', ',', '.'); Console.WriteLine("Available beers are:"); foreach (string beer in beers) { Console.WriteLine(beer); } 29
  • 31. Other String Operations Replacing Substrings, Deleting Substrings, Changing Character Casing,Trimming
  • 32. Replacing and Deleting Substrings  Replace(string, string) – replaces all occurrences of given string with another  The result is new string (strings are immutable)  Remove(index, length) – deletes part of a string and produces new string as result string cocktail = "Vodka + Martini + Cherry"; string replaced = cocktail.Replace("+", "and"); // Vodka and Martini and Cherry string price = "$ 1234567"; string lowPrice = price.Remove(2, 3); // $ 4567 32
  • 33. Changing Character Casing  Using method ToLower()  Using method ToUpper() string alpha = "aBcDeFg"; string lowerAlpha = alpha.ToLower(); // abcdefg Console.WriteLine(lowerAlpha); string alpha = "aBcDeFg"; string upperAlpha = alpha.ToUpper(); // ABCDEFG Console.WriteLine(upperAlpha); 33
  • 34. Trimming White Space  Using Trim()  Using Trim(chars)  Using TrimStart() and TrimEnd() string s = " example of white space "; string clean = s.Trim(); Console.WriteLine(clean); string s = " tnHello!!! n"; string clean = s.Trim(' ', ',' ,'!', 'n','t'); Console.WriteLine(clean); // Hello string s = " C# "; string clean = s.TrimStart(); // clean = "C# " 34
  • 36. Building and Modifying Strings Using the StringBuilder Class
  • 37. Constructing Strings  Strings are immutable!  Concat(), Replace(), Trim(), ... return new string, do not modify the old one  Do not use "+" for strings in a loop!  It runs very, very inefficiently! public static string DupChar(char ch, int count) { string result = ""; for (int i=0; i<count; i++) result += ch; return result; } Very bad practice. Avoid this! 37
  • 38. Slow Building Strings with + Live Demo
  • 39. StringBuilder: How It Works?  StringBuilder keeps a buffer memory, allocated in advance  Most operations use the buffer memory and do not allocate new objects H e l l o , C # !StringBuilder: Length=9 Capacity=15 Capacity used buffer (Length) unused buffer 39
  • 40. How the + Operator Performs String Concatenations?  Consider the following string concatenation:  It is equivalent to this code:  Several new objects are created and left to the garbage collector for deallocation  What happens when using + in a loop? string result = str1 + str2; StringBuilder sb = new StringBuilder(); sb.Append(str1); sb.Append(str2); string result = sb.ToString(); 40
  • 41. The StringBuilder Class  StringBuilder(int capacity) constructor allocates in advance buffer of given size  By default 16 characters are allocated  Capacity holds the currently allocated space (in characters)  this[int index] (indexer in C#) gives access to the char value at given position  Length holds the length of the string in the buffer 41
  • 42. The StringBuilder Class (2)  Append(…) appends a string or another object after the last character in the buffer  Remove(int startIndex, int length) removes the characters in given range  Insert(int index, string str) inserts given string (or object) at given position  Replace(string oldStr, string newStr) replaces all occurrences of a substring  ToString() converts the StringBuilder to String 42
  • 43. Changing the Contents of a String with StringBuilder  Use the System.Text.StringBuilder class for modifiable strings of characters:  Use StringBuilder if you need to keep adding characters to a string public static string ReverseString(string s) { StringBuilder sb = new StringBuilder(); for (int i = s.Length-1; i >= 0; i--) sb.Append(s[i]); return sb.ToString(); } 43
  • 44. StringBuilder – Another Example  Extracting all capital letters from a string public static string ExtractCapitals(string s) { StringBuilder result = new StringBuilder(); for (int i = 0; i<s.Length; i++) { if (Char.IsUpper(s[i])) { result.Append(s[i]); } } return result.ToString(); } 44
  • 46. Formatting Strings Using ToString() and String.Format()
  • 47. Method ToString()  All classes in C# have public virtual method ToString()  Returns a human-readable, culture-sensitive string representing the object  Most .NET Framework types have own implementation of ToString()  int, float, bool, DateTime int number = 5; string s = "The number is " + number.ToString(); Console.WriteLine(s); // The number is 5 47
  • 48. Method ToString(format)  We can apply specific formatting when converting objects to string  ToString(formatString) method int number = 42; string s = number.ToString("D5"); // 00042 s = number.ToString("X"); // 2A // Consider the default culture is Bulgarian s = number.ToString("C"); // 42,00 лв double d = 0.375; s = d.ToString("P2"); // 37,50 % 48
  • 49. Formatting Strings  The formatting strings are different for the different types  Some formatting strings for numbers:  D – number (for integer types)  C – currency (according to current culture)  E – number in exponential notation  P – percentage  X – hexadecimal number  F – fixed point (for real numbers) 49
  • 50. Method String.Format()  Applies templates for formatting strings  Placeholders are used for dynamic text  Like Console.WriteLine(…) string template = "If I were {0}, I would {1}."; string sentence1 = String.Format( template, "developer", "know C#"); Console.WriteLine(sentence1); // If I were developer, I would know C#. string sentence2 = String.Format( template, "elephant", "weigh 4500 kg"); Console.WriteLine(sentence2); // If I were elephant, I would weigh 4500 kg. 50
  • 51. Composite Formatting  The placeholders in the composite formatting strings are specified as follows:  Examples: {index[,alignment][:formatString]} double d = 0.375; s = String.Format("{0,10:F5}", d); // s = " 0,37500" int number = 42; Console.WriteLine("Dec {0:D} = Hex {1:X}", number, number); // Dec 42 = Hex 2A 51
  • 52. Formatting Dates  Dates have their own formatting strings  d, dd – day (with/without leading zero)  M, MM – month  yy, yyyy – year (2 or 4 digits)  h, HH, m, mm, s, ss – hour, minute, second DateTime now = DateTime.Now; Console.WriteLine( "Now is {0:d.MM.yyyy HH:mm:ss}", now); // Now is 31.11.2009 11:30:32 52
  • 53. Cultures  Cultures in .NET specify formatting / parsing settings specific to country / region / language  Printing the current culture:  Changing the current culture:  Culture-sensitive ToString(): 53 Console.WriteLine(System.Threading. Thread.CurrentThread.CurrentCulture); System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-CA"); CultureInfo culture = new CultureInfo("fr-CA"); string s = number.ToString("C", culture); // 42,00 $
  • 54. Parsing Numbers and Dates  Parsing numbers and dates is culture-sensitive  Parsing a real number using "." as separator:  Parsing a date in specific format: 54 string str = "3.14"; Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; float f = float.Parse(str); // f = 3.14 string dateStr = "25.07.2011"; DateTime date = DateTime.ParseExact(dateStr, "dd.MM.yyyy", CultureInfo.InvariantCulture);
  • 56. Summary  Strings are immutable sequences of characters (instances of System.String)  Declared by the keyword string in C#  Can be initialized by string literals  Most important string processing members are:  Length, this[], Compare(str1, str2), IndexOf(str), LastIndexOf(str), Substring(startIndex, length), Replace(oldStr, newStr), Remove(startIndex, length), ToLower(), ToUpper(), Trim() 56
  • 57. Summary (2)  Objects can be converted to strings and can be formatted in different styles (using ToString() method)  Strings can be constructed by using placeholders and formatting strings (String.Format(…)) 57
  • 58. форум програмиране,форум уеб дизайн курсове и уроци по програмиране,уеб дизайн – безплатно програмиранеза деца – безплатни курсове и уроци безплатен SEO курс -оптимизация за търсачки уроци по уеб дизайн, HTML,CSS, JavaScript,Photoshop уроци по програмиранеи уеб дизайн за ученици ASP.NET MVCкурс – HTML,SQL,C#,.NET,ASP.NETMVC безплатен курс"Разработка на софтуер в cloud среда" BG Coder -онлайн състезателна система -online judge курсове и уроци по програмиране,книги – безплатно отНаков безплатен курс"Качествен програменкод" алго академия – състезателно програмиране,състезания ASP.NET курс -уеб програмиране,бази данни, C#,.NET,ASP.NET курсове и уроци по програмиране– Телерик академия курсмобилни приложения с iPhone, Android,WP7,PhoneGap freeC#book, безплатна книга C#,книга Java,книга C# Дончо Минков -сайт за програмиране Николай Костов -блог за програмиране C#курс,програмиране,безплатно Strings andText Processing http://academy.telerik.com
  • 59. Exercises 1. Describe the strings in C#. What is typical for the string data type? Describe the most important methods of the String class. 2. Write a program that reads a string, reverses it and prints the result at the console. Example: "sample"  "elpmas". 3. Write a program to check if in a given expression the brackets are put correctly. Example of correct expression: ((a+b)/5-d). Example of incorrect expression: )(a+b)). 59
  • 60. Exercises (2) 4. Write a program that finds how many times a substring is contained in a given text (perform case insensitive search). Example:The target substring is "in".The text is as follows: The result is: 9. We are living in an yellow submarine. We don't have anything else. Inside the submarine is very tight. So we are drinking all the day. We will move out of it in 5 days. 60
  • 61. Exercises (3) 5. You are given a text. Write a program that changes the text in all regions surrounded by the tags <upcase> and </upcase> to uppercase.The tags cannot be nested. Example: The expected result: We are living in a <upcase>yellow submarine</upcase>. We don't have <upcase>anything</upcase> else. We are living in a YELLOW SUBMARINE. We don't have ANYTHING else. 61
  • 62. Exercises (4) 6. Write a program that reads from the console a string of maximum 20 characters. If the length of the string is less than 20, the rest of the characters should be filled with '*'. Print the result string into the console. 7. Write a program that encodes and decodes a string using given encryption key (cipher).The key consists of a sequence of characters.The encoding/decoding is done by performing XOR (exclusive or) operation over the first letter of the string with the first of the key, the second – with the second, etc. When the last key character is reached, the next is the first. 62
  • 63. Exercises (5) 8. Write a program that extracts from a given text all sentences containing given word. Example:The word is "in".The text is: The expected result is: Consider that the sentences are separated by "." and the words – by non-letter symbols. We are living in a yellow submarine. We don't have anything else. Inside the submarine is very tight. So we are drinking all the day. We will move out of it in 5 days. We are living in a yellow submarine. We will move out of it in 5 days. 63
  • 64. Exercises (6) 9. We are given a string containing a list of forbidden words and a text containing some of these words. Write a program that replaces the forbidden words with asterisks. Example: Words: "PHP, CLR, Microsoft" The expected result: Microsoft announced its next generation PHP compiler today. It is based on .NET Framework 4.0 and is implemented as a dynamic language in CLR. ********* announced its next generation *** compiler today. It is based on .NET Framework 4.0 and is implemented as a dynamic language in ***. 64
  • 65. Exercises (7) 10. Write a program that converts a string to a sequence of C# Unicode character literals. Use format strings. Sample input: Expected output: 11. Write a program that reads a number and prints it as a decimal number, hexadecimal number, percentage and in scientific notation. Format the output aligned right in 15 symbols. Hi! u0048u0069u0021 65
  • 66. Exercises (8) 12. Write a program that parses an URL address given in the format: and extracts from it the [protocol], [server] and [resource] elements. For example from the URL http://www.devbg.org/forum/index.php the following information should be extracted: [protocol] = "http" [server] = "www.devbg.org" [resource] = "/forum/index.php" [protocol]://[server]/[resource] 66
  • 67. Exercises (9) 13. Write a program that reverses the words in given sentence. Example: "C# is not C++, not PHP and not Delphi!"  "Delphi not and PHP, not C++ not is C#!". 14. A dictionary is stored as a sequence of text lines containing words and their explanations. Write a program that enters a word and translates it by using the dictionary. Sample dictionary: .NET – platform for applications from Microsoft CLR – managed execution environment for .NET namespace – hierarchical organization of classes 67
  • 68. Exercises (10) 15. Write a program that replaces in a HTML document given as string all the tags <a href="…">…</a> with corresponding tags [URL=…]…/URL]. Sample HTML fragment: <p>Please visit <a href="http://academy.telerik. com">our site</a> to choose a training course. Also visit <a href="www.devbg.org">our forum</a> to discuss the courses.</p> <p>Please visit [URL=http://academy.telerik. com]our site[/URL] to choose a training course. Also visit [URL=www.devbg.org]our forum[/URL] to discuss the courses.</p> 68
  • 69. Exercises (11) 16. Write a program that reads two dates in the format: day.month.year and calculates the number of days between them. Example: 17. Write a program that reads a date and time given in the format: day.month.year hour:minute:second and prints the date and time after 6 hours and 30 minutes (in the same format) along with the day of week in Bulgarian. Enter the first date: 27.02.2006 Enter the second date: 3.03.2004 Distance: 4 days 69
  • 70. Exercises (12) 18. Write a program for extracting all email addresses from given text. All substrings that match the format <identifier>@<host>…<domain> should be recognized as emails. 19. Write a program that extracts from a given text all dates that match the format DD.MM.YYYY. Display them in the standard date format for Canada. 20. Write a program that extracts from a given text all palindromes, e.g. "ABBA", "lamal", "exe". 70
  • 71. Exercises (13) 21. Write a program that reads a string from the console and prints all different letters in the string along with information how many times each letter is found. 22. Write a program that reads a string from the console and lists all different words in the string along with information how many times each word is found. 23. Write a program that reads a string from the console and replaces all series of consecutive identical letters with a single one. Example: "aaaaabbbbbcdddeeeedssaa"  "abcdedsa". 71
  • 72. Exercises (14) 24. Write a program that reads a list of words, separated by spaces and prints the list in an alphabetical order. 25. Write a program that extracts from given HTML file its title (if available), and its body text without the HTML tags. Example: <html> <head><title>News</title></head> <body><p><a href="http://academy.telerik.com">Telerik Academy</a>aims to provide free real-world practical training for young people who want to turn into skillful .NET software engineers.</p></body> </html> 72
  • 73. FreeTrainings @Telerik Academy  Fundamentals of C# Programming Course  csharpfundamentals.telerik.com  Telerik Software Academy  academy.telerik.com  Telerik Academy @ Facebook  facebook.com/TelerikAcademy  Telerik Software Academy Forums  forums.academy.telerik.com

Editor's Notes

  1. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  2. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  3. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  4. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  5. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  6. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  7. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  8. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  9. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  10. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  11. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  12. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  13. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  14. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  15. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  16. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  17. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  18. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  19. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  20. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  21. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  22. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*