「Java」substringメソッドで文字列の末尾から切り出すサンプル

環境
JavaSE1.8
Eclipse 2019

関数
public String substring(int beginIndex)
この文字列の部分文字列である文字列を返します。部分文字列は指定されたインデックスで始まり、この文字列の最後までになります。
パラメータ:beginIndex – 開始インデックス(この値を含む)。
戻り値:指定された部分文字列。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
public class StrendCutDemo {
public static void main(String[] args) {
String target = "Study Skill Become Smart";
// 文字列の長さ
int strLength = target.length();
System.out.println("文字列の長さ: " + strLength);
// 切り出したい文字列の長さ
int cutLength = 10;
//文字列の末尾から切り出します
String result = target.substring(strLength - cutLength);
System.out.println("結果: "+result);
}
}
package com.arkgame.study; public class StrendCutDemo { public static void main(String[] args) { String target = "Study Skill Become Smart"; // 文字列の長さ int strLength = target.length(); System.out.println("文字列の長さ: " + strLength); // 切り出したい文字列の長さ int cutLength = 10; //文字列の末尾から切り出します String result = target.substring(strLength - cutLength); System.out.println("結果: "+result); } }
package com.arkgame.study;

public class StrendCutDemo {

      public static void main(String[] args) {

            String target = "Study Skill Become Smart";
            // 文字列の長さ
            int strLength = target.length();
            System.out.println("文字列の長さ: " + strLength);
            // 切り出したい文字列の長さ
            int cutLength = 10;
            
            //文字列の末尾から切り出します
            String result = target.substring(strLength - cutLength);
            System.out.println("結果: "+result);
      }

}

実行結果
文字列の長さ: 24
結果: come Smart

Java

Posted by arkgame