Q:

How to get substring from a string in C#?

belongs to collection: C# Basic Programs | String programs

0

Given a string and we have to get the substring of N characters.

For Example: Input string is "India is great country" and we want to extract the substring (5 characters ) from 9th index, which will be "great".

String.Substring()

String.Substring() Method returns the given number of characters (length) from given starting position (index).

Syntax:

String String.Substring(int index, int length );

Here,
index – is the starting indexing from where you want to extract the substring (indexing starts from 0).
length – is the total number of characters to be extracted.
Return type String – method will return the length characters from index (substring), which will be the result.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Consider the program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            String str1;
            String str2;

            Console.Write("Enter string : ");
            str1 = Console.ReadLine();

            str2 = str1.Substring(9, 5);
            
            Console.WriteLine("Sub string is: " + str2);
        }
    }
  
}

Output

Enter string : India is great country.
Sub string is: great

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | String programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to trim the string in C#, an example of String... >>
<< How to convert string into lowercase in C#?...