Part 30 string SubString()
substring() is a contiguous sequence of characters within a string. The String. Substring method retrieves a substring from a string instance.
Let’s , you want to get a substring of the first 15 characters from a string.
class Program
{
static void Main(string[] args)
{
// A long string
string bio = "csharp tutorial point author by chandan";
string author = bio.Substring(0, 15);
Console.WriteLine(author);
Console.ReadLine();
}
}
Output :
csharp tutorial
class Program
{
static void Main(string[] args)
{
// A long string
string bio = "csharp tutorial point author by chandan";
int startIndex = 7;
int endIndex = bio.Length - 7;
string title = bio.Substring(startIndex, endIndex);
Console.WriteLine(title);
Console.ReadLine();
}
}
Output:
tutorial point author by chandan
class Program
{
static void Main(string[] args)
{
// A long string
string bio = "csharp tutorial, point author by chandan";
string data = bio.Substring(0, bio.IndexOf(","));
Console.WriteLine(data);
Console.ReadLine();
}
}
Output
csharp tutorial