Download presentation
Presentation is loading. Please wait.
Published byLambert Newton Modified over 9 years ago
1
www.javacup.ir Sadegh Aliakbary
2
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 info@javacup.irinfo@javacup.ir 2JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
3
Strings String Regular Expressions String Formatting Mutable Strings StringBuffer StringBuilder 3JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
4
Search in Strings indexOf() method 4JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
5
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
6
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
7
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
8
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
9
Pattern Examples National code boolean b = code.matches("\\d{10}"); Email expression (not perfect) String mail = "sadegh.aliakbary@gmail.com"; String emailRegex = "[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*" + "@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*" + "(\\.[A-Za-z]{2,})"; boolean b = mail.matches(emailRegex); 9JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
10
Pattern and Matcher Classes String str = "Ali's email: ali.alavi@gmail.com and "+ "Taghi's email: taghi.taghavi@gmail.com and "; //Note to parentheses: String regex = "(\\w+)\\.(\\w+)@(\\w+)\\.(\\w{2,})"; 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
11
Replace Patterns String str = "ali.alavi@gmail.com and also " + "taghi.taghavi@chmail.ir"; // Note to parentheses: String regex = "(\\w+)\\.(\\w+)@(\\w+)\\.(\\w{2,})"; 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
12
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
13
What is wrong with this code: String email = "ali"; email+="."; email+="alavi"; email+="@"; email+="gmail"; email+="."; email+="com"; System.out.println(email); 13JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
14
Mutable Strings StringBuilder buffer = new StringBuilder("ali"); buffer.append(".").append("alavi"); buffer.append("@"); 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
15
JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source15
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.