[go: up one dir, main page]

0% found this document useful (0 votes)
18 views27 pages

1 String Regex

The document covers various aspects of Java GUI and file streams, including predefined libraries, the String class, regular expressions, and their applications. It details the functionalities of Java's standard libraries such as java.lang, java.util, and java.io, as well as methods for string manipulation and regex operations. Additionally, it explains the importance of regex in validating and processing strings, along with examples of its usage.

Uploaded by

samkumaran005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views27 pages

1 String Regex

The document covers various aspects of Java GUI and file streams, including predefined libraries, the String class, regular expressions, and their applications. It details the functionalities of Java's standard libraries such as java.lang, java.util, and java.io, as well as methods for string manipulation and regex operations. Additionally, it explains the importance of regex in validating and processing strings, along with examples of its usage.

Uploaded by

samkumaran005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

UNIT II – JAVA GUI AND FILE

STREAMS
 Predefined Libraries
 Using String class
 Working with Data & Time
 Utility framework
 Java I/O
 AWT & Swings
 Regular Expressions
 Files, Streams and Object Serialization
 Generic collections – Generic Classes and Methods
 Java Applet Basics- Graphics and Animation in Applet- Event
Handling and Applet Communication
 Reflections in Java

BVL_DR.KALAM COMPUTING CENTRE, MIT


20-Aug-25 1
CAMPUS ANNA UNIVERSITY
Predefined Libraries
(Java Standard Library / Java API)
• Collection of pre-built classes and interfaces
provided by the JDK.
• Offer ready-to-use functionalities for common
programming tasks
• Prevent developers from having to write code
from scratch for basic operations.
• Examples of predefined libraries:
– java.lang, java.util, java.io, java.net, java.sql,
java.awt and javax.swing

BVL_DR.KALAM COMPUTING CENTRE, MIT


20-Aug-25 2
CAMPUS ANNA UNIVERSITY
Predefined Libraries – Cont’d
• java.lang:
– Contains fundamental classes that are automatically
imported into every Java program.
– It includes core functionalities like primitive data type
wrappers (e.g., Integer, Double), String class for text
manipulation, Math class for mathematical operations,
and System class for system-level operations.
• java.util:
– provides utility classes and interfaces,
– includes data structures like ArrayList, HashMap, and
HashSet, as well as classes for date and time operations
(Date, Calendar), and random number generation
(Random)

BVL_DR.KALAM COMPUTING CENTRE, MIT


20-Aug-25 3
CAMPUS ANNA UNIVERSITY
Predefined Libraries – Cont’d
• java.io:
– Offers classes for input/output operations, enabling
interaction with files, streams, and other data
sources.
– Examples include FileInputStream, FileOutputStream,
BufferedReader, and PrintWriter.
• java.net:
– supports network programming
– provides classes for creating network applications,
handling URLs, and managing network connections
(e.g., Socket, ServerSocket, URL).
BVL_DR.KALAM COMPUTING CENTRE, MIT
20-Aug-25 4
CAMPUS ANNA UNIVERSITY
Predefined Libraries – Cont’d
• java.sql:
– Provides the Java Database Connectivity (JDBC) API
– Allows Java applications to interact with relational
databases.
– It includes classes for establishing connections, executing
SQL queries, and processing results.
• java.awt and javax.swing:
– These packages are used for creating graphical user
interfaces (GUIs).
– java.awt provides foundational GUI components
– javax.swing offers a more advanced and platform-
independent set of components.

BVL_DR.KALAM COMPUTING CENTRE, MIT


20-Aug-25 5
CAMPUS ANNA UNIVERSITY
Using String class
• String is a sequence of characters, for e.g. “Hello” is a string of
5 characters.
• In java, string is an immutable object which means it is
constant and can cannot be changed once it is created.
• Java only supports operator overloading for the String class.
– The + operator allows us to combine two strings.
– For instance, “hello"+“world"=“helloworld"
• Note: If there is a necessity to make a lot of modifications to
Strings of characters, then we should use String Buffer & String
Builder Classes
• There are two ways to create a String in Java
– String literal
– Using new keyword

BVL_DR.KALAM COMPUTING CENTRE, MIT


20-Aug-25 6
CAMPUS ANNA UNIVERSITY
Using String class – Cont’d
• String literal:
– It is a sequence of characters enclosed
in double quotation marks (” “).
– In java, Strings can be created by
assigning a String literal to a String
instance
String str1 = "Book";
String str2 = "Book";
• When double quotes are used to generate a String, JVM searches the String
Constant pool(A location inside the heap memory) whether a string is already
present in the string pool before generating a new object in the pool.
• If an existing String object is found, it simply returns the reference to it; otherwise,
a new String object with the specified value is created and stored in the String
pool.

