Strings and Text Processing Processing and Manipulating Text SoftUni Team Technical Trainers Software University

Slides:



Advertisements
Similar presentations
Java Programming Strings Chapter 7.
Advertisements

Processing and Manipulating Text Information Svetlin Nakov Telerik Corporation
 Dimitar Ivanov Introduction to programming with microcontrollers.
C# Advanced Topics Methods, Classes and Objects SoftUni Team Technical Trainers Software University
Methods Writing and using methods, overloads, ref, out SoftUni Team Technical Trainers Software University
Software University Curriculum, Courses, Exams, Jobs SoftUni Team Technical Trainers Software University
Fundamentals SoftUni Welcome to Software University SoftUni Team Technical Trainers Software University
AngularJS Directives Defining Custom Directives SoftUni Team Technical Trainers Software University
Software Testing Lifecycle Exit Criteria Evaluation, Continuous Integration Ivan Yonkov Technical Trainer Software University.
Teamwork and Personal Skills Course Introduction Software University SoftUni Team Technical Trainers.
Conditional Statements Implementing Control-Flow Logic in C# SoftUni Team Technical Trainers Software University
Loops Repeating Code Multiple Times SoftUni Team Technical Trainers Software University
Strings in PHP Working with Text in PHP Strings and String Functions Mario Peshev Technical Trainer Software University
Methods, Arrays, Lists, Dictionaries, Strings, Classes and Objects
Svetlin Nakov Technical Trainer Software University
Build Processes and Continuous Integration Automating Build Processes Software University Technical Trainers SoftUni Team.
Processing and Manipulating Text Information Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Processing Redis with.NET How to Operate with Redis Databases SoftUni Team Technical Trainers Software University
Multidimensional Arrays, Sets, Dictionaries Processing Matrices, Multidimensional Arrays, Dictionaries, Sets SoftUni Team Technical Trainers Software University.
Test-Driven Development Learn the "Test First" Approach to Coding SoftUni Team Technical Trainers Software University
Defining Classes Classes, Fields, Constructors, Methods, Properties SoftUni Team Technical Trainers Software University
Functions Reusable Parts of Code SoftUni Team Technical Trainers Software University
Java Collections Basics Arrays, Lists, Strings, Sets, Maps Svetlin Nakov Technical Trainer Software University
Graphs and Graph Algorithms Fundamentals, Terminology, Traversal, Algorithms SoftUni Team Technical Trainers Software University
Arrays, Lists, Stacks, Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Using SQL Connecting, Retrieving Data, Executing SQL Commands, … Svetlin Nakov Technical Trainer Software University
C# Basics Course Introduction Svetlin Nakov Technical Trainer Software University
String String Builder. System.String string is the alias for System.String A string is an object of class string in the System namespace representing.
Jekyll Static Site Generator Template-Based Site Generation Svetlin Nakov Technical Trainer Software University
Forms Overview, Query string, Submitting arrays, PHP & HTML, Input types, Redirecting the user Mario Peshev Technical Trainer Software.
Processing and Manipulating Text Information Learning & Development Team Telerik Software Academy.
Exam Preparation Algorithms Course: Sample Exam SoftUni Team Technical Trainers Software University
Console Input / Output Reading and Writing to the Console SoftUni Team Technical Trainers Software University
Processing JSON in.NET JSON, JSON.NET LINQ-to-JSON and JSON to XML SoftUni Team Technical Trainers Software University
Associative Arrays and Objects Associative Arrays, Objects Svetlin Nakov Technical Trainer Software University
Regular Expressions /^Hel{2}o\s*World\n$/ SoftUni Team Technical Trainers Software University
Java Collections Basics Arrays, Lists, Strings, Sets, Maps Bogomil Dimitrov Technical Trainer Software University
Processing and Manipulating Text Information Telerik Software Academy C# Fundamentals – Part 2.
Design Patterns: Structural Design Patterns General and reusable solutions to common problems in software design Software University
Advanced C# Course Introduction SoftUni Team Technical Trainers Software University
Reflection Programming under the hood SoftUni Team Technical Trainers Software University
C# Advanced Topics Methods, Arrays, Lists, Dictionaries, Strings, Classes and Objects SoftUni Team Technical Trainers Software University
Mocking with Moq Tools for Easier Unit Testing SoftUni Team Technical Trainers Software University
Regular Expressions /^Hel{2}o\s*World\n$/ SoftUni Team Technical Trainers Software University
Operators and Expressions
Mocking Unit Testing Methods with External Dependencies SoftUni Team Technical Trainers Software University
Mocking with Moq Mocking tools for easier unit testing Svetlin Nakov Technical Trainer Software University
Test-Driven Development Learn the "Test First" Approach to Coding Svetlin Nakov Technical Trainer Software University
Processing Sequences of Elements
Strings, Dictionaries, Lambda and LINQ Text Processing, Dictionaries, Lambda Functions, LINQ SoftUni Team Technical Trainers Software University
Sets, Dictionaries SoftUni Team Technical Trainers Software University
Advanced Tree Structures Binary Trees, AVL Tree, Red-Black Tree, B-Trees, Heaps SoftUni Team Technical Trainers Software University
Functional Programming Data Aggregation and Nested Queries Ivan Yonkov Technical Trainer Software University
Doctrine The PHP ORM SoftUni Team Technical Trainers Software University
Creating Content Defining Topic, Creating Technical Training Materials SoftUni Team Technical Trainers Software University
First Steps in PHP Creating Very Simple PHP Scripts SoftUni Team Technical Trainers Software University
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Generics SoftUni Team Technical Trainers Software University
Strings and Text Processing
Strings and Text Processing
Strings and Text Processing
Arrays, Lists, Stacks, Queues
Fast String Manipulation
Processing Variable-Length Sequences of Elements
Data Definition and Data Types
String String Builder.
Processing and Manipulating Text
Text Processing and Regex API
Strings and Text Processing
Presentation transcript:

