码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-06-18 15:58:49      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   http   

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 }

运行效果:

bubuko.com,布布扣

java替换字符串和用indexof查找字符,布布扣,bubuko.com

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

标签:style   class   blog   code   java   http   

原文地址:http://www.cnblogs.com/sweetculiji/p/3791223.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!