Java實驗項目三——遞歸實現字符串查找和替換操作


Program:按照下面要求實現字符串的操作:

    (1)設計一個提供下面字符串操作的類

      1)編寫一個方法,查找在一個字符串中指定字符串出現的次數。

      2)編寫一個方法,參數(母字符串,目標字符串,替換字符串)將母字符串中的所有目標字符用替換字符串替換。

      3)編寫一個方法,判斷一個email地址是否合法。

    (2)編寫一個測試類,對(1)中的字符串類的方法逐一進行測試。

 

Description:前兩個問題采用遞歸實現,最后一個問題,采用正則驗證。代碼如下:

 

 1 /*
 2  * Description:采用遞歸實現字符串操作類
 3  * 
 4  * */
 5 
 6 package tools;
 7 
 8 
 9 public class Operate {
10     
11     //遞歸查找字符串中指定字符出現的次數
12     public static int searchEleNum(String str,String targetEle) {    //參數為字符串和指定字符
13         
14         if( str.indexOf(targetEle) == -1 ) {
15             return 0;
16         }else {
17             //從當前找到位置的下一個位置下標開始,截取字符串,再進行遞歸
18             return 1 + searchEleNum( str.substring( str.indexOf( targetEle ) + 1 ),targetEle);
19         }
20     }
21     
22     
23     //遞歸替換,將母字符串的目標字符串,替換成指定字符串
24     public static String replaceAll(String parent,String targetEle,String replaceEle ) {
25         
26         //當目標元素不存在時,返回母字符串
27         if( parent.indexOf(targetEle) == -1 ) {
28             
29             return parent;
30         }else {                    //目標元素存在時,采用截取的方式進行遞歸
31             
32             //獲取目標元素開始下標
33             int beginIndex = parent.indexOf(targetEle);
34             //獲取目標元素結束位置的下一位置下標
35             int endIndex = beginIndex + targetEle.length();
36         
37             //采用遞歸的方法,截取目標元素在parent中的前面字符串 + 替換字符串 + 目標元素在parent中的后面字符串遞歸
38             //注意:substring()方法,當有兩個參數時,后者所表示下標元素取不到
39             return parent.substring(0,beginIndex) + replaceEle +
40                     replaceAll(parent.substring(endIndex), targetEle, replaceEle);
41         }
42         
43     }
44     
45     //判斷email地址是否合法
46     public static boolean ifEmeil(String email) {
47         
48         //字符串不為空
49         if( email != null && !"".equals(email) ) {
50             
51             //采用正則驗證郵箱地址合法性
52             if( email.matches( "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$" ) ) {    
53                 
54                 return true;
55             }else {
56                 
57                 return false;
58             }
59         }
60         
61         return false;
62     }
63     
64 }

 

 

 

 1 /*
 2  * Description:字符串操作
 3  * 
 4  * Written By:Cai
 5  * 
 6  * Date Written:2017-09-25
 7  * 
 8  * */
 9 
10 package main;
11 
12 import tools.Operate;
13 
14 
15 public class DemoThree4 {
16     
17     public static void main(String args[]) {
18         
19         String str1 = "hello world";        //聲明並初始化一個字符串變量
20         
21         String email = "1234567789@qq.com";    //設置QQ郵箱地址,驗證email合法性
22         
23         //測試查找字符串出現次數的方法
24         System.out.println( str1 + "中,字符‘l’出現的次數為:" + Operate.searchEleNum(str1, "l") );
25         //測試替換指定字符的方法
26         System.out.println( str1 + "中,替換所有字符‘l’為字符6:" + Operate.replaceAll(str1, "l", "6") );
27         
28         //驗證email地址合法性
29         System.out.println( Operate.ifEmeil(email) );
30         System.out.println( Operate.ifEmeil(str1));
31         
32     }
33     
34 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM