日期:2014-05-20 浏览次数:20744 次
import java.util.Scanner;
public class Insert_Test {
private static char[]text=new char[100];
public static void main(String[] args){
System.out.print("Please enter a string.");
Scanner input=new Scanner(System.in);
String s=input.nextLine();
text=s.toCharArray();
System.out.print("Please enter insert position.");
int n=input.nextInt();
System.out.print("Please enter insert text.");
String insert_text=input.nextLine();
System.out.print("Do you want inset(1 or 0)?");
int m=input.nextInt();
if (m==1){
insert(insert_text,n);
}
for(int i=0;i<text.length;i++){
System.out.print(text[i]);
}
}
public static void insert(String insert_text,int startposition){
int start_point=startposition;
char[] insert_part=insert_text.toCharArray();
char[] temp=new char[100];
for (int i=start_point-1,n=0;i<text.length;i++,n++){
temp[n]=text[i];
//System.out.print(temp[n]);
}
for (int i=start_point-1,n=0;n<insert_part.length;i++,n++){
text[i]=insert_part[n];
//System.out.print(text[i]);
}
for (int i=start_point+insert_part.length-1,n=0;n<temp.length;i++,n++){
text[i]=temp[n];
}
}
}
input.nextLine();
import java.util.Scanner;
public class Insert_Test {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
input.useDelimiter("\r\n");
System.out.print("Please enter a string.");
String s=input.nextLine();
char[] text=s.toCharArray();
System.out.print("Please enter insert position.");
int n=input.nextInt();
if(n>=s.length()){
System.out.println("插入位置大于等于原本string长度,将在原文最后添加");
n=s.length()+1;
}
input.nextLine();
System.out.print("Please enter insert text.");
String insert_text=input.nextLine();
System.out.print("Do you want inset(1 or 0)?");
int m=input.nextInt();
System.out.println("原来的是:"+s);
System.out.println("添加位置:"+n);
System.out.println("加入内容:"+insert_text);
if (m==1){
text=insert(text,insert_text,n);
}
System.out.print("结果为:");
for(int i=0;i<text.length;i++){
System.out.print(text[i]);
}
}
public static char[] insert(char[] text,String insert_text,int startposition){
int start_point=startposition;
char[] insert_part=insert_text.toCharArray();
char[] temp_text=new char[text.length+insert_part.length];
for(int i=0;i<start_point-1;i++){
temp_text[i]=text[i];
}
for(int i=0,n=start_point-1;i<insert_part.length;i++,n++){
temp_text[n]=insert_part[i];
}
for(int i=start_point-1+insert_part.length,n=start_point-1;n<text.length;i++,n++){
temp_text[i]=text[n];
}
return temp_text;
}
}