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

1 Answer

0 votes
using System;
 
class Program
{
    public static string get_last_n_characters(string s, int n) {
        if (n >= s.Length) { 
            return s; 
        }
        return s.Substring(s.Length - n);
    } 
    static void Main() {
        string s = "c++ c php c# java golang nodejs";
        int n = 6;
 
        string last_n_ch = get_last_n_characters(s, n);

        Console.Write(last_n_ch);
    }
}
 
 
 
/*
run:
 
nodejs
 
*/

 



answered Feb 24, 2020 by avibootz

Related questions

...