Methodological Instructions
Theme: Use of strings
Aim: Use of procedures and functions for processing strings
Assessment criteria
Knowledge
· Know what is the substring
Understanding
· Describes how to search a word in sentence
· Describes how to work with Substring method of string
Application
· Write code for processing the strings using functions
· Write code for processing the strings using procedures
Language objectives
Student is able to describe how to look for the given word in a piece of text as well as manipulating Substring of an initial string.
For instance, one part of string, substring, could be replaced by Replace method of string.
Also, student can justify the purpose of function or procedure used for processing string.
Vocabulary and terminology specific to the subject:
string, string operators, comparing stings, substring, append, index , length of the substring
Useful expressions for dialogs and letters:
Well, to find a desired substring in the whole … .
For example, to get first n characters substring from a string… .
As a matter of fact, the stings are used in the wide variety of cases to … ..
Theory
In C# and .NET, a string is represented by the String class. The String.Substring method retrieves a substring from a string instance in C#. The method has the following two overloaded forms.
String characters are zero-indexed. Means, the position of the first characters in a string starts at 0th position.
Let’s say, you want to get a substring of first 12 characters from a string. We can use the Substring method and pass it starting index 0 and length of the substring 12 to get first 12 char substring from a string.
The following code snippet is an example of retrieving a 12 chars substring from a string.
1. // A long string
2. string bio = "Mahesh Chand is a founder of C# Corner. Mahesh is also an author, speaker, and software architect. Mahesh founded C# Corner in 2000.";
3.
4. // Get first 12 characters substring from a string
5. string authorName = bio.Substring(0, 12);
6. Console.WriteLine(authorName);
Now, if you want to retrieve a substring everything after 12th position. You can simply pass the starting position in the string to get rest of the string as a substring. The following code snippet gets a substring that has all characters after the 12th position in a string.
1. // Get everything else after 12th position
2. string authorBio = bio.Substring(12);
3. Console.WriteLine(authorBio);
The complete code example is listed in Listing 1.
1. using System;
2. class Program
3. {
4. static void Main(string[] args)
5. {
6. // A long string
7. string bio = "Mahesh Chand is a founder of C# Corner. Mahesh is also an author, " +
8. "speaker, and software architect. Mahesh founded C# Corner in 2000.";
9.
10. // Get first 12 characters substring from a string
11. string authorName = bio.Substring(0, 12);
12. Console.WriteLine(authorName);
13.
14. // Get everything else after 12th position
15. string authorBio = bio.Substring(12);
16. Console.WriteLine(authorBio);
17.
18. Console.ReadKey();
19. }
20. }
The first parameter of Substring method is starting index of the substring. The second parameter is the number of characters including whitespaces. You can use String.Length to find out the end index of a string.
Find the Frequency of the Word ʺisʺ in a given Sentence
static int CountStringOccurrences(string text, string pattern)
{
int count = 0;
int i = 0;
while ((i = text.IndexOf(pattern, i)) != -1)
{
i += pattern.Length;
count++;
}
return count;
}
static void Main(string[] args)
{
string s1;
int Count;
Console.WriteLine("Enter the String : ");
s1 = Console.ReadLine();
Count = CountStringOccurrences(s1, "is");
Console.WriteLine(Count);
Console.ReadLine();
}
П. Questions for self-assessment.
What is the string in C#?
How to declare string variable?
How to initialize string variable?
Visual Aids and Materials links.
1. Slides
2. https://www.c-sharpcorner.com/article/how-to-create-dynamic-strings-in-c-sharp/
3. https://www.c-sharpcorner.com/UploadFile/mahesh/substring-in-C-Sharp/
Students' Practical Activities:
Students must be able:
· Write code for processing the strings using functions
· Write code for processing the strings using procedures
Скачано с www.znanio.ru
Материалы на данной страницы взяты из открытых источников либо размещены пользователем в соответствии с договором-офертой сайта. Вы можете сообщить о нарушении.