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

3 Answers

0 votes
using System;

class Program
{
    static void Main() {
        string s = "c# programming";
        
        string last_char = s.Substring(s.Length - 1);

        Console.Write(last_char);
    }
}



/*
run:

g

*/

 



answered Feb 23, 2021 by avibootz
0 votes
using System;

class Program
{
    static void Main() {
        string s = "c# programming";
        
        string last_char = s.Substring(Math.Max(0, s.Length - 1)); 

        Console.Write(last_char);
    }
}



/*
run:

g

*/

 



answered Feb 23, 2021 by avibootz
0 votes
using System;
 
class Program
{
    static void Main() {
        string s = "c# programming";
         
        char last_char = s[s.Length - 1];
 
        Console.Write(last_char);
    }
}
 
 
 
/*
run:
 
g
 
*/

 



answered Feb 23, 2021 by avibootz

Related questions

1 answer 258 views
1 answer 203 views
1 answer 195 views
1 answer 249 views
1 answer 233 views
1 answer 171 views
1 answer 172 views
...