如何在 C# 中获取字符串的最后 4 个字符?

csharpprogrammingserver side programming更新于 2025/5/25 5:07:17

首先,设置字符串 −

string str = "Football and Tennis";

现在,使用 substring() 方法获取最后 4 个字符 −

str.Substring(str.Length - 4);

让我们看看完整的代码 −

示例

using System;
public class Demo {
   public static void Main() {
      string str = "Football and Tennis";
      string res = str.Substring(str.Length - 4);
      Console.WriteLine(res);
   }
}

输出

nnis

相关文章