java程序:String字符串处理算法
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
public class MyString{
public MyString(){
}
public int indexOf(String content,String find){
return indexOf(content,find,0);
}
public int indexOf(String content,String find,int beginIndex){ char[]ca_content=content.toCharArray();
int len_content=content.length();
int len_find=find.length();
for(int i=beginIndex;i char[]ca_temp=new char[len_find]; for(int j=0;j ca_temp[j]=ca_content[i+j]; } String temp=new String(ca_temp); if(temp.equals(find)){ return i; } } return -1; } public int lastIndexOf(String content,String find){ int start_to_pos=0; int position=-1; while(true){ int index1=indexOf(content,find,start_to_pos); if(index1!=-1){ start_to_pos=index1+1; position=index1; }else{ break; } } return position; } public String replaceFirst(String content,String find,String replace){ int index=indexOf(content,find); if(index==-1){ return content; } String part1=substring(content,0,index); String part2=substring(content,index + find.length(),content.length()); return part1+replace+part2; } public String replaceLast(String content,String find,String replace){ int index=lastIndexOf(content,find); if(index==-1){ return content; } String part1=substring(content,0,index); String part2=substring(content,index + find.length(),content.length()); return part1+replace+part2; } /* * equals replace(String content,String find,String replace) */ public String replaceAll(String content,String find,String replace){ String mycontent=content; if(find.equals(replace)){ return mycontent; } while(true){ int index1=indexOf(mycontent,find); if(index1!=-1){ mycontent=replaceFirst(mycontent,find,replace); }else{ break; } } return mycontent; } /* * equals replaceAll(String content,String find,String replace) */ public String replace(String content,String find,String replace){ return replaceAll(content,find,replace); } public String substring(String content,int beginIndex,int endIndex){ int len=endIndex - beginIndex; if(len==0){ return""; } char[]char_content=content.toCharArray(); char[]char_mycontent=new char[len]; for(int i=beginIndex,j=0;j char_mycontent[j]=char_content[i]; } String mycontent=new String(char_mycontent); return mycontent; } public String substring(String content,int beginIndex){ return substring(content,beginIndex,content.length()); } } /* 测试程序: public class TestMyString{ public static void main(String args[]){ new TestMyString(); } public TestMyString(){ MyString ms=new MyString(); System.out.println(ms.substring("hello",0)); System.out.println(ms.substring("hello",0,5)); System.out.println(ms.substring("hello",0,4)); System.out.println(ms.indexOf("hello","ll")); System.out.println(ms.indexOf("hello","l",2)); System.out.println(ms.indexOf("hello","l",3)); System.out.println(ms.indexOf("hello","l")); System.out.println(stIndexOf("hello","l")); System.out.println(ms.replace("hello","ll","**")); System.out.println(ms.replace("hello","ll",""));