Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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.

C# Sharp Exercises: Extract a substring from a given string.

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:

Flowchart: Extract a substring from a given string

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.