どうも、ちょげ(@chogetarou)です。
文字列(string)の先頭の文字を削除する方法を紹介します。
方法

文字列(string)の先頭の文字を削除する方法は、2つあります。
Substring()
1つは、Substring()を使う方法です。
まず、文字列からSubstring()を呼び出します。
そして、Substring()の引数に「1」を指定します。
string result = text.Substring(1);
上記のSubstring()は、呼び出した文字列の先頭の文字を削除した文字列を返します。
使用例
public class Sample{
public static void Main(){
string text = "Python";
string text2 = "Swift";
string text3 = "Kotlin";
string result = text.Substring(1);
string result2 = text2.Substring(1);
string result3 = text3.Substring(1);
System.Console.WriteLine(result);
System.Console.WriteLine(result2);
System.Console.WriteLine(result3);
}
}
出力:
ython
wift
otlin
Remove()
もう1つは、Remove()を使う方法です。
まず、文字列からRemove()を呼び出します。
そして、Remove()の第1引数に0、第2引数に1を指定します。
string result = text.Remove(0, 1);
上記のRemove()は、呼び出した文字列の先頭の文字を削除した文字列を返します。
使用例
public class Sample{
public static void Main(){
string text = "Python";
string text2 = "Swift";
string text3 = "Kotlin";
string result = text.Remove(0, 1);
string result2 = text2.Remove(0, 1);
string result3 = text3.Remove(0, 1);
System.Console.WriteLine(result);
System.Console.WriteLine(result2);
System.Console.WriteLine(result3);
}
}
出力:
ython
wift
otlin
まとめ
文字列(string)の先頭の文字を削除する方法は、次の2つです。
- Substring()を使う方法
string result = text.Substring(1);
- Remove()を使う方法
string result = text.Remove(0, 1);
コメント