题解 | #截取字符串#
截取字符串
https://www.nowcoder.com/practice/a30bbc1a0aca4c27b86dd88868de4a4a
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { // 输入 Scanner sc = new Scanner(System.in); String s = sc.nextLine(); int length = sc.nextInt(); // 将字符串转成数组处理 char[] chars = s.toCharArray(); char[] result = new char[length]; // 用新数组替代 for (int i = 0; i < length; i++) { result[i] = chars[i]; } System.out.println(String.valueOf(result)); } }