1. String padstring (string, int minlength, char padchar );
It is to use padchar to fill the string with at least minlength before the string.
For example ("7", 3 '0'), the result is "007 ";
For example ("2012", 3, '0'), the result is "2012 ";
Minlength may be negative.
public static void main(String[] args) {System.out.println(padString("7", 3, '0'));System.out.println(padString("2012", 3, '0'));}static String padString(String string,int minLength,char padChar){int len = string.length();if(minLength<0 || minLength<len){return string;}StringBuffer sb = new StringBuffer();int pads = minLength - len;while(pads-->0){sb.append(padChar);}sb.append(string);return sb.toString();}
2. Command Parsing
For commands
-Name Jack-age 20-address "Hangzhou zheda Road"
To change to [-name Jack,-age 20,-address "Hangzhou zheda Road"]
A command is composed of parameters and value pairs. A parameter starts with "-". Values are separated by spaces,
The value between double quotation marks "" is regarded as a whole.
. All parameters and values only include English letters, numbers, minus signs, and double quotation marks
Public static void main (string [] ARGs) {system. out. println (Parser ("-name Jack-age 20-address \" Hangzhou zheda road \ ""); system. out. println (Parser ("-name Jack-age 20-address \" Hangzhou zheda road \ ""); system. out. println (Parser ("-name Jack-age 20-address \" Hangzhou zheda road \ ""); system. out. println (Parser ("-name Jack-age 20-address \" Hangzhou zheda road \ "");} static string Parser (string s Tr) {string Params [] = Str. split ("-"); For (INT I = 0; I <Params. length; I ++) {Params [I] = Params [I]. trim (); // remove the leading and trailing spaces. Params [I] = Params [I]. replaceall ("\ s +", ""); // remove the intermediate space} stringbuffer sb = new stringbuffer (); sb. append ("["); For (INT I = 0; I <Params. length; I ++) {If (Params [I]. length ()! = 0 &&! Params [I]. Equals ("") {sb. append ("-"); sb. append (Params [I]); if (I! = Params. Length-1) {sb. append (",") ;}} sb. append ("]"); return sb. tostring ();}
3, DIF (string str1, string str2 );
Output different characters in two strings. If character a appears in str1 but not in str2, output-. on the contrary, the output is + A; the repeated substring in the string is not output. <Br>
ABCDE, bcde output-A <br>
Dabc, aabcef output + A,-D, + e, + F <br>
Abcdefe, aabcadef output + A, + A,-E;
Train of Thought: Map 26 letters into a 26-size integer array flag, initially all 0, a corresponds to flag [0], if it appears in str2, then add 1, if it appears in str1, It is reduced by 1,
Finally, traverse the array and print the number of occurrences.
public static void main(String[] args) {System.out.println(dif("abcde","bcde"));System.out.println(dif("dabc","aabcef"));System.out.println(dif("abcdefe","aabcadef"));}static String dif(String str1, String str2) {int flag[] = new int[26];StringBuffer sb = new StringBuffer();str1 = str1.toLowerCase().trim();str2 = str2.toLowerCase().trim();for(int i=0;i<str1.length();i++){flag[str1.charAt(i)-'a']--;}for(int i=0;i<str2.length();i++){flag[str2.charAt(i)-'a']++;}for(int i=0;i<flag.length;i++){//System.out.println("flag["+i+"]="+flag[i]);if(flag[i]<0){int count = Math.abs(flag[i]);for(int j=0;j<count;j++){sb.append("-");sb.append((char)(i+'a'));sb.append(",");}}else if(flag[i]>0){for(int j=0;j<flag[i];j++){sb.append("+");sb.append((char)(i+'a'));sb.append(",");}}}if(sb.lastIndexOf(",")==sb.length()-1)sb.deleteCharAt(sb.length()-1);return sb.toString();}
Go to CIDR 2015 school recruitment software development pen questions (Hangzhou Station)