How to get the last N characters from a string in C#

2 Answers

0 votes
using System;

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

        Console.Write(last_char);
    }
}



/*
run:

amming

*/

 



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

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

        Console.Write(last_char);
    }
}



/*
run:

amming

*/

 



answered Feb 23, 2021 by avibootz

Related questions

...