How to find the first 10 characters of a string in C#?



To get the first 10 characters, use the substring() method.

Let’s say the following is our string −

string str = "Cricket is a religion in India!";

Now to get the first 10 characters, set the value 10 in the substring() method as shown below −

string res = str.Substring(0, 10);

Let us see the complete code.

Example

 Live Demo

using System; public class Demo {    public static void Main() {       string str = "Cricket is a religion in India!";       string res = str.Substring(0, 10);       Console.WriteLine(res);    } }

Output

Cricket is
Updated on: 2020-06-22T09:56:07+05:30

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements