1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture Java methods: Object-oriented programming and data structures (2nd AP edition): Chapter 10 - Maria Litvin, Gary Litvin

31 44 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 31
Dung lượng 253,36 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Chapter 10 - Strings. In this chapter, the learning objectives are: Learn about literal strings, learn about string constructors and commonly used methods, understand immutability of strings, learn to format numbers into strings and extract numbers from strings, learn several useful methods of the Character class, learn about the StringBuffer class.

Trang 1

and Data Structures

Maria Litvin ● Gary Litvin

2nd AP edition with GridWorld

Trang 2

Objectives:

commonly used methods

extract numbers from strings

Character class

Trang 3

The String class

string of characters.

• The String class belongs to the java.lang

package, which is built into Java.

and methods.

Trang 4

Literal Strings

text in double quotes.

they are “just there.”

Trang 5

Literal Strings (cont’d)

as parameters.

String fileName = "fish.dat";

button = new JButton("Next slide");

if ("Start" equals(cmd))

Trang 6

Literal Strings (cont’d)

characters (described in Section 6.5) For example:

Trang 7

Immutability

none of its methods can change the string.

several references can point to the same

object safely: there is no danger of changing

an object through one reference without the others being aware of the change.

Trang 9

Immutability (cont’d)

create a new string and throw away the old one for every small change.

Trang 10

String s2 = new String();

private String errorMsg; errorMsg

is null

Empty strings

Trang 11

Constructors

• String ’s no-args and copy constructors are not used much.

(Chapter 12) into strings

String s1 = new String ();

String s2 = new String (s1);

String s1 = ""; String s2 = s1;

Trang 12

”Wind".charAt (2);

Returns:

Character positions in strings

are numbered starting from 0

Trang 13

String s2 = s.substring (i, j);

returns the substring of chars in

positions from i to j-1

String s2 = s.substring (i);

returns the substring from the i-th

char to the end

Trang 15

String date ="July 5, 2012 1:28:19 PM";

Trang 16

returns true if the string s1 matches s2, case-blind

int diff = s1.compareTo(s2);

returns the “difference” s1 - s2

int diff = s1.compareToIgnoreCase(s2);

returns the “difference” s1 - s2, case-blind

Trang 17

Methods — Replacements

String s2 = s1.trim ();

returns a new string formed from s1 by

removing white space at both ends

String s2 = s1.replace(oldCh, newCh);

returns a new string formed from s1 by replacing all occurrences of oldCh with newCh

String s2 = s1.toUpperCase();

String s2 = s1.toLowerCase();

returns a new string formed from s1 by

converting its characters to upper (lower) case

Trang 19

Integer and Double

are “wrapper” classes

from java.lang that

represent numbers as objects They also provide useful static methods.

Trang 20

Numbers to Strings (cont’d)

• The DecimalFormat class can be used for formatting numbers into strings.

Trang 21

Numbers to Strings (cont’d)

int m = 5, d = 19, y = 2007;

double amt = 123.5;

System.out.printf (

"Date: %02d/%02d/%d Amount = %7.2f\n", m, d, y, amt);

String s = String format(

"Date: %02d/%02d/%d Amount = %7.2f\n", m, d, y, amt);

Displays,

sets s to:

"Date: 05/19/2007 Amount 123.50"

Trang 22

Numbers from Strings

NumberFormatException if s does not

represent a valid number (integer, real

number, respectively).

String s1 = "-123", s2 = "123.45";

int n = Integer.parseInt(s1);

double x = Double.parseDouble(s2);

Trang 24

• Character also has methods that convert a letter to the upper or lower case.

Trang 26

int d = Character.digit (ch, radix);

returns the int value of the digit ch in the given

int radix

char ch = Character.forDigit (d, radix);

returns a char that represents int d in a given

int radix

Trang 27

The StringBuffer Class

mutable object

StringBuffer() // empty StringBuffer of the default capacity StringBuffer(n) // empty StringBuffer of a given capacity StringBuffer(str) // converts str into a StringBuffer

methods

• The toString method converts this

StringBuffer into a String

Trang 28

Review:

character into a literal string?

• Is "length".length() allowed syntax? If so,

what is the returned value?

efficient or less efficient?

Trang 29

Review (cont’d):

Trang 30

Review (cont’d):

few overloaded versions.

compareTo methods?

String object?

Trang 31

Review (cont’d):

a string.

String into an int ?

identify the category to which a given

character belongs.

StringBuffer classes?

Ngày đăng: 04/11/2020, 23:15

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN