You are on page 1of 23

Object-Oriented

Programming Using Java

Numbers and Strings


Contents
• Introduct to Numbers Classes
• Class Integer
• Strings and Manipulating Characters in a
String
• Comparing Strings
• Regular Expressions
References:
https://docs.oracle.com/javase/tutorial/java/d
ata/index.html

2
The Numbers Classes
• All of the numeric wrapper classes are
subclasses of the abstract class Number

3
Class Integer
public final class Integer
extends Number
implements Comparable<Integer>

•java.lang.Object
• java.lang.Number
• java.lang.Integer

4
Class Integer

5
Class Integer
Modifier and Type Method and Description
int intValue() Returns the value of this Integer as an int.
static int compare(int x, int y) Compares two int values numerically.
int compareTo(Integer anotherInteger) Compares
two Integer objects numerically.
boolean equals(Object obj) Compares this object to the specified
object.
static int max(int a, int b) Returns the greater of two int values as if by
calling Math.max.
static int min(int a, int b) Returns the smaller of two int values as if by
calling Math.min.
static int parseInt(String s) Parses the string argument as a signed
decimal integer.
static String toBinaryString(int i)Returns a string representation of the
integer argument as an unsigned integer in base 2.

6
When working with
Numbers?

7
Formatting Numeric Print
Output

8
Strings
• Strings are a sequence of characters.
• In the Java, strings are objects.

9
Creating Format Strings

10
Manipulating Characters in
a String
String anotherPalindrome = "Niagara. O roar again!";
char aChar = anotherPalindrome.charAt(9);
String roar = anotherPalindrome.substring(11, 15);

11
Manipulating Characters in
a String

12
Other Methods
Returns a copy of this string with leading and
String trim()
trailing white space removed.
Returns a copy of this string converted to
String toLowerCase() lowercase or uppercase. If no conversions
String toUpperCase() are necessary, these methods return the
original string.
int indexOf(int ch) Returns the index of the first (last)
int lastIndexOf(int ch) occurrence of the specified character.
Returns true if the string contains the
boolean contains(CharSequence s)
specified character sequence.
Returns a new string resulting from replacing
String replace(char oldChar, char newChar) all occurrences of oldChar in this string with
newChar.
Replaces each substring of this string that
String replaceAll(String regex, String
matches the given regular expression with
replacement)
the given replacement.

13
Other Methods

14
Comparing Strings
Method Description
Returns an integer indicating whether
int compareTo(String anotherString) this string is greater than (result is > 0),
int compareToIgnoreCase(String str) equal to (result is = 0), or less than
(result is < 0) the argument.
Returns true if and only if the argument
boolean equals(Object anObject)
is a String object that represents the
boolean equalsIgnoreCase(String
same sequence of characters as this
anotherString)
object.
Returns true if this string ends with or
boolean endsWith(String suffix)
begins with the substring specified as
boolean startsWith(String prefix)
an argument to the method.
Tests whether this string matches the
boolean matches(String regex)
specified regular expression.

15
Comparing Strings
Java Heap
s1
Hello
s2 Greeting

s3

s4 String Pool

Hello

Hello

16
Regular expressions
Character Description
[abc] a, b, or c (simple class)
[^abc] Any character except a, b, or c (negation)
[a-zA-Z] a through z, or A through Z, inclusive (range)
[a-d[m-p]] a through d, or m through p: [a-dm-p] (union)
[a-z&&[def]] d, e, or f (intersection)
a through z, except for b and c: [ad-z]
[a-z&&[^bc]]
(subtraction)
a through z, and not m through p: [a-lq-z]
[a-z&&[^m-p]]
(subtraction)

17
Regular expressions
Predefined
Description
Character
Any character (may or may not match line
.
terminators)
\d A digit: [0-9]
\D A non-digit: [^0-9]
\s A whitespace character: [ \t\n\x0B\f\r]
\S A non-whitespace character: [^\s]
\w A word character: [a-zA-Z_0-9]
\W A non-word character: [^\w]

18
Regular expressions
Quantifiers Description
X? X, once or not at all: {0, 1}
X* X, zero or more times: {0, }
X+ X, one or more times: {1, }
X{n} X, exactly n times
X{n,} X, at least n times
X{n,m} X, at least n but not more than m times

19
Regex Example 1
• Kiểm tra mã sv bắt đầu bằng 2 ký tự a-z và
sau đó là 4 ký số 0-9
String regex="[a-zA-Z]{2}[0-9]{4}";
String code1 = "QL1234";
String code2 = "Q12345";
String code3 = "HT23456";
System.out.println(code1.matches(regex)); //true
System.out.println(code2.matches(regex)); //false
System.out.println(code3.matches(regex)); //false

20
Regex Example 2
• Kiểm tra họ tên là họ Nguyen, chỉ gồm các
ký tự a-z không phân biệt hoa thường
String regex="(?i)nguyen[a-zA-Z\\s]+";
String name = "Nguyen Thi Cat Hanh";
System.out.println(name.matches(regex)); //true

21
Regex Example 3
• Kiểm tra email
String regex=".+@.+.vn";//có chứa '@' và kết thúc .vn
String email1 = "ntt@hoasen.edu.vn";
String email2 = "ntt@hoasen.edu.jp";
String email3 = "ntthoasen.edu.vn";
String email4 = "@hoasen.edu.vn";
System.out.println(email1.matches(regex)); //true
System.out.println(email2.matches(regex)); //false
System.out.println(email3.matches(regex)); //false
System.out.println(email4.matches(regex)); //false

22
Q&A

23

You might also like