BVL_DR.KALAM COMPUTING CENTRE, MIT


20-Aug-25 7
CAMPUS ANNA UNIVERSITY
Using String class – Cont’d
• Using New Keyword:
– To create a new instance of a
string, we use new keyword.
– When we create a string using new
keyword, it gets created in heap
memory rather than string
constant pool.
– When we create a string using new
keyword, it always create a new
string irrespective of whether the
string is already present or not in
the heap memory.
String str3 = new String("Book");
String str4 = new String("Book");
BVL_DR.KALAM COMPUTING CENTRE, MIT
20-Aug-25 8
CAMPUS ANNA UNIVERSITY
Comparing Strings:
== Operator checks if two
references point to the same
memory location.
equals() Method checks if
two strings have the same
content, regardless of their
memory locations.

Output:

BVL_DR.KALAM COMPUTING CENTRE, MIT


20-Aug-25 9
CAMPUS ANNA UNIVERSITY
BVL_DR.KALAM COMPUTING CENTRE, MIT
20-Aug-25 10
CAMPUS ANNA UNIVERSITY
Output:

BVL_DR.KALAM COMPUTING CENTRE, MIT


20-Aug-25 11
CAMPUS ANNA UNIVERSITY
Using String class – Cont’d
(String Methods)
• int length()- returns the length of the String that is, the
number of characters in a String.
• char charAt(int index) - return a character value at a
specified position (index) of a String.
• int indexOf(int ch) - The requested char value index is
returned.
• String concat(String string1) –
– used for concatenating or adding two strings into a single
string.
– This method appends its String argument to the indicated
string argument and returns a new instance of the String class.
– We can also use the + operator to produce the same result as
the concat method
BVL_DR.KALAM COMPUTING CENTRE, MIT
20-Aug-25 12
CAMPUS ANNA UNIVERSITY
Using String class – Cont’d
(String Methods)
• String substring(int beginIndex) - returns the substring from
the index which is passed as an argument to the method.
• String substring(int beginIndex, int endIndex) - returns the
new String by using the required index range (including the
start index and excluding the end index).
• int compareTo(String string1, String string2)
– Compares values lexicographically and returns an integer value
that describes if the first string is less than, equal to, or greater
than the second string.
– Suppose s1 and s2 are two string variables. If:
s1 == s2 : 0
s1 > s2 : positive value
s1 < s2 : negative value

BVL_DR.KALAM COMPUTING CENTRE, MIT


20-Aug-25 13
CAMPUS ANNA UNIVERSITY
Using String class – Cont’d
(String Methods)
• String toUpperCase() -converts all the characters of a
given string to uppercase.
• String toLowerCase() - converts all the characters of a
given string to lowercase.
• String trim() - returns the new String after removing
whitespaces at both ends. It does not affect whitespaces
in the middle of the String.
• String replace(char oldChar, char newChar) –
– replaces each occurrence of the first argument of the string
with the second argument and returns the resulting string.
• static String valueOf(int value) - returns a string
representation of the supplied int value.

BVL_DR.KALAM COMPUTING CENTRE, MIT


20-Aug-25 14
CAMPUS ANNA UNIVERSITY
BVL_DR.KALAM COMPUTING CENTRE, MIT
20-Aug-25 15
CAMPUS ANNA UNIVERSITY
Regular Expressions
(regex)
• A powerful tool in Java for working with strings.
• regex serves as a search pattern, helps us to find, replace, or modify
strings in a structured way.
• For example,
• if we use the regular expression ab*, we are issuing an instruction to
match a string that has an ‘a’ followed by zero or more b’s.
• So strings like ab, abc, abbc, etc. will match our regular expression.
• Regular expressions make finding patterns in text much easier.
• Some high-level use cases include,
– Email validation
– Phone number validation
– Credit card number validation
– Password pattern matching
– String replacement

BVL_DR.KALAM COMPUTING CENTRE, MIT


20-Aug-25 16
CAMPUS ANNA UNIVERSITY
Regular Expressions
(regex)
• The java.util.regex API is the backbone for handling
all regex operations
• java.util.regex package that contains three classes
– Pattern - used to define a pattern (the regex itself)
– Matcher - used to search for the pattern within a string.
– PatternSyntaxException - is thrown if the regular
expression syntax is not correct.

BVL_DR.KALAM COMPUTING CENTRE, MIT


20-Aug-25 17
CAMPUS ANNA UNIVERSITY
Regular Expressions – Cont’d

