How do I get the last four characters from a string in C#?

前端 未结 20 1059
陌清茗
陌清茗 2020-11-30 18:13

Suppose I have a string:

\"34234234d124\"

I want to get the last four characters of this string which is \"d124\". I can use <

20条回答
  •  攒了一身酷
    2020-11-30 19:02

    You can use an extension method:

    public static class StringExtension
    {
        public static string GetLast(this string source, int tail_length)
        {
           if(tail_length >= source.Length)
              return source;
           return source.Substring(source.Length - tail_length);
        }
    }
    

    And then call:

    string mystring = "34234234d124";
    string res = mystring.GetLast(4);
    

提交回复
热议问题