80
Strings, I/O, Formatting, and Parsing

Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Embed Size (px)

Citation preview

Page 1: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Strings, I/O,Formatting,and Parsing

Page 2: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

String, StringBuilder, and StringBuffer

Page 3: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

The String Class

• String are immutable objects• Create an instance of String with

the keyword "new“• Example:

– String s = new String();– String s = new String("abcdef");– String s = "abcdef";

Page 4: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

The String Class

Page 5: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

The String Class

Page 6: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

The String Class

Page 7: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

The String Class

• Another example:

Page 8: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer
Page 9: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

The String Class

• Output:

Page 10: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

The String Class

• And now?

Page 11: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer
Page 12: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

The String Class

• Example:

Page 13: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

The String Class

• Que imprime?:

Page 14: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

The String Class

• Imprime "spring winter spring summer".

Page 15: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Important facts about String and memory

• JVM reserve a special area called "Sreing constant pool”

• When the compiler encounters a string literal, it checks the pool to see if it exists. If found, the literal reference to the new addresses the existing chain (the existing chain has an additional reference.) [Immutable]

Page 16: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

new String

Page 17: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

String Class Methods

Page 18: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

String Methods

• public char charAt(int index)

String x = "airplane";System.out.println( x.charAt(2) );

// output ‘r’

Page 19: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

String Methods

• public String concat(String s)

