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

2 Answers

0 votes
using System;

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

        Console.Write(last_4_chars);
    }
}



/*
run:

ming

*/

 



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

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

        Console.Write(last_4_chars);
    }
}



/*
run:

ming

*/

 



answered Feb 23, 2021 by avibootz

Related questions

1 answer 149 views
149 views asked Mar 9, 2017 by avibootz
1 answer 102 views
1 answer 132 views
2 answers 188 views
1 answer 127 views
1 answer 86 views
...