题解 | #截取字符串#
截取字符串
https://www.nowcoder.com/practice/a30bbc1a0aca4c27b86dd88868de4a4a
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
String str = in.next();
int k = in.nextInt();
System.out.println( str.substring(0,k));
}
}
}
思路: 直接使用String类中的方法 substring() 它是左闭右开区间 从而生成需要截取的字符串#算法##华为od机考#
