題目描述
給定一個字串s,還有兩個整數start以及end。輸出一個字串s中範圍在start以及end - 1之間的子字串。
原題網址
輸入格式
第一行輸入只有英文字母的字串s,字元長度在1到100之間(包含1和100)。第二行輸入start以及end兩整數,用空格隔開,start的範圍在0到end之間(包含0,但不包含end),end的範圍在start到字串s的長度之間(不包含start,但包含字串s的長度)。
輸出格式
輸出指定範圍的子字串。
範例輸入
Helloworld
3 7
3 7
範例輸出
lowo
額外解釋
字串:Helloworld 索引:0123456789
範例中傳入的start為3,end為7,所以會使用索引3、4、5、6的字元來組成子字串「lowo」。
解題概念
直接使用字串物件提供的「substring」方法來取得子字串。
參考答案
import java.util.Scanner;
public class Solution {
public static void main(final String[] args) {
final Scanner in = new Scanner(System.in);
final String S = in.next();
final int start = in.nextInt();
final int end = in.nextInt();
System.out.println(S.substring(start, end));
}
}