C# Sharp Exercises: Extract a substring from a given string
C# Sharp String: Exercise-13 with Solution
Write a program in C# Sharp to extract a substring from a given string without using the library function.
Sample Solution:-
C# Sharp Code:
using System;
public class exercise13
{
public static void Main()
{
string str;
char[] arr1;
int pos, l, ln, c = 0;
Console.Write("\n\nExtract a substring from a given string:\n");
Console.Write("--------------------------------------------\n");
Console.Write("Input the string : ");
str = Console.ReadLine();
ln=str.Length;
arr1 = str.ToCharArray(0, ln);
Console.Write("Input the position to start extraction :");
pos= Convert.ToInt32(Console.ReadLine());
Console.Write("Input the length of substring :");
l= Convert.ToInt32(Console.ReadLine());
Console.Write("The substring retrieve from the string is : ");
while (c < l)
{
Console.Write(arr1[pos+c-1]);
c++;
}
Console.Write("\n\n");
}
}
Sample Output:
Extract a substring from a given string: -------------------------------------------- Input the string : Welcome to w3resource.com Input the position to start extraction :11 Input the length of substring :11 The substring retrieve from the string is : w3resource
Flowchart:
C# Sharp Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a program in C# Sharp to read a string through the keyboard and sort it using bubble sort.
Next: Write a C# Sharp program to check whether a given substring is present in the given string
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework