Str Processing
Str Processing
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
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#";
s H e l l o , C #
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 in to support multiple languages and
alphabets
◦ System.String is of reference type
Immutable
◦ String objects are immutable: they cannot be changed after
they have been created.
◦ All of the String methods and C# operators that appear to
modify a string actually return the results in a new string
object.
string s1 = "Hello ";
string s2 = s1;
s1 += "World";
System.Console.WriteLine(s2);
//Output: Hello
The System.String Class
◦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'
index = 0 1 2 3 4 5
s[index] = H e l l o !
Declaring Strings
◦There are two ways of declaring string variables:
◦Using the C# keyword string
◦Using the .NET's fully qualified class name System.String
◦The following three declarations are equivalent
string str1;
System.String str2;
String str3;
Creating Strings
◦ Before initializing a string variable has null value
string s = Console.ReadLine();
◦ Case-sensitive
◦ Last occurrence
LastIndexOf(string)
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
index = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 …
s[index] = C # P r o g r a m m i n g …
Extracting Substrings
◦ str.Substring(int startIndex, int length)
string filename = @"C:\Pics\fall2020.jpg";
string name = filename.Substring(8, 8);
// name is fall2020
◦ str.Substring(int startIndex)
string filename = @"C:\Pics\fall2020.jpg";
string nameAndExtension = filename.Substring(8);
// nameAndExtension is fall2020.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 \ f a l l 2 0 2 0 . j p g
Splitting Strings
◦ To split a string by given separator(s) use the following method:
string[] Split(params char[])
◦ Example:
string listOfGames = “BattleField, Uncharted, COD, NFS”;
string[] games = listOfGames.Split(' ', ',' , '.');
Console.WriteLine("Available games are:");
foreach (string game in games)
{
Console.WriteLine(game);
}
Replacing and Deleting Substrings
◦ Replace(string, string) – replaces all occurrences of given string
with another
◦ The result is new string (strings are immutable)
string course = “MCA + MBA + MSC”;
string replaced = courses.Replace("+", "and");
// MCA and MBA and MSC
int number = 5;
string s = "The number is " + number.ToString();
Console.WriteLine(s); // The number is 5
Sample Programs
1. Write a program which
Takes an Input String
Appends a digit after every character with total count of that
character (case is ignored) in the string.
Example if input is Banana, the output must be B1a3n2a3n2a3
Sample Programs
1. Write a program which
Removes all characters from the second string which is there in
the first string
Enter String 1: programming
Enter String 2: computer
On removing characters from second we get : cute