Sadegh Aliakbary
Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP is clearly noted as the source in the used case. JAVACUP shall not be liable for any errors in the content, or for any actions taken in reliance thereon. Please send your feedback to 2JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
Strings String Regular Expressions String Formatting Mutable Strings StringBuffer StringBuilder 3JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
Search in Strings indexOf() method 4JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
regionMatches() regionMatches(offset, str, strOffset, length) A substring of this String object is compared to a substring of the argument other String str1 = "Salam. Bye. See You Later"; String str2 = "Hi! Bye!"; //returns true: boolean b = str1.regionMatches(7, str2, 4, 3); 5JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
Regular Expressions (1) ^exprexpr at beginning of line. expr$expr at end of line..any single character (except newline) [xyz] either x, y, or z. [p-z] a range: any character from p to z. [p-z1-9] either any character from p to z or any digit from 1 to 9 [^p-z] ‘^’ as first character inside a bracket negates the pattern. it matches any character except characters p to z. Xy Matches X followed by y. x | y either x or y. 6JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
Regular Expressions (2) \d digits (equivalent to [0–9]). \D non-digits \wword characters ([a-zA-Z_0-9]) \W non-word characters \s whitespaces (equivalent to [ \t\n\x0B\f\r]) \S non-whitespaces \A beginning of string \Z end of string 7JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
Regular Expressions (3) expr? 0 or 1 occurrence of expr (expr{0,1}) expr* 0 or more occurrences of expr (expr{0,}). expr+ 1 or more occurrences of expr (expr{1,}) expr{x} x occurrences of expr. expr{x, y} between x and y occurrences of expr. expr{x,} x or more occurrences of expr. 8JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
Pattern Examples National code boolean b = code.matches("\\d{10}"); expression (not perfect) String mail = String Regex = "[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*" + + "(\\.[A-Za-z]{2,})"; boolean b = mail.matches( Regex); 9JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
Pattern and Matcher Classes String str = "Ali's and "+ "Taghi's and "; //Note to parentheses: String regex = Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); while (matcher.find()) { System.out.print("FirstName="+matcher.group(1)); System.out.print(", LastName="+matcher.group(2)); System.out.print(", Domain="+matcher.group(3)); System.out.println(", Ext="+matcher.group(4)); } 10JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
Replace Patterns String str = and also " + // Note to parentheses: String regex = Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); String safe = matcher.replaceAll( "$1 DOT $2 [AT] $3 DOT $4"); Safe ali DOT alavi [AT] gmail DOT com and also taghi DOT taghavi [AT] chmail DOT ir 11JAVACUP.ir
Format Strings String formatted = String.format("%d %.2f", 5, 2.123); System.out.println(formatted); System.out.printf("%d %5.2f", 12, 2.123); %b Boolean %c Character %f Floating point numer in decimal format %s String %t Date/time … 12JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
What is wrong with this code: String = "ali"; +="."; +="alavi"; +="gmail"; +="."; +="com"; System.out.println( ); 13JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
Mutable Strings StringBuilder buffer = new StringBuilder("ali"); buffer.append(".").append("alavi"); buffer.append("gmail").append(".com"); System.out.println(buffer.toString()); StringBuffer: thread-safe StringBuilder: NOT thread-safe 14JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source15