How to get the first character from a string in C#

2 Answers

0 votes
using System;

class Program
{
    static void Main() {
        string s = "c# programming";

        char first_char = s[0];

        Console.Write(first_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 > 1 ? s.Substring(0, 1) : ""; 
 
        Console.Write(first_two_char);
    }
}
 
 
 
/*
run:
 
c
 
*/

 



answered Feb 23, 2021 by avibootz

Related questions

1 answer 150 views
2 answers 272 views
1 answer 113 views
1 answer 154 views
3 answers 286 views
1 answer 99 views
...