Strings and Text Processing Processing and Manipulating Text SoftUni Team Technical Trainers Software University

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

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

What Is a String?

 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 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 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' Hello! index = str[index] =

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]); }}

Strings Live Demo

Creating and Using Strings Declaring, Creating, Reading and Printing

 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

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

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

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  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()

Reading and Printing Strings Live Demo

Manipulating Strings Comparing, Concatenating, Searching, Extracting Substrings, Splitting

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);

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  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);

Comparing Strings Live Demo

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

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

Concatenating Strings Live Demo

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 = int firstIndex = // 5 int noIndex = .IndexOf("/"); // -1 string verse = "To be or not to be.."; int lastIndex = verse.LastIndexOf("be"); // 16

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 = … C# Programming… index = str[index] = 26

Searching in Strings Live Demo

Extracting Substrings  Extracting substrings  str.Substring(int startIndex, int length)  str.Substring(int startIndex) string filename string name = filename.Substring(8, 8); // name is Rila2009 string filename string nameAndExtension = filename.Substring(8); // nameAndExtension is Summer2009.jpg C:\Pics\Rila2005.jpg 28

Extracting Substrings Live Demo

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

Splitting Strings Live Demo

Other String Operations Replacing Substrings, Deleting Substrings, Changing Character Casing, Trimming

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 = "$ "; string lowPrice = price.Remove(2, 3); // $

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  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# ";

Other String Operations Live Demo

Building and Modifying Strings Using the StringBuilder Class

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

Slow Building Strings with + Live Demo

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 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();}

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

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  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();}

Using StringBuilder Live Demo

Exercises in Class

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

? ? ? ? ? ? ? ? ? Strings and Text Processing

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

Free Software University  Software University Foundation – softuni.orgsoftuni.org  Software University – High-Quality Education, Profession and Job for Software Developers  softuni.bg softuni.bg  Software Facebook  facebook.com/SoftwareUniversity facebook.com/SoftwareUniversity  Software YouTube  youtube.com/SoftwareUniversity youtube.com/SoftwareUniversity  Software University Forums – forum.softuni.bgforum.softuni.bg