Presentation is loading. Please wait.

Presentation is loading. Please wait.

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A string is a sequence of characters Strings are objects of the String.

Similar presentations


Presentation on theme: "Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A string is a sequence of characters Strings are objects of the String."— Presentation transcript:

1 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A string is a sequence of characters Strings are objects of the String class A string literal is a sequence of characters enclosed in double quotation marks: "Hello, World!" String length is the number of characters in the String Example: "Harry".length() is 5 Empty string: "" 4.5 The String Class

2 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Use the + operator: String name = "Dave"; String message = "Hello, " + name; // message is "Hello, Dave" If one of the arguments of the + operator is a string, the other is converted to a string String a = "Agent”; int n = 7; String bond = a + n; // bond is "Agent7" Concatenation

3 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Useful to reduce the number of System.out.print instructions: System.out.print("The total is "); System.out.println(total); versus System.out.println("The total is " + total); Concatenation in Print Statements

4 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Convert to number: int n = Integer.parseInt(strI); double x = Double.parseDouble(strD); Convert to string: String str = "" + n; str = Integer.toString(n); str = Double.toString(x); Also methods for long and float; Long.toString() and Float.toString(). Converting between Strings and Numbers

5 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. String greeting = "Hello, World!"; String sub = greeting.substring(0, 5); // sub is "Hello" Supply start and “past the end” position First position is at 0 Substrings

6 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. String sub2 = greeting.substring(7, 12); // sub2 is "World“ Substring length is “past the end” - start Substrings

7 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Assuming the String variable s holds the value "Agent", what is the effect of the assignment s = s + s.length() ? Answer: s is set to the string Agent5 Self Check 4.14

8 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Assuming the String variable river holds the value "Mississippi", what is the value of river.substring(1, 2) ? of river.substring(2, river.length() - 3) ? Answer: The strings "i" and "ssissi" Self Check 4.15

9 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading Exception Reports

10 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Escape Character \ Suppose you want to print Hello, “World”! System.out.println(“Hello, “World”!”); Use escape character \ (backslash), System.out.println(“Hello, \“World\”!”); To print a backslash use two \\ in a row, System.out.println(“Located in C:\\Temp\\Secret.txt”); Another escape character is \n, which denotes a newline.

11 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Escape Character \ Escape sequences are useful for including international characters in strings Java uses the Unicode encoding scheme to denote international characters. Uses a 2-byte code which can encode over 65,000 characters How to print which has an accented letter ( ) System.out.printlin(“All the way to San Jose\u00E9!”) Chinese script is not alphabetic but ideographic- a character represents an idea or thing

12 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. German Keyboard

13 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Thai Alphabet

14 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chinese Ideographs


Download ppt "Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A string is a sequence of characters Strings are objects of the String."

Similar presentations


Ads by Google