String operation in C#
String operation in C#
net/publication/352837605
Strings in C#
CITATIONS READS
0 2,145
1 author:
Tarfa Hamed
University of Mosul
41 PUBLICATIONS 317 CITATIONS
SEE PROFILE
All content following this page was uploaded by Tarfa Hamed on 30 June 2021.
Advanced Programming in C#
Lecture Four
C# - Strings
You can create string object using one of the following methods −
1
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
Declaring strings in C#
string VariableName;
• where:
Example:
2
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
Greetings: Hello
3
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
The String class has numerous methods that help you in working with
the string objects.
The following table provides some of the most commonly used
methods:
No. Methods & Description
1. public static int Compare(string strA, string strB)
Compares two specified string objects and returns an integer that
indicates their relative position in the sort order. (0 if equal)
2. public static string Concat(string str0, string str1)
Concatenates two string objects.
4
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
5
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
Examples
The following example demonstrates some of the methods mentioned
above:
Comparing Strings
if (String.Compare(str1, str2) == 0)
{
Console.WriteLine(str1 + " and " + str2 + " are
equal.");
}
else
{
Console.WriteLine(str1 + " and " + str2 + " are
not equal.");
}
6
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
if (str.Contains("test"))
{
Console.WriteLine("The sequence 'test' was
found.");
}
Getting a Substring
7
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
String Concatenation
The + operator can be used between strings to combine them. This is
called concatenation:
string firstName = "John";
Console.WriteLine(name);
8
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
HW on Strings:
Write a full C# program for each of the following:
1. Write a method public static string replace(string
old, string s1, string s2). The method receives three
strings old, s1 and s2. The method replaces s1 with s2 in the
old string.
Example: Input: "programming", "ing", "er"
Output: "programmer"
Note: Do not use the method Replace() of the String class.
2. Write a method public static substring (string
origin, int start, int n). The method receives a string
and two integer variables. The method returns a substring from the
origin string starting from start of size n characters .
Example: Input: "Second semester of programming",
7,8
Output: "semester"
Note: Do not use the method Substring() of the String
class.
3. Write a method public static int Locationof(string
old, string st). The method receives two strings: old and st.
The method returns the first index of st in the old string
Example: Input: "Lecture Four", "Four"
Output: 8
9
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
Output: "AdvancedC#Programming"
Note: Do not use the method Replace() of the String class.
8. Write a method public static remove_string(string
old, string s1). The method receives two strings old and
s1, the method removes s1 from the old string and returns the
result.
Example: Input "programming","gra"
Output: "promming"
Note: Do not use EndsWith(), Contains(), and Remove()
methods of the String class.
9. Write a method public static string
classify_string(string origin, string numbers,
string letters, string special).
The method receives a string and returns three strings: numbers,
letters and special
Example: Input "fb48GF%7$#&@kp"
output: "fbGFkp" , "487", "%$#&@"
10. Write a method public static
capitalize_first(string old), the method receives a
string and capitalizes the first letter of each word.
Example: Input: "this is the fourth lecture"
Output: "This Is The Fourth Lecture"
11