How to get the first two characters of a string in C#

2 Answers

0 votes
using System;
 
class Program
{
    static void Main() {
        string s = "c# programming";
         
        string first_two_char = s.Substring(0, 2); 
 
        Console.Write(first_two_char);
    }
}
 
 
 
/*
run:
 
c#
 
*/

 



answered Feb 23, 2021 by avibootz
0 votes
using System;
 
class Program
{
    static void Main() {
        string s = "c# programming";
         
        string first_two_char = s.Length > 2 ? s.Substring(0, 2) : ""; 
 
        Console.Write(first_two_char);
    }
}
 
 
 
/*
run:
 
c#
 
*/

 



answered Feb 23, 2021 by avibootz

Related questions

1 answer 83 views
1 answer 157 views
1 answer 130 views
3 answers 256 views
1 answer 121 views
1 answer 165 views
...