BVL_DR.KALAM COMPUTING CENTRE, MIT


20-Aug-25 18
CAMPUS ANNA UNIVERSITY
BVL_DR.KALAM COMPUTING CENTRE, MIT
20-Aug-25 19
CAMPUS ANNA UNIVERSITY
Regular Expressions – Cont’d
Common matching symbols:
Regular Description
Expression
. Matches any character
^regex Finds regex that must match at the beginning of the line.
regex$ Finds regex that must match at the end of the line.
[abc] Set definition, can match the letter a or b or c.
[abc][vz] Set definition, can match a or b or c followed by either v or z.
[^abc] When a caret appears as the first character inside square brackets, it negates the
pattern. This pattern matches any character except a or b or c.
[a-d1-7] Ranges: matches a letter between a and d and figures from 1 to 7, but not d1.

X|Z Finds X or Z.
XZ Finds X directly followed by Z.
$ Checks if a line end follows.
BVL_DR.KALAM COMPUTING CENTRE, MIT
20-Aug-25 20
CAMPUS ANNA UNIVERSITY
Regular Expressions – Cont’d
Meta characters: Have a pre-defined meaning and make certain common patterns easier to use. For
example, we can use \d as simplified definition for [0..9].
Regular Description
Expression
\d Any digit, short for [0-9]
\D A non-digit, short for [^0-9]
\s A whitespace character, short for [ \t\n\x0b\r\f]
\S A non-whitespace character, short for .3K3R3R3U3=3U3X3R3J3KGIN3

OTJO\OJ[GRR_

\w A word character, short for [a-zA-Z_0-9]


\W A non-word character [^\w]
\S+ Several non-whitespace characters .KRRU3=UXRJ3GY3Z]U3YKVGXGZK3MXU[VY

\b Matches a word boundary where a word character is [a-zA-Z0-9_]


Note: These meta characters have the same first letter as their representation, e.g., digit,
space, word, and boundary. Uppercase symbols define the opposite.
BVL_DR.KALAM COMPUTING CENTRE, MIT
20-Aug-25 21
CAMPUS ANNA UNIVERSITY
Regular Expressions – Cont’d
Quantifier:defines how often an element can occur. The symbols ?, *, + and {} are
qualifiers.

Regular Description Examples


Expression

* Occurs zero or more times, is short for {0,} X* finds no or several letter X, <sbr
/> .* finds any character sequence

+ Occurs one or more times, is short for {1,} X+- Finds one or several letter X

? Occurs no or one times, ? is short for {0,1}. X? finds no or exactly one letter X

{X} Occurs X number of times, {} describes the order of the \d{3} searches for three digits, .{10} for
preceding liberal any character sequence of length 10.

{X,Y} Occurs between X and Y times, \d{1,4} means \d must occur at least
once and at a maximum of four.

*? ? after a quantifier makes it a reluctant quantifier. It tries


to find the smallest match. This makes the regular
expression stop at the first match.

BVL_DR.KALAM COMPUTING CENTRE, MIT


20-Aug-25 22
CAMPUS ANNA UNIVERSITY
Regular Expressions – Cont’d
• Grouping and Capturing
– Groups are created in regex by enclosing part of the regex in
parentheses ()
– For example, (\d\d) matches a two-digit number and captures it as a
group.
• Strings in Java have built-in support for regular expressions.
• Strings have four built-in methods for regular expressions: *
matches(), * split()), * replaceFirst() * replaceAll()
• The replace() method does NOT support regular expressions.
Method Description
s.matches("regex") Evaluates if "regex" matches s. Returns only true if the WHOLE string
can be matched.
s.split("regex") Creates an array with substrings of s divided at occurrence
of "regex". "regex" is not included in the result.
s.replaceFirst("regex"), "replacement" Replaces first occurance of "regex" with "replacement.
s.replaceAll("regex"), "replacement" BVL_DR.KALAM
Replaces COMPUTING CENTRE,
all occurrences MIT with "replacement.
of "regex"
20-Aug-25 23
CAMPUS ANNA UNIVERSITY
BVL_DR.KALAM COMPUTING CENTRE, MIT
20-Aug-25 24
CAMPUS ANNA UNIVERSITY
BVL_DR.KALAM COMPUTING CENTRE, MIT
20-Aug-25 25
CAMPUS ANNA UNIVERSITY
BVL_DR.KALAM COMPUTING CENTRE, MIT
20-Aug-25 26
CAMPUS ANNA UNIVERSITY
BVL_DR.KALAM COMPUTING CENTRE, MIT
20-Aug-25 27
CAMPUS ANNA UNIVERSITY

You might also like