题解 | #截取字符串#

截取字符串

http://www.nowcoder.com/practice/a30bbc1a0aca4c27b86dd88868de4a4a

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        Scanner sr=new Scanner(System.in);
        while(sr.hasNext())
        {
            String str1=sr.nextLine();//字符串长度设为n,1=<n<=100
            //String str1=sr.nextInt();若使用这种输入语句,保存并提交编译会出现“数组越界”错误
            int k=Integer.parseInt(sr.nextLine());//1=<k<=n
            System.out.println(str1.substring(0,k));//截取str1中位置0~(k-1)的字符串
            //利用substring(int A,int B)方法,截取字符串中位置A到B-1的字符串
        }
        sr.close();
    }
}
全部评论

相关推荐

简历投递官网
投递中国农业银行等公司10个岗位 >
点赞 评论 收藏
分享
大猪蹄子哥:你猜我为啥笑不出来,因为我真在二号线上
点赞 评论 收藏
分享
12 5 评论
分享
牛客网
牛客企业服务