Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Substring in C#

We can display or get the substring of a string like below:

Case 1: Mentioning only the starting position

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13.  
  14. /* Substring in Chsarp Example */
  15. string name = "Forget code";
  16. string subName = name.Substring(3);
  17.  
  18. Console.WriteLine(subName);
  19. }
  20.  
  21. }
  22. }


Output:
get code


Case 2: Mentioning the starting position and the number of characters to be displayed.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. /* Substring in Chsarp Example */
  14. string name = "Forget code";
  15. string subName = name.Substring(3,8);
  16.  
  17. Console.WriteLine(subName);
  18. }
  19.  
  20. }
  21. }


Output:
get code

..