どうも、ちょげ(@chogetarou)です。
System.Linqを使って文字列(string)の末尾の1文字を取得する方法を紹介します。
方法

System.Linqを使って文字列(string)の最後の1文字を取得するには、LastOrDefault()を使います。
まず、System.Linqを導入します。
using System.Linq;
そして、文字列からLastOrDefault()を呼び出します。
char result = text.LastOrDefault();
上記のLastOrDefault()は、呼び出した文字列の末尾の1文字を取得します。
使用例
using System;
using System.Linq;
public class Sample{
public static void Main(){
string text = "Python";
string text2 = "Swift";
string text3 = "Kotlin";
char result = text.LastOrDefault();
char result2 = text2.LastOrDefault();
char result3 = text3.LastOrDefault();
System.Console.WriteLine(result);
System.Console.WriteLine(result2);
System.Console.WriteLine(result3);
}
}
出力:
n
t
n
コメント