どうも、ちょげ(@chogetarou)です。
文字列(string)の最初と最後の文字を1文字ずつ削除する方法を紹介します。
方法

文字列(string)の前後の文字を1文字ずつ削除するには、substring()を使います。
まず、文字列からsubstring()を呼び出します。
そして、substring()の第1引数に「1」、第2引数に文字列の文字数を「−1」した値を指定します。
//text=対象の文字列
String result = text.substring(1, text.length() - 1);
上記のsubstring()は、呼び出した文字列の前後の文字削除した文字列(string)を生成します。
使用例
public class Main {
public static void main(String[] args) throws Exception {
String text = "Hello,World";
String result = text.substring(1, text.length() - 1);
System.out.println(result);
}
}
出力:
ello,Worl
コメント