Strings and Text Processing

Slides:



Advertisements
Similar presentations
Processing and Manipulating Text Information Svetlin Nakov Telerik Corporation
Advertisements

Methods, Arrays, Lists, Dictionaries, Strings, Classes and Objects
Strings and Text Processing Processing and Manipulating Text SoftUni Team Technical Trainers Software University
Arrays, Lists, Stacks, Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Processing and Manipulating Text Information Learning & Development Team Telerik Software Academy.
Processing and Manipulating Text Information Telerik Software Academy C# Fundamentals – Part 2.
Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Generics SoftUni Team Technical Trainers Software University
Strings and Text Processing
Graphs and Graph Algorithms
Version Control Systems
Static Members and Namespaces
Functional Programming
Databases basics Course Introduction SoftUni Team Databases basics
Abstract Classes, Abstract Methods, Override Methods
Sets, Hash table, Dictionaries
Strings and Text Processing
C# Basic Syntax, Visual Studio, Console Input / Output
Interface Segregation / Dependency Inversion
Data Structures Course Overview SoftUni Team Data Structures
C# Basic Syntax, Visual Studio, Console Input / Output
Reflection SoftUni Team Technical Trainers Java OOP Advanced
/^Hel{2}o\s*World\n$/
Linear Data Structures: Stacks and Queues
Classes, Properties, Constructors, Objects, Namespaces
EF Code First (Advanced)
PHP MVC Frameworks MVC Fundamentals SoftUni Team Technical Trainers
Processing Sequences of Elements
Repeating Code Multiple Times
Inheritance Class Hierarchies SoftUni Team Technical Trainers C# OOP
Java OOP Overview Classes and Objects, Members and Class Definition, Access Modifier, Encapsulation Java OOP Overview SoftUni Team Technical Trainers.
Data Definition and Data Types
Databases advanced Course Introduction SoftUni Team Databases advanced
Arrays, Lists, Stacks, Queues
Balancing Binary Search Trees, Rotations
Debugging and Troubleshooting Code
Entity Framework: Relations
Fast String Manipulation
Array and List Algorithms
Functional Programming
Processing Variable-Length Sequences of Elements
Regular Expressions (RegEx)
C# Advanced Course Introduction SoftUni Team C# Technical Trainers
Databases Advanced Course Introduction SoftUni Team Databases Advanced
Combining Data Structures
Arrays and Multidimensional Arrays
Built-in Functions. Usage of Wildcards
Data Definition and Data Types
Multidimensional Arrays, Sets, Dictionaries
Extending functionality using Collections
Exporting and Importing Data
Language Comparison Java, C#, PHP and JS SoftUni Team
Functional Programming
C# Advanced Course Introduction SoftUni Team C# Technical Trainers
Exporting and Importing Data
CSS Transitions and Animations
Iterators and Comparators
Reflection SoftUni Team Technical Trainers C# OOP Advanced
Processing and Manipulating Text
Software Quality Assurance
Version Control Systems
Polymorphism, Interfaces, Abstract Classes
Text Processing and Regex API
/^Hel{2}o\s*World\n$/
Strings and Text Processing
Files, Directories, Exceptions
CSS Transitions and Animations
Iterators and Generators
File Types, Using Streams, Manipulating Files
Multidimensional Arrays
Presentation transcript:

Strings and Text Processing Processing and Manipulating Text Advanced C# SoftUni Team Technical Trainers Software University http://softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Table of Contents What is a String? Manipulating Strings Comparing, Concatenating, Searching Extracting Substrings, Splitting © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Table of Contents (2) Other String Operators Replacing Substrings, Deleting Substrings Changing Character Casing, Trimming Building and Modifying Strings Why the + Operator is Slow? Using the StringBuilder Class © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

sli.do #CSharp-Advanced Questions sli.do #CSharp-Advanced

What is a String? Strings are represented by System.String objects in .NET Framework String objects contain an immutable (read-only) sequence of characters Before initializing, a string variable has null value

Empty vs. Null

