- fas
- 第二题
package net.mindview.strings; import java.util.ArrayList; import java.util.List; /** * 无限循环 */ public class InfiniteRecursion { public InfiniteRecursion(){ } @Override String toString() { return " InfiniteRecursion address" + super.toString() + \n"; } static void main(String[] args) { List<InfiniteRecursion> list = new ArrayList<InfiniteRecursion>(); for(int i=0; i<5; i++){ list.add(new InfiniteRecursion()); } System.out.println(list); } }
- fa
- 第四题
package net.mindview.strings; import java.util.Formatter; Receipt { private double total = 0; private Formatter f = new Formatter(System.); public final int ITEM_WIDTH = 15int QTY_WIDTH = 5int PRICE_WIDTH = 10int PRICISION_FLOAT_WIDHT = 2int PRICISION_String_WIDHT = public final String TITLE_FORMAT = %-" + ITEM_WIDTH + s %" + QTY_WIDTH + " + PRICE_WIDTH + s\npublic final String PRICE_FORMAT = ." + PRICISION_String_WIDHT + " + QTY_WIDTH + d % PRICE_WIDTH + " + PRICISION_FLOAT_WIDHT + f\n; public final String TOTAL_FORMAT = ; //打印标题 printTitle(){ * 含义: 格式化字符串串以%开头 * -: 表示左对齐 * 15: 15表示宽度 * s:表示数据的类型是String * .2f:表示浮点数保留的小数位数 * .15s:表示字符串的长度最多15个字符 */ f.format(TITLE_FORMAT,Item",QtyPrice); f.format(TITLE_FORMAT,1)">------------); } 正文的内容 void print(String name,int qty,1)">double price){ f.format(PRICE_FORMAT,name,qty,price); total += price; } 总价 printTotal(){ f.format(TOTAL_FORMAT,1)">Tax"",total*0.06); f.format(TOTAL_FORMAT,1)">Total1.06 main(String[] args) { Receipt receipt = Receipt(); receipt.printTitle(); receipt.print(Jack`s Magic Beans4,1)">4.25); receipt.print(Princess Peas3,1)">5.1Three Bears Porridge1,1)">14.29); receipt.printTotal(); } }
运行结果:
Item Qty Price ---- --- ----- Jack`s Magic Be 4 Princess Peas 3 5.10 Three Bears Por 1 Tax 1.42 ----- Total 25.06
- 第五题
); receipt.printTotal(); } }
运行结果:
25.06
- afd
- 第七题:
package net.mindview.strings.test7; Test7 { main(String[] args) { 两种写法都可以 String regex = ^[A-Z].*\\.$; String regex1 = \\p{Upper}.*\\.$; String str = D.; String str1 = Dfasdfasfasfdasfdasfasfasdf.; String str2 = Dfasdfasfasfdasfdasfasfasdf.E; System..println(str.matches(regex)); System..println(str1.matches(regex)); System..println(str2.matches(regex)); System..println(str.matches(regex1)); System..println(str1.matches(regex1)); System..println(str2.matches(regex1)); } }
true false false
- 第八题
package net.mindview.strings; import java.util.Arrays; Splitting { static String knights = Then,when you have found the shrubbery,you must cut down the mightiest tree in the forest... with... a herring!; split(String regex){ System..println(Arrays.toString(knights.split(regex))); } 表示的时按照空格分割字符串 运行结果:[Then,when,you,have,found,the,shrubbery,must,cut,down,mightiest,tree,in,forest...,with...,a,herring!] split(" ); 表示按照非单次字符分割字符串--这里的非单次字符是空格和,\\W+); 这个表示:费单次字符之前带n的地方进行分割字符串 这里的分割符是n空格和n,1)">运行结果:[The,whe,you have found the shrubbery,you must cut dow,the mightiest tree i,the forest... with... a herring!] split(n\\W+); } }
package net.mindview.strings.test8; import net.mindview.strings.Splitting; Test8 { main(String[] args) { String regex = the|you; Splitting.split(regex); } }
- 第九题
package net.mindview.strings.test9; import net.mindview.strings.Splitting; Test9 { A|E|I|O|U|a|e|i|o|u; 通过嵌入式标志表达式 (?i) 也可以启用不区分大小写的匹配。 String regex1 = (?i)a|e|i|o|u[abc] 表示a或b或c String regex2 = (?i)[aeIoU]out.println(Splitting.knights.replaceAll(regex,1)">_)); System.out.println(Splitting.knights.replaceAll(regex1,1)">out.println(Splitting.knights.replaceAll(regex2,1)">)); } }
- 第十题
package net.mindview.strings.test10; import java.util.regex.Matcher; import java.util.regex.Pattern; Test10 { main(String[] args) { String str = Java now has regular expressions; for(String arg: args){ Pattern p = Pattern.compile(arg); Matcher m = p.matcher(str); while(m.find()){ System.out.println(Match \"" + m.group() +\" at positions " + m.start() + -" + (m.end()-1) ); } System.); } } }
输入参数:
^Java \Breg.* n.w\s+h(a|i)s s? s* s+ s{4} s{}. s{0,1)">3}
输出结果
Match Java" at positions 0-3 Match now has5-11 Match "" at positions 0-- Match 1-2-3-4-6-7-68-79-810-9s11-12-13-1214-1315-1416-17-1618-1719-1820-1921-2022-2123-2224-2325-2426-2627-2728-29-2830-2931-3132-sss 31
- 第十一题
package net.mindview.strings.test11; import java.util.regex.Matcher; import java.util.regex.Pattern; Test11 { main(String[] args) { Pattern p = Pattern.compile((?i)((^[aeIoU])|(\\s+[aeIoU]))\\w+?[aeIoU]\\b); Matcher m = p.matcher(Arline ate eight apples and " + one orange while Anita hadn't any(m.find()){ System." + m.group() + (m.end() - )); } } }
运行结果:
Match Arline ate one30 orange37 Anita44-49
- sfa
- f
- a
- fda
- sdf
- af
- a
- fd
- af
- as
- fd
- asfd
- af
- da
- f
- df
- as
- fa
- sf
- asf
- a
- sf
- af
- asf