Program: Implement the operation of the string as required below:
(1) Design a class that provides the following string manipulation
1) Write a method that finds the number of occurrences of the specified string in a string.
2) Write a method that replaces all target word trailing characters substitution strings in the parent string with parameters (female string, target string, replacement string).
3) Write a method to determine whether an email address is legitimate.
(2) write a test class and test each method of the string class in (1).
Description: The first two problems are recursive implementation, the last problem, the use of regular validation. The code is as follows:
1 /*2 * Description: Recursive implementation of string manipulation classes3 * 4 * */5 6 Packagetools;7 8 9 Public classOperate {Ten One //the number of occurrences of a specified character in a recursive lookup string A Public Static intSearchelenum (String str,string targetele) {//arguments are strings and specified characters - - if(Str.indexof (targetele) = =-1 ) { the return0; -}Else { - //starts at the next position of the currently found position, intercepts the string, and then recursively - return1 + searchelenum (str.substring (Str.indexof (targetele) + 1), Targetele); + } - } + A at //recursive substitution, replacing the target string of the parent string with the specified string - Public Staticstring ReplaceAll (String parent,string targetele,string replaceele) { - - //returns the parent string when the target element does not exist - if(Parent.indexof (targetele) = =-1 ) { - in returnparent; -}Else{//when the target element is present, recursion is performed in a truncated manner to + //get target element start subscript - intBeginindex =Parent.indexof (targetele); the //gets the next position subscript for the end position of the target element * intEndIndex = Beginindex +targetele.length (); $ Panax Notoginseng //Recursive method to intercept the target element in the parent in the preceding string + replace String + target element in the parent after the string recursion - //Note: The substring () method, when there are two parameters, the latter indicates that the subscript element does not take the returnParent.substring (0,beginindex) + Replaceele + + ReplaceAll (parent.substring (EndIndex), Targetele, Replaceele); A } the + } - $ //determine if an email address is legitimate $ Public Static BooleanIfemeil (String email) { - - //string is not empty the if(Email! =NULL&& "". Equals (email)) { - Wuyi //Verify email address legality with regular verification the 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}) (\\]?) $" ) ) { - Wu return true; -}Else { About $ return false; - } - } - A return false; + } the -}
1 /*2 * Description: string manipulation3 * 4 * Written By:cai5 * 6 * Date written:2017-09-257 * 8 * */9 Ten Packagemain; One A Importtools. Operate; - - the Public classDemoThree4 { - - Public Static voidMain (String args[]) { - +String str1 = "Hello World";//Declaration and initialization of a string variable - +String email = "[email protected]";//set up QQ email address to verify email legality A at //to test how many occurrences of a string are found -System.out.println (str1 + ", the character ' L ' appears in the number of times:" + operate.searchelenum (str1, "L") ); - //test method for replacing a specified character -System.out.println (str1 + ", replace all characters ' l ' for character 6:" + Operate.replaceall (str1, "L", "6") ); - - //Verify email address legality in System.out.println (Operate.ifemeil (email)); - System.out.println (Operate.ifemeil (str1)); to + } - the}
Java Lab Project Three--recursive implementation of string lookup and substitution operations