package com.tarena.day11; public class StringDemo { public static void main(String[] args) { String s1 = "ABC";//字符串字面量,都是String实例 String s2 = s1; s1 += "DEF"; s1.concat("DEF"); System.out.println(s1);//ABCDEF System.out.println(s2);//ABC //字符串对象一旦创建,内容永远不变(不变模式) String s3 = s1.toLowerCase(); System.out.println(s3); System.out.println(s1); System.out.println(s3.toUpperCase()); String s5 = s3.concat("/t /n /r"); String s6 = s5.trim(); System.out.println(s6); String cardName = "黑桃10"; String suitName = cardName.substring(0,2); String ID ="211258198706163653"; String birthday = ID.substring(6,14); System.out.println(birthday); String email ="liucangsong@gmail.com"; String name = email.substring(0,email.indexOf('@')); int index = email.lastIndexOf("."); String doman = email.substring(index +1);//cn String url = "http://www.tarena.com.cn/index.html"; index = url.indexOf("/",7); String page = url.substring(index+1); System.out.println(page); if(page.endsWith(".html")) { System.out.println("这是网页"); } if(page.startsWith("index")) { System.out.println("这是网站首页"); } char c = page.charAt(0); char[] chs = page.toCharArray(); } } package com.tarena.day11; public class StaticStringDemo { /** * @param args */ public static void main(String[] args) { String s1 = "abc"; String s2 = "abc"; String s3 = "abc"; String s4 ="a"+'b'+"c"; String s5 = new String("abc");//两个对象 String s6 = s1+""; int size = 1024*1024;//1M System.out.println(s1 ==s2); System.out.println(s2 ==s3); System.out.println(s3 ==s4); System.out.println(s4 ==s5); System.out.println(s4 ==s5); //字符串的常量的“静态池”,java会把相同的字符串字面量 //引用为字符串“静态池”中的相同字符串对象 //字面量的连接结果被(javac)优化为一个字符串字面量 //字符串字面量在编译时运行。 } } package com.tarena.day11; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexTestHarnessV5 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (true) { System.out.printf("%nEnter your regex: "); Pattern pattern = Pattern.compile(scanner.nextLine()); System.out.printf("Enter input string to search: "); Matcher matcher = pattern.matcher(scanner.nextLine()); boolean found = false; while (matcher.find()) { System.out.printf("Found /"%s/" starting index %d ending index %d.%n", matcher.group(), matcher.start(), matcher.end()); found = true; } if (!found) { System.out.printf("No match found.%n"); } } } }