Comparing, Concatenating, Searching, Extracting Substrings, Splitting * Manipulating Strings Comparing, Concatenating, Searching, Extracting Substrings, Splitting (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

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 int result = string.Compare(str1, str2, false);

Comparing Strings (2) Equality checking by operator == Performs case-sensitive Ordinal comparison Using the case-sensitive Equals() method The same effect like the operator == if (str1 == str2) { … } if (str1.Equals(str2)) { … }

Comparing Strings Exercises in class © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

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"

Concatenating Strings Exercises in class © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Searching in Strings 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 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

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 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 l a . j p g

Resources = trainings/1531/java-advanced-january-2017 Problem: Parse URLs Write a program that parses an URL address given in the format: [protocol]://[server]/[resource] and extracts from it the Extract protocol, server and resource https://softuni.bg/courses/csharp-advanced Protocol = https Server = softuni.bg Resources = trainings/1531/java-advanced-january-2017

URL Solution: Parse URLs string[] reminder = input.split("://"); string protocol = reminder[0]; int serverEndIndex = reminder[1].IndexOf("/"); string server = reminder[1].Substring(0, serverEndIndex); string resource = reminder[1].Substring (serverEndIndex + 1); URL Check your solution here: https://judge.softuni.bg/Contests/Compete/Index/595

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

Splitting Strings Exercises in class © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Other String Operations * Other String Operations Replacing Substrings, Deleting Substrings, Changing Character Casing, Trimming (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

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

Changing Character Casing Using the method ToLower() 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);

Problem: Parse Tags Write a program that changes the text in all regions surrounded by the tags <upcase> and </upcase> to upper-case. The tags cannot be nested. 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.

Solution: Parse Tags //TODO: Read from console int startIndex = text.IndexOf(openTag); while (startIndex != -1) { int endIndex = text.IndexOf(closeTag); if (endIndex == -1) break; string upcase = text.Substring(startIndex, endIndex - startIndex + closeTag.Length); string replaceUpcase = upcase.Replace(openTag, "") .Replace(closeTag, "").ToUpper(); text = text.Replace(upcase, replaceUpcase); startIndex = text.IndexOf(openTag); }//TODO: Write to console

Trimming White Space str.Trim() – trims whitespaces at start and end of string str.Trim(params char[] chars) str.TrimStart() and str.TrimEnd() 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# ";

Padding str.PadLeft(int totalWidth, char symbol) – adds symbols at the begging str.PadRight(int totalWidth, char symbol) – adds symbols at the end string s = "Ana"; string padded = s.PadLeft(5, ' '); Console.WriteLine(padded); // ' Ana' string s = "Ana"; string padded = s.PadRight(5, ' '); Console.WriteLine(padded); // 'Ana '

Formating Strings string.Format() provides a format language to which we add variables. Insert and format double numbers Insert and convert to HEX value int number = 10; string value = string.Format("{0:f3}“, number); int number = 10; string value = string.Format("{0:X}“, number);

Formating Strings (2) Insert DateTime Padding string value = string.Format(“It is now {0:d} at {0:t}“, DateTime.Now); // It is now 4/10/2016 at 10:04 AM int number = 10; string value = string.Format("{0,10}“, number); //Padding Right string value = string.Format("{0,-10}“, number); //Padding Left

Other String Operations Exercises in class

Building and Modifying Strings Using the StringBuilder Class

StringBuilder: How It Works? Capacity StringBuilder: Length = 9 Capacity = 15 H e l o , C # ! used buffer (Length) unused buffer StringBuilder keeps a buffer memory, allocated in advance Most operations use the buffer memory and do not allocate new objects

Changing the Contents of a String * Changing the Contents of a String 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(); Introducing the StringBuffer Class StringBuffer represents strings that can be modified and extended at run time. The following example creates three new String objects, and copies all the characters each time a new String is created: String quote = "Fasten your seatbelts, "; quote = quote + "it's going to be a bumpy night."; It is more efficient to preallocate the amount of space required using the StringBuffer constructor, and its append() method as follows: StringBuffer quote = new StringBuffer(60); // alloc 60 chars quote.append("Fasten your seatbelts, "); quote.append(" it's going to be a bumpy night. "); StringBuffer also provides a number of overloaded insert() methods for inserting various types of data at a particular location in the string buffer. Instructor Note The example in the slide uses StringBuffer to reverse the characters in a string. A StringBuffer object is created, with the same length as the string. The loop traverses the String parameter in reverse order and appends each of its characters to the StringBuffer object by using append(). The StringBuffer therefore holds a reverse copy of the String parameter. At the end of the method, a new String object is created from the StringBuffer object, and this String is returned from the method. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

The StringBuilder Class StringBuilder(int capacity) constructor allocates in advance buffer of size capacity 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

The StringBuilder Class (2) sb.Append(…) appends a string or another object after the last character in the buffer sb.Remove(int startIndex, int length) removes the characters in given range sb.Insert(int index, string str) inserts given string (or object) at given position sb.Replace(string oldStr, string newStr) replaces all occurrences of a substring sb.ToString() converts the StringBuilder to String

Using StringBuilder Exercises in class

Summary Strings are immutable sequences of * Summary Strings are immutable sequences of 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 (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Strings and Text Processing https://softuni.bg/courses/advanced-csharp © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license Attribution: this work may contain portions from "Fundamentals of Computer Programming with C#" book by Svetlin Nakov & Co. under CC-BY-SA license "C# Part I" course by Telerik Academy under CC-BY-NC-SA license "C# Part II" course by Telerik Academy under CC-BY-NC-SA license © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Free Trainings @ Software University Software University Foundation – softuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software University @ Facebook facebook.com/SoftwareUniversity Software University @ YouTube youtube.com/SoftwareUniversity Software University Forums – forum.softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.