String operations are essential for Java siege attackers. An excellent siege attacker is lazy and will write some of his common code into a tool class or tool library that can be expanded and reused, these are the secrets of these excellent engineers.
Let's start with the basic String operation. Android has a TextUtils class. If you have nothing to do, click it.
1 public class StringUtils {2 3 4 private static final String regEx_script = "<script [^>] *?> [\ S \ S] *? <\\/ Script> "; // define the regular expression 5 private static final String regEx_style =" <style [^>] *?> [\ S \ S] *? <\\/ Style> "; // define the regular expression 6 private static final String regEx_html =" <[^>] +> "; // define the regular expression of the HTML Tag 7 private static final String regEx_space = "\ s * | \ t | \ r | \ n "; // define a space, press enter, line break, 8 9/** 10 * @ param htmlStr 11 * @ return 12 * delete Html Tag 13 */14 public static String delHTMLTag (String htmlStr) {15 Pattern p_script = Pattern. compile (regEx_script, Pattern. CASE_INSENSITIVE); 16 Matcher m_script = p_script.mat Cher (htmlStr); 17 htmlStr = m_script.replaceAll (""); // filter the script tag 18 19 Pattern p_style = Pattern. compile (regEx_style, Pattern. CASE_INSENSITIVE); 20 Matcher m_style = p_style.matcher (htmlStr); 21 htmlStr = m_style.replaceAll (""); // filter the style Tag 22 23 Pattern p_html = Pattern. compile (regEx_html, Pattern. CASE_INSENSITIVE); 24 Matcher m_html = p_html.matcher (htmlStr); 25 htmlStr = m_html.replaceAll ("" ); // Filter the html Tag 26 27 Pattern p_space = Pattern. compile (regEx_space, Pattern. CASE_INSENSITIVE); 28 Matcher m_space = p_space.matcher (htmlStr); 29 htmlStr = m_space.replaceAll (""); // filter spaces and press enter to tag 30 return htmlStr. trim (); // return text String 31} 32 33 34 public static String join (String [] strs, String split) {35 if (strs = null | strs. length = 0) {36 return ""; 37} 38 StringBuffer sb = new StringBuffer (); 39 sb. append (strs [0]); 40 for (int I = 1; I <strs. length; I ++) {41 if (split! = Null) {42 sb. append (split); 43} 44 45 sb. append (strs [I]); 46} 47 48 return sb. toString (); 49} 50 51 public static String validString (String str) {52 return TextUtils. isEmpty (str )? "": Str; 53} 54 55 public static String ToDBC (String input) {56 if (null = input) {57 return ""; 58} 59 60 char [] c = input. toCharArray (); 61 for (int I = 0; I <c. length; I ++) {62 if (c [I] = 12288) {63 c [I] = (char) 32; 64 continue; 65} 66 if (c [I]> 65280 & c [I] <65375) 67 c [I] = (char) (c [I]-65248 ); 68} 69 return new String (c); 70} 71 72 73/** 74 * Verify that the entered email format complies with 75*76 * @ para M email 77 * @ return is valid 78 */79 public static boolean isEmail (String email) {80 boolean tag = true; 81 final String pattern1 = "^ ([a-z0-9A-Z] + [-| \.]?) + [A-z0-9A-Z] @ ([a-z0-9A-Z] + (-[a-z0-9A-Z] + )? \\.) + [A-zA-Z] {2,} $ "; 82 final Pattern pattern = Pattern. compile (pattern1); 83 final Matcher mat = pattern. matcher (email); 84 if (! Mat. find () {85 tag = false; 86} 87 return tag; 88} 89/** 90 * str --> yyyy-MM-dd 91*92 * @ param strDate 93 * @ return 94 */95 @ SuppressLint ("SimpleDateFormat ") 96 public static Date toDate (String strDate) {97 Date date = null; 98 try {99 SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm "); 100 date = sdf. parse (strDate); 101} catch (ParseException e) {102 System. out. println (e. getMessage (); 103} 104 return date; 105 106} 107}