字符串中从第m个字符开始的全部字符复制成为另一个字符串。

eviljordan 2012-11-11 09:39:03
输入
数字n 一行字符串数字m

输出
从m开始的子串

样例输入
6
abcdef
3
样例输出
cdef

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner rd=new Scanner(System.in);
int n=rd.nextInt();
String str[]=new String[n];
for(int i=0;i<str.length;i++)
{
str[i]=rd.nextLine();
}
int m=rd.nextInt();
copy(str,m);
}
public static void copy(String[] str,int m)
{
String a[]=new String[(str.length-m)+1];
System.arraycopy(str, m, a, 0, a.length);
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]);
}

}

为什么我的没反应?
...全文
1908 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wnygqg 2012-11-11
  • 打赏
  • 举报
回复
都是高手!!!
nmyangym 2012-11-11
  • 打赏
  • 举报
回复
那简单,用String的方法 substring()就行。使用很简单。 按你的格式,改了一下。参考一下:

	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		Scanner rd=new Scanner(System.in);
		int n=rd.nextInt();
		rd.nextLine();              //去掉回车换行的影响.
		String str;
		do							//输入的字符串长度不等于n,重输入。
		{
			str=rd.next();
		}while(str.length()!=n);
		int m=rd.nextInt();
		copy(str,m);
	}
	public static void copy(String str,int m)
	{
		String temp=str.substring(m);				//关键就这句.
		System.out.println("new string is "+temp);
	}
eviljordan 2012-11-11
  • 打赏
  • 举报
回复
引用 4 楼 nmyangym 的回复:
我测试了啊! 6 I love java very much ! 2 java very much!
啊,这样啊,我知道了。 输入: 6 abcdef 3 要这样啊~是输入一个字符串 好像错了。。怎么搞
nmyangym 2012-11-11
  • 打赏
  • 举报
回复
我测试了啊! 6 I love java very much ! 2 java very much!
eviljordan 2012-11-11
  • 打赏
  • 举报
回复
引用 1 楼 nmyangym 的回复:
稍微改一下就可以了. Java code 123456789101112131415161718192021222324 public static void main(String[] args) { // TODO Auto-generated method stub Scanner rd=new ……
改好了还是输出不到东西?为什么会这样的?你调试一下看可以吗
chenhao237 2012-11-11
  • 打赏
  • 举报
回复
String st="sdgsdg"; 如果想从第2位开始,就是 st.subString(1); //第2位对应的索引是1
nmyangym 2012-11-11
  • 打赏
  • 举报
回复
稍微改一下就可以了.

	public static void main(String[] args)
       	{
        	// TODO Auto-generated method stub
		Scanner rd=new Scanner(System.in);
		int n=rd.nextInt();
		rd.nextLine();				//去掉回车换行的影响.
		String str[]=new String[n];
		for(int i=0;i<str.length;i++)
		{
			str[i]=rd.nextLine();
		}
		int m=rd.nextInt();
		copy(str,m);
	}
	public static void copy(String[] str,int m)
	{
		String a[]=new String[(str.length-m)];		//再加 1 就越界了。
		System.arraycopy(str, m, a, 0, a.length);
		for(int i=0;i<a.length;i++)
		{
			System.out.print(a[i]);
		} 
	}

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