String x = "taxi";System.out.println( x.concat("

cab") );

// output is "taxi cab"

Page 20: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

String Methods

• public boolean equalsIgnoreCase(String s)

String x = "Exit";System.out.println(x.equalsIgnoreCase("EXIT");

// is "true“

System.out.println(x.equalsIgnoreCase("tixe")); // is "false"

Page 21: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

String Methods

• public int length()

String x = "01234567";System.out.println( x.length() ); // returns "8"

Page 22: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

String Methods

• public String replace(char old, char new)

String x = "oxoxoxox";System.out.println( x.replace('x', 'X') ); // output is "oXoXoXoX"

Page 23: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

String Methods

• public String substring(int begin)• public String substring(int begin, int

end)

String x = "0123456789"; System.out.println( x.substring(5) ); // output is "56789"System.out.println( x.substring(5, 8)); // output is "567"

Page 24: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

String Methods

• public String toLowerCase()

String x = "A New Moon";System.out.println( x.toLowerCase()

);

// output is "a new moon"

Page 25: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

String Methods

• public String toUpperCase()

String x = "A New Moon";System.out.println( x.

toUpperCase() );

// output is “A NEW MOON"

Page 26: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

String Methods

• public String trim()

String x = " hi ";System.out.println( x + "x" ); // result is " hi x"System.out.println( x.trim() + "x"); // result is "hix"

Page 27: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

The StringBuffer and StringBuilder Classes

• Java.lang.StringBuilder and java.lang.StringBuffer classes should be used when making several modifications to the strings.

• Unlike String, objects of type StringBuffer and StringBuilder can be modified again and again without leaving behind discarded objects

• A common use of StringBuffer and StringBuilder is on file I / S

Page 28: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

StringBuffer vs. StringBuilder

• The StringBuilder class was added in Java 5.

• StringBuilder is Not thread safe. In other words, their methods are not synchronized (Chapter 9)

• Sun recommends that StringBuilder instead of StringBuffer whenever possible because StringBuilder will run faster.

Page 29: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Using StringBuffer and StringBuilder

• StringBuffer

• StringBuilder

Page 30: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Key methods of StringBuffer and

StringBuilder• public synchronized StringBuffer append(String s)

StringBuffer sb = new StringBuffer("set ");sb.append("point");System.out.println(sb); // output is "set point“

StringBuffer sb2 = new StringBuffer("pi = ");sb2.append(3.14159f);System.out.println(sb2); // output is "pi = 3.14159"

Page 31: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Key methods of StringBuffer and

StringBuilder• public StringBuilder delete(int

start, int end)

StringBuilder sb = new StringBuilder("0123456789");

System.out.println(sb.delete(4,6)); // output is "01236789"

Page 32: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Key methods of StringBuffer and

StringBuilder• public StringBuilder insert(int

offset, String s)

StringBuilder sb = new StringBuilder("01234567");sb.insert(4, "---");System.out.println( sb ); // output is "0123---4567"

Page 33: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Key methods of StringBuffer and

StringBuilder• public synchronized StringBuffer

reverse()StringBuffer s = new StringBuffer("A man a plan a canal

Panama");sb.reverse();System.out.println(sb); // output: "amanaP lanac a nalp a nam A"

Page 34: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Key methods of StringBuffer and

StringBuilder

• public String toString()

StringBuffer sb = new StringBuffer("test string");System.out.println( sb.toString() ); // output is "test string"

Page 35: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

File Navigation and I/O

• File .- The API says that the kind of file is "an abstract representation n of a file and directory path. " The File class is not used for reading or writing data, is used to work at high level, creating new files, search files, delete files, create directories, and working with paths (path).

Page 36: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

File Navigation and I/O

• FileReader .- This class is used to read character files. The method read() is very low, allowing you to read the entire flow (stream) character. FileReaders often surrounded by objects of more s high level, BufferedReaders, which improves performance and provides the means most convenient to work s with the data.

Page 37: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

File Navigation and I/O

• BufferedReader.- In comparison with FileReaders n, BufferedReaders read into relatively large data file at a time, and maintains these data in a buffer. When prompted for the next character or line of data is retrieved from memory (minimizes the time) and then perform read operations. In addition s, BufferedReader provides methods more suitable as readLine(), giving the next line of arhiva

Page 38: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

File Navigation and I/O

• FileWriter.- This class is used for writing character files. His method write() to write character (s) of strings or a file. FileWriter are usually surrounded by writers such as high-level objects or PrintWriters BufferedWriters, which provide better performance and higher, with more method flexible to write the data.

Page 39: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

File Navigation and I/O

• BufferedWriter.- Compared with FileWriters, writes BufferedWriters relatively large pieces of data from one file at a time, minimizes the number of times of write operations to a file. In addition, the BufferedWriter class provides the method NewLine () that facilitates the creation of line separators automatically.

Page 40: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

File Navigation and I/O

• PrintWriter.- This class has been improved significantly in Java 5. Because it has new methods and constructors (you can build a PrintWriter from a File or String). New methods like format (), printf () and append () PrintWriter make it very flexible and powerful.

Page 41: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Creating files using the File class

• File type objects are used to represent the current files (but not data files) or directories that exist on a physical computer disk.

Page 42: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Creating files using the File class File

• Solo se a creado el objeto File, no el archivo físico.

Page 43: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Creating files using the File class File

• The above code prints false, true, true• If you run a second time, print: true, false,

true• boolean exists() .- This method returns

true if it finds the specified file.• boolean createNewFile() .- This method

creates a file if it is not exists.• boolean isDirectory() .- This method

returns true if it finds the specified directory

Page 44: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Using FileWriter & FileReader

Page 45: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Using FileWriter & FileReader

• Output:

12 howdyfolks

Page 46: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer
Page 47: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Writer & Reader Hierarchy

Page 48: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Combining I/O

• Example 1 :

Page 49: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Combining I/O

• Example 2 :

Page 50: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Combining I/O

• Example 3 :

Page 51: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Combining I/O

• Example 4:

Page 52: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Combining I/O

• Example 4:

Page 53: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Combining I/O• Example 5:

Page 54: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Combining I/O• Example 6:

Page 55: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Serialization

• “Save this object and all its instance variables"

Page 56: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Working with ObjectOutputStream &

ObjectInputStream

• ObjectOutputStream.writeObject() • // serialize and write• ObjectInputStream.readObject() • // read and deserialize

Page 57: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Working with ObjectOutputStream &

ObjectInputStream

• Java.io.ObjectOutputStream and java.io.ObjectInputStream classes are high level in the java.io package, and as we learned earlier, that means surrounding the lower classes and java.io.FileInputStream java.io.FileOutputStream

Page 58: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

• Example 1:

Page 59: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

• Example 2:

Page 60: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Dates, Numbers, and Currency

Page 61: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Working with Dates, Numbers, and

Currencies• java.util.Date The majority of the

method of this kind are obsolete, but can be used as a bridge between the Calendar and DateFormat class. A Date instance can convert the date and time, to milliseconds.

• java.util.Calendar This class provides a variety of methods who help make and manipulate dates and times

Page 62: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Working with Dates, Numbers, and

Currencies• java.text.DateFormat This class

is used to format dates. "01/01/70" -> "January 1 1970”

• java.text.NumberFormat This class is used to format numbers and local currencies (Currencies) for everyone.

Page 63: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

The Date Class• Example 1

Page 64: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

The Date Class• Example 2

Page 65: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

The Calendar Class

Page 66: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

The DateFormat Class

Page 67: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

The Locale Class

Page 68: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

The Locale Class

Page 69: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

The Locale Class

Page 70: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

The NumberFormat Class

Page 71: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Summary

Page 72: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Parsing, Tokenizing and Formatting

• Simple search:

Page 73: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Parsing, Tokenizing and Formatting

• Searches using Metacharacters:• source: a12c3e456f• index: 0123456789• pattern: \d

-> regex will tell us that it found digits at positions 1, 2, 4, 6, 7, and 8.

• In case of:• source: “a 1 56 _Z”• index: 0123456789• pattern: \w

-> regex will tell us that it found digits at positions 0, 2, 4, 5, 7, and 8.

Page 74: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Parsing, Tokenizing and Formatting

• In case of:– source: "cafeBABE"– index: 01234567– pattern: [a-cA-C]

-> returns positions 0, 1, 4, 5, 6.

Page 75: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Parsing, Tokenizing and Formatting

• Searches using Quantifiers:• + : One or more occurrences• * : Zero or more occurrences• ? : Zero or one occurrences• ^ : For example, with pattern:

“proj1([^,])*” => Give me zero or more characters that aren't a comma

Page 76: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Parsing, Tokenizing and Formatting

• The Predefined Dot: "any character can serve here."– source: "ac abc a c"– pattern: a.c=> will produce the output

• 3 abc• 7 a c

Page 77: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Parsing, Tokenizing and Formatting

• Searching Using the Scanner Class:import java.util.*;class ScanIn { public static void main(String[] args) { System.out.print("input: "); System.out.flush(); try { Scanner s = new Scanner(System.in); String token; do { token = s.findInLine(args[0]); System.out.println("found " + token); } while (token != null); } catch (Exception e) { System.out.println("scan exc"); } }}

• The invocation and inputjava ScanIn "\d\d"input: 1b2c335f456

• produce the following:found 33found 45found null

Page 78: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Parsing, Tokenizing and Formatting

• Tokenizing with String.split()import java.util.*;class SplitTest { public static void main(String[] args) { String[] tokens = args[0].split(args[1]); System.out.println("count " + tokens.length); for(String s : tokens) System.out.println(">" + s + "<"); }}* java SplitTest "ab5 ccc 45 @" "\d"Output: count 4 >ab< > ccc < >< > @<

Page 79: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

Parsing, Tokenizing and Formatting

• Tokenizing with Scanner

Page 80: Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer

FIN