java替换字符串和用indexof查找字符

java自带替换

String s="hlz_and_hourui哈哈";
String new_S=s.replaceAll("哈", "笑毛");
System.out.println(new_S);

则输出为:"hlz_and_hourui笑毛笑毛";

 

 1 package find_repalce_keywords;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.IOException;
 5 import java.io.InputStreamReader;
 6 
 7 public class find_repalce_keywords {
 8 
 9     public static void main(String[] args) {
10         
11         String s="hlz_and_hourui哈哈";
12         //java自带替换
13         String new_S=s.replaceAll("哈", "笑毛");
14         System.out.println(new_S);
15         String input = null;
16         String finding=null;
17         System.out.println("enter a string:");
18         System.out.flush();
19         
20         try {
21             input=getString();
22         } catch (IOException e) {
23             e.printStackTrace();
24         }
25         System.out.println("输入你想查找的词");
26         try {
27             finding=getString();
28         } catch (IOException e) {
29             e.printStackTrace();
30         }
31         FindAndRepace far=new FindAndRepace();
32             far.repalce(input, finding);
33             
34 
35     }
36 
37     //I/O操作
38     public static String getString() throws IOException {
39         InputStreamReader isr=new InputStreamReader(System.in);
40         BufferedReader br=new BufferedReader(isr);
41         String s=br.readLine();
42         return s;
43     }
44 }
45 
46 class FindAndRepace
47 {
48     int i=0;
49     public int repalce(String str,String finding)
50     {
51         if(str.indexOf(finding, i)!=-1)
52         {
53             
54             System.out.print("要找的"+finding+"在"+(str.indexOf(finding, i)+1)+"||||");
55             i=str.indexOf(finding, i)+1;
56             repalce(str, finding);//递归
57         }
58         else
59         {
60             System.out.println("搜索结束");
61         }
62         return i;
63     }
64 
65 
66 }

运行效果:

java替换字符串和用indexof查找字符,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。