Docsity

Strings and Text Processing: Manipulating Strings in C# using .NET, Study Guides, Projects, Research of Programming Languages

Partial preview of the text

Download Strings and Text Processing: Manipulating Strings in C# using .NET and more Study Guides, Projects, Research Programming Languages in PDF only on Docsity!

Strings and Text Processing

Processing and Manipulating Text Using the .NET String Class

  • What is a String?
  • Manipulating Strings
    • Concatenating, Searching, Substring
    • Splitting, Replacing

Table of Contents

  • Strings are sequences of characters (texts)
  • The string data type in C#
    • Declared by the string keyword
    • Maps to System.String .NET data type
  • Strings are enclosed in quotes:
  • Concatenated using the "+" operator:

What is String?

string s = "Hello, C#"; string s = "Hello" + " " + "C#";

  • Strings are immutable (read-only) sequences of characters
  • Accessible by index (read-only)
  • Strings use Unicode (can use most alphabets, e.g. Arabic)

In C# Strings are Immutable, use Unicode

string str = "Hello, C#"; char ch = str[2]; // OK str[2] = 'a'; // Error! string greeting = "你好"; // (lí-hó) Taiwanese

MANIPULATING STRINGS

  • Use the + or the += operators
  • Use the Concat() method

Concatenating

string greet = "Hello, "; string name = "John"; string result = string.Concat(greet, name); Console.WriteLine(result); //"Hello, John" string text = "Hello, "; text += "John"; //"Hello, John" string text = "Hello" + ", " + "world!"; //"Hello, world!"

Solution: Repeat Strings

string[] words = Console.ReadLine().Split(); string result = ""; foreach (string word in words) { int repeatTimes = word.Length; for (int i = 0; i < repeatTimes; i++) result += word; } Console.WriteLine(result);

  • IndexOf() - returns the first match index or - 1
  • LastIndexOf() - finds the last occurrence

Searching (1)

string fruits = "banana, apple, kiwi, banana, apple"; Console.WriteLine(fruits.LastIndexOf("banana")); // Console.WriteLine(fruits.LastIndexOf("orange")); //- 1 string fruits = "banana, apple, kiwi, banana, apple"; Console.WriteLine(fruits.IndexOf("banana")); // Console.WriteLine(fruits.IndexOf("orange")); //- 1

  • Contains() - Check whether one string contains other string

Searching (2)

string text = "I love fruits.";

Console.WriteLine(text.Contains("fruits")); //True

Console.WriteLine(text.Contains("banana")); //False

  • You are given a text and a remove word
  • Remove all substrings that are equal to the remove word

Problem: Substring

ice kicegiceiceb kgb abc tcabctqw tctqw key keytextkey text word wordawordbwordc abc

▪ Split a string by given separator

Splitting (1)

string text = "Hello, [email protected], you have been using [email protected] in your registration"; string[] words = text.Split(", "); //words[]: //"Hello" //"[email protected]" //"you have been using [email protected] in your registration"

▪ Split can be used with multiple separators

Splitting (2)

char[] separators = new char[] { ' ', ',', '.' }; string text = "Hello, I am John."; string[] words = text.Split(separators); //"Hello", "", "I", "am", "John", ""

▪ Replace(match, replacement) – replaces all occurrences ▪ The result is a new string (strings are immutable)

Replacing

string text = "Hello, [email protected], you have been using [email protected] in your registration."; string replacedText = text .Replace("[email protected]", "[email protected]"); Console.WriteLine(replacedText); //Output: //Hello, [email protected], you have been using [email protected] in your registration.

  • You are given a text and a string of banned words
  • Replace all banned words in the text with asterisks

Problem: Text Filter

Linux, Windows It is not Linux, it is GNU/Linux. Linux is merely the kernel, while GNU adds the functionality...

It is not *****, it is GNU/*****. ***** is merely

the kernel, while GNU adds the functionality...