Download presentation
Presentation is loading. Please wait.
Published byJayson Porter Modified over 8 years ago
1
Strings and Text Processing Processing and Manipulating Text SoftUni Team Technical Trainers Software University http://softuni.bg
2
Table of Contents 1.What is a String? 2.Creating and Using Strings Declaring, Creating, Reading and Printing 3.Manipulating Strings Comparing, Concatenating, Searching Extracting Substrings, Splitting 2
3
Table of Contents (2) 4.Other String Operators Replacing Substrings, Deleting Substrings Changing Character Casing, Trimming 5.Building and Modifying Strings Why the + Operator is Slow? StringBuilder Using the StringBuilder Class 3
4
What Is a String?
5
Strings are sequences of characters Each character is a Unicode symbol string System.String Represented by the string data type in C# ( System.String ) Example: string str = "Hello, C#"; Hello, C#str 5
6
6 System.String 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 System.String is a reference type The System.String Class
7
7 char[] String objects are like arrays of characters ( char[] ) String.Length Have fixed length ( String.Length ) Elements can be accessed directly by index The index is in the range [0... Length - 1] The System.String Class (2) string str = "Hello!"; int length = str.Length; // length = 6 char ch = str[1]; // ch = 'e' 012345 Hello! index = str[index] =
8
8 Strings – First Example static void Main() { string song = "Stand up, stand up, Balkan Superman."; string song = "Stand up, stand up, Balkan Superman."; Console.WriteLine("song = \"{0}\"", song); Console.WriteLine("song = \"{0}\"", song); Console.WriteLine("song.Length = {0}", song.Length); Console.WriteLine("song.Length = {0}", song.Length); for (int i = 0; i < song.Length; i++) for (int i = 0; i < song.Length; i++) { Console.WriteLine("song[{0}] = {1}", i, song[i]); Console.WriteLine("song[{0}] = {1}", i, song[i]); }}
9
Strings Live Demo
10
Creating and Using Strings Declaring, Creating, Reading and Printing
11
Several ways of declaring string variables: string Using the C# keyword string System.String Using the.NET's fully qualified class System.String The above three declarations are equivalent Declaring Strings string str1; System.String str2; String str3; 11
12
Creating Strings null 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 new string() Using the string constructor – new string() 12
13
Creating Strings (2) null Uninitialized variables have value of null Assigning a string literal Assigning from another string variable string str; // str is equal to null string str = "I am a string literal!"; string str2 = str; 13
14
Creating Strings (3) Assigning from the result of a string operation char Using the string constructor with a repeating char char[] Using the string constructor with a char[] string str = new string('*', 20); string str = new string({ 'h', 'e', 'l', 'l', 'o' }); string str = 42.ToString(); 14
15
15 Reading strings from the console Console.ReadLine() Use the method Console.ReadLine() Reading and Printing Strings 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 Write() WriteLine() Use the methods Write() and WriteLine()
16
Reading and Printing Strings Live Demo
17
Manipulating Strings Comparing, Concatenating, Searching, Extracting Substrings, Splitting
18
18 Several ways to compare two strings: Dictionary-based string comparison Case-insensitive Case-sensitive Comparing Strings 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 int result = string.Compare(str1, str2, false);
19
Comparing Strings (2) == Equality checking by operator == Performs case-sensitive comparison Equals() Using the case-sensitive Equals() method == The same effect like the operator == if (str1 == str2) { …} if (str1.Equals(str2)) { …} 19
20
20 Finding the first string in a lexicographical order from a given list of strings: Comparing Strings – Example string[] towns = { "Sofia", "Varna", "Plovdiv", "Pleven", "Bourgas", "Rousse", "Yambol" }; "Rousse", "Yambol" }; string firstTown = towns[0]; for (int i = 1; i < towns.Length; i++) { string currentTown = towns[i]; string currentTown = towns[i]; if (string.Compare(currentTown, firstTown) < 0) if (string.Compare(currentTown, firstTown) < 0) { firstTown = currentTown; firstTown = currentTown; }} Console.WriteLine("First town: {0}", firstTown);
21
Comparing Strings Live Demo
22
Concatenating Strings There are two ways to combine strings: Concat() 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" 22
23
Concatenating Strings – Example string firstName = "Software"; string lastName = "University"; string fullName = firstName + " " + lastName; Console.WriteLine(fullName); // Software University int age = 5; string nameAndAge = "Name: " + fullName + "\nAge: " + age; Console.WriteLine(nameAndAge); // Name: Software University // Age: 5 23
24
Concatenating Strings Live Demo
25
25 Finding a character or substring within given string str.IndexOf(string term) – returns the index of the first occurrence of term in str Returns -1 if there is no match str.LastIndexOf(string term) – returns the index of the last occurrence of term in str Searching in Strings string email = "vasko@gmail.org"; int firstIndex = email.IndexOf("@"); // 5 int noIndex = email.IndexOf("/"); // -1 string verse = "To be or not to be.."; int lastIndex = verse.LastIndexOf("be"); // 16
26
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 012345678910111213… C# Programming… index = str[index] = 26
27
Searching in Strings Live Demo
28
Extracting Substrings Extracting substrings str.Substring(int startIndex, int length) str.Substring(int startIndex) string filename = @"C:\Pics\Rila2009.jpg"; string name = filename.Substring(8, 8); // name is Rila2009 string filename = @"C:\Pics\Summer2009.jpg"; string nameAndExtension = filename.Substring(8); // nameAndExtension is Summer2009.jpg 012345678910111213141516171819 C:\Pics\Rila2005.jpg 28
29
Extracting Substrings Live Demo
30
Splitting Strings To split a string by given separator(s) use the following method: Example: string[] Split(params char[] separator) string listOfBeers = "Amstel, Zagorka, Tuborg, Becks."; string[] beers = listOfBeers.Split(' ', ',', '.'); Console.WriteLine("Available beers are:"); foreach (string beer in beers) { Console.WriteLine(beer); Console.WriteLine(beer);} 30
31
Splitting Strings Live Demo
32
Other String Operations Replacing Substrings, Deleting Substrings, Changing Character Casing, Trimming
33
Replacing and Deleting Substrings str.Replace(string match, string term) – replaces all occurrences of given string with another The result is a new string (strings are immutable) str.Remove(int index, int length) str.Remove(int index, int length) – deletes part of a string and produces a 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 33
34
Changing Character Casing ToLower() Using the method ToLower() ToUpper() Using the method ToUpper() string alpha = "aBcDeFg"; string lowerAlpha = alpha.ToLower(); // abcdefg Console.WriteLine(lowerAlpha); string alpha = "aBcDeFg"; string upperAlpha = alpha.ToUpper(); // ABCDEFG Console.WriteLine(upperAlpha); 34
35
35 str.Trim() – trims whitespaces at start and end of string Trim(params char[] chars) str.Trim(params char[] chars) str.TrimStart() and str.TrimEnd() Trimming White Space string s = " example of white space "; string clean = s.Trim(); Console.WriteLine(clean); // example of white space string s = " \t\nHello!!! \n"; string clean = s.Trim(' ', ',','!', '\n','\t'); Console.WriteLine(clean); // Hello string s = " C# "; string clean = s.TrimStart(); // clean = "C# ";
36
Other String Operations Live Demo
37
Building and Modifying Strings Using the StringBuilder Class
38
Constructing Strings Strings are immutable! Concat()Replace()Trim() Concat(), Replace(), Trim(),... return a new string (allocate new piece of memory), do not modify the old one Do not concatenate strings in loops! Results in terrible performance. public static string ConcatenateChar(char ch, int count) { string result = string.Empty; string result = string.Empty; for (int i = 0; i < count; i++) for (int i = 0; i < count; i++) { result += ch; } return result; return result;} Very bad practice. Avoid this! 38
39
Slow Building Strings with + Live Demo
40
StringBuilder: How It Works? StringBuilder StringBuilder keeps a buffer memory, allocated in advance Most operations use the buffer memory and do not allocate new objectsHello,C#! StringBuilder : Length = 9 Capacity = 15 Capacity used buffer (Length) (Length) unused buffer 40
41
41 System.Text.StringBuilder Use the System.Text.StringBuilder class for modifiable strings of characters: StringBuilder Use StringBuilder if you need to keep adding characters to a string Changing the Contents of a String public static string ReverseString(string s) { StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder(); for (int i = s.Length - 1; i >= 0; i--) for (int i = s.Length - 1; i >= 0; i--) { sb.Append(s[i]); sb.Append(s[i]); } return sb.ToString(); return sb.ToString();}
42
The StringBuilder Class StringBuilder(int capacity) StringBuilder(int capacity) constructor allocates in advance buffer of size capacity By default 16 characters are allocated Capacity Capacity holds the currently allocated space (in characters) this[int index] this[int index] (indexer in C#) gives access to the char value at given position Length Length holds the length of the string in the buffer 42
43
The StringBuilder Class (2) sb.Append(…) sb.Append(…) appends a string or another object after the last character in the buffer sb.Remove(int startIndex, int length) sb.Remove(int startIndex, int length) removes the characters in given range sb.Insert(int index, string str) sb.Insert(int index, string str) inserts given string (or object) at given position sb.Replace(string oldStr, string newStr) sb.Replace(string oldStr, string newStr) replaces all occurrences of a substring sb.ToString() StringBuilder String sb.ToString() converts the StringBuilder to String 43
44
44 Extracting all capital letters from a string StringBuilder – Another Example public static string ExtractCapitals(string s) { StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder(); for (int i = 0; i < s.Length; i++) for (int i = 0; i < s.Length; i++) { if (char.IsUpper(s[i])) if (char.IsUpper(s[i])) { result.Append(s[i]); result.Append(s[i]); } } return result.ToString(); return result.ToString();}
45
Using StringBuilder Live Demo
46
Exercises in Class
47
Summary Strings are immutable sequences of System.String characters (instances of System.String ) Can only be iterated Support operations such as Substring(), IndexOf(), Trim(), etc. Changes to the string create a new object, instead of modifying the old one StringBuilder offers good performance Recommended when concatenating strings in a loop 47
48
? ? ? ? ? ? ? ? ? http://softuni.org/courses Strings and Text Processing
49
License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" licenseCreative Commons Attribution- NonCommercial-ShareAlike 4.0 International Attribution: this work may contain portions from "Fundamentals of Computer Programming with C#" book by Svetlin Nakov & Co. under CC-BY-SA licenseFundamentals of Computer Programming with C#CC-BY-SA "C# Part I" course by Telerik Academy under CC-BY-NC-SA licenseC# Part ICC-BY-NC-SA "C# Part II" course by Telerik Academy under CC-BY-NC-SA licenseC# Part IICC-BY-NC-SA 49
50
Free Trainings @ Software University Software University Foundation – softuni.orgsoftuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg softuni.bg Software University @ Facebook facebook.com/SoftwareUniversity facebook.com/SoftwareUniversity Software University @ YouTube youtube.com/SoftwareUniversity youtube.com/SoftwareUniversity Software University Forums – forum.softuni.bgforum.softuni.bg
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.