题解 | #截取字符串#

截取字符串

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);
        // 输入的字符串
        String inputStr = in.nextLine();
        // 输入的整数
        int k = in.nextInt();
        StringBuilder strBuilder = new StringBuilder(inputStr);
        String res = strBuilder.substring(0,k);
        System.out.println(res);
    }
}

这个可以直接用 StringBuilder 类的 substring 方法来解决。

全部评论

相关推荐

点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-27 20:55
点赞 评论 收藏
分享
见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
流浪的神仙:无恶意,算法一般好像都得9硕才能干算法太卷啦
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务