如何在 C# 中獲取字串中的最後 4 個字元?


首先,設定字串 -

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

更新於: 2020 年 6 月 22 日

2K+ 瀏覽量

開啟你的職業 生涯

完成課程獲取認證

開始學習
廣告
© . All rights reserved.