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

1 answer 221 views
1 answer 175 views
2 answers 322 views
1 answer 199 views
1 answer 214 views
2 answers 315 views
1 answer 225 views
...