0% found this document useful (0 votes)
8 views48 pages

3 - String and Regex

The document provides an overview of strings and regular expressions in Java, detailing the creation, immutability, and various methods associated with strings. It explains the differences between String, StringBuilder, and StringBuffer, highlighting their mutability and use cases. Additionally, it covers string comparisons, conversions, and practical examples, including a palindrome check and string/number casting.

Uploaded by

khanhtl2003gttn
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)
8 views48 pages

3 - String and Regex

The document provides an overview of strings and regular expressions in Java, detailing the creation, immutability, and various methods associated with strings. It explains the differences between String, StringBuilder, and StringBuffer, highlighting their mutability and use cases. Additionally, it covers string comparisons, conversions, and practical examples, including a palindrome check and string/number casting.

Uploaded by

khanhtl2003gttn
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/ 48

STRING AND REGEX

Instructor: DieuNT1

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use


Agenda
01. Creating Strings

02. String Immutable

03. String Methods

04. StringBuilder and StringBuffer

05. Regular Expression

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 2


Section 1

Creating Strings

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 3


String class

• Is a sequence of characters, for e.g. “Hello” is a string of 5


String
characters.

• Is an immutable object which means it is constant and can


In Java, string
cannot be changed once it has been created.

• There are two ways to create a String in Java


Creating a String • String literal
• Using new keyword

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 4


String class hierarchy
What is a Java String? In Java, a string is an object that represents a sequence of characters or
char values. The java.lang.String class is used to create a Java string object.

▪ The String class implements Serializable, Comparable and CharSequence interfaces.

▪ The CharSequence interface is used to represent the sequence of characters. String, StringBuffer and
StringBuilder classes implement it.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 5


The String class
▪ String class has 11 constructors, 40+ methods
▪ Very useful for programming and learning OOP

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 6


Constructing a String
▪ There are two ways to create a String object:
✔By string literal : Java String literal is created by using double quotes.

String s = "Welcome to Java";

✔By new keyword : Java String is created by using a keyword “new”. Can create String
from string literal or char[]
• String literal is characters in quotes. Java treats string literals as String objects

String message = new String("Welcome to Java");

• Create from char[]:


char[] chars = {'G', 'o', ... };
String s = new String(chars);

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 7


Java String pool
Java String pool refers to collection of Strings which are stored in heap memory that is used to store
string literals.

▪ String literals are created by enclosing a sequence of characters within double quotation marks,
such as "Hello, World!".
▪ Whenever a new object is created, String pool first checks whether the object is already present
in the pool or not:
✔ If it is present, then same reference is returned to the variable
✔ Else new object will be created in the String pool and the respective reference will be returned.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 8


Java String pool
▪ For example, if you have multiple variables initialized with the same string literal, they will all refer to
the same string object in the string pool:
String str1 = "Hello";
String str2 = "Hello";
System.out.println(str1 == str2); // Output: true

▪ If you create a string object using the new keyword, it will not be interned and will not be part of the
string pool:
String str3 = new String("Hello");
String str4 = new String("Hello");
System.out.println(str3 == str4); // Output: false

▪ You can explicitly intern a string object using the intern() method.
String str5 = new String("Hello").intern();
String str6 = new String("Hello").intern();
System.out.println(str5 == str6); // Output: true

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 9


Section 2

String Immutable

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 10


Immutable String and Interned String
▪ String objects are immutable:
• String s = "Java"; //creates a String object
• s = "HTML"; // creates a new String

▪ Original "Java" object still exists but is inaccessible since variable


‘s’ now points to the new object

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 11


Immutable String and Interned String
▪ Since strings immutable and ubiquitous JVM uses unique instance for
same string literals (interning)
▪ Improves efficiency and saves memory:

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 12


String Comparison
▪ We can compare string in Java on the basis of content and reference.
▪ It is used in authentication (by equals() method), sorting (by compareTo()
method), reference matching (by == operator) etc.
▪ There are three ways to compare string in java:
✔In authentication: by equals()/equalsIgnoreCase() method

✔In sorting: by == operator

✔Reference matching: by compareTo() method. This method compares values


lexicographically (từ vựng) and returns an integer value that describes if first string is less than,
equal to or greater than second string.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 13


String Comparisons
▪ How do you compare the contents of two strings?
if (s1 == s2)
System.out.println("s1 and s2 are the same object");
else
System.out.println("s1 and s2 are different objects");

▪ The == operator checks if two strings are same object reference


▪ The == operator does not compare string contents
▪ Use equals() to compare contents

if (s1.equal(s2))
System.out.println("s1 and s2 are the same content");
else
System.out.println(“s1 and s2 are different content");

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 14


Immutable String
▪ Example:
public class TestImmutableString {

public static void main(String[] args) {


String s = "FPT Software";
s.concat(" Academy"); // concat() method appends the string at the end
System.out.println(s);// will print FPT Software because strings are
// immutable objects
}
}

▪ Output: Stack memory Heap space


FPT Software

Solution:
s = s.concat(" Academy");

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 15


Immutable String and Interned String
▪ Substring extraction with no modification:
String sentence = "The quick brown fox jumps over the lazy dog.";
String substring = sentence.substring(4, 10); // Creates a new string "brown fox"
System.out.println(sentence); // Still contains the complete sentence
System.out.println(substring); // Outputs "brown fox"

▪ Methods that return new strings:


✔Many string methods operate on the original string but return a new string with the transformed
content. Examples include toLowerCase(), toUpperCase(), trim(), etc.

String sentence = "The quick brown fox jumps over the lazy dog.";
String newSentence = sentence.toUpperCase();
System.out.println(sentence == newSentence); // Print 'false'

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 16


Section 3

String Methods

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 17


String class
▪ Methods:
✔char charAt(int index): It returns the character at the specified index. Specified index value should be
between 0 to length() -1 both inclusive. It throws IndexOutOfBoundsException if index<0||>= length of String.
✔boolean equals(Object obj): Compares the string with the specified string and returns true if both matches
else false.
✔int compareTo(String string): This method compares the two strings based on the Unicode value of each
character in the strings.
✔boolean startsWith(String prefix): It tests whether the string is having specified prefix, if yes then it returns
true else false.
✔boolean endsWith(String suffix): Checks whether the string ends with the specified suffix.
✔int hashCode(): It returns the hash code of the string.
✔int indexOf(int ch): Returns the index of first occurrence of the specified character ch in the string.
✔boolean contains(CharSequence s): It checks whether the string contains the specified sequence of char
values. If yes then it returns true else false. It throws NullPointerException of ‘s’ is null.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 18


String class
▪ Methods:
✔String concat(String str): Concatenates the specified string “str” at the end of the string.
✔String trim(): Returns the substring after omitting leading and trailing white spaces from the original string.

✔byte[] getBytes(): This method is similar to the above method it just uses the default charset encoding for
converting the string into sequence of bytes.

✔int length(): It returns the length of a String.

✔boolean matches(String regex): It checks whether the String is matching with the specified regular
expression regex.
✔static String valueOf(): This method returns a string representation of passed arguments such as int, long,
float, double, char and char array.
✔char[] toCharArray(): Converts the string to a character array.
✔String[] split(String regex): Same as split(String regex, int limit) method however it does not have any
threshold limit.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 19


compareTo() method
▪ compareTo compares Strings: s1.compareTo(s2)
✔0 if equal // Comparing strings lexicographically
String str1 = "apple";
✔< 0 if s1 < s2 String str2 = "banana";

✔> 0 if s1 > s2 // Compare str1 and str2


int result1 = str1.compareTo(str2);
System.out.println("Comparison of \"" + str1 + "\" and \""
+ str2 + "\": " + result1);
//Output: Comparison of "apple" and "banana": -1

▪ equalsIgnoreCase(), compareToIgnoreCase(): ignore case when comparing

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 20


String Length, Characters, and Combining Strings

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 21


Obtaining Substrings

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 22


Converting, Replacing, and Splitting Strings
▪ Example:String str = "Welcome";
String newStr = str.toLowerCase();
System.out.println(newStr);

newStr = str.toUpperCase();
System.out.println(newStr);

newStr = str.replaceFirst("e", "X");


//Output: WXlcome
System.out.println(newStr);

newStr = str.replace('e', 'X');


System.out.println(newStr); //Output:
WXlcomX
▪ split() extracts tokens from a string based on delimiters
String[] tokens =
"Java#HTML#Perl".split("#");
for (int i = 0; i < tokens.length; i++)
System.out.print(tokens[i] + " ");
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 23
Finding a Character or a Substring in a String
▪ indexOf() and lastIndexOf() find chars/substrings in a string

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 24


Conversion between Strings and Arrays
▪ Strings convert to/from char[] arrays:
✔String to char[]: char[] chars = s.toCharArray();
✔char[] to String:

String s = new String(charArray);


String s = String.valueOf(charArray);

▪ Example:
String str = new String(new char[]{'J', 'a', 'v', 'a'});
String str = String.valueOf(new char[]{'J', 'a', 'v', 'a'});

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 25


Formatting Strings
▪ String.format creates formatted strings:

String.format(format, args...)

▪ Example:
String s = String.format("%5.2f", 45.556);

▪ Like printf but returns string rather than printing

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 26


Finger Exercise
▪ A string is a palindrome if it reads the same forward and backward. The words
“mom,” “dad,” and “noon,” for instance, are all palindromes.
▪ Write a program that prompts the user to enter a string and reports whether the
string is a palindrome.

public static boolean isPalindrome(String s) {


// The index of the first character in the string
int low = 0;
// The index of the last character in the string
int high = s.length() - 1;
while (low < high) {
if (s.charAt(low) != s.charAt(high))
return false; // Not a palindrome
low++;
high--;
}
return true; // The string is a palindrome
}

Java Basic
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 27
String/Number casting
▪ Convert a digit sequence to number

// Each class in right hand side is called wrapper


// class of the corresponding primitive type
byte b = Byte.parseByte("128"); // NumberFormatException
short s = Short.parseShort("32767");
int x = Integer.parseInt("2");
int y = Integer.parseInt("2.5");// NumberFormatException
int z = Integer.parseInt("a"); // NumberFormatException
long l = Long.parseLong("15");
float f = Float.parseFloat("1.1");
double d = Double.parseDouble("2.5");

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 28


Section 4

StringBuilder and StringBuffer

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 29


StringBuilder and StringBuffer classes
▪ StringBuilder / StringBuffer alternate String classes
✔Allows modifying contents unlike immutable String
✔Can add, insert, append to StringBuilder/Buffer
✔StringBuffer is synchronized, thread-safe
✔StringBuilder faster for single thread use

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 30


String/StringBuilder/StringBuffer
▪ String is immutable, if you try to alter their values, another object gets created,
▪ StringBuffer and StringBuilder are mutable so they can change their values.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 31


Methods of StringBuffer class
Method Description
public synchronized StringBuffer append(String s) Is used to append the specified string with this string.
The append() method is overloaded like:
✔append(char),
✔append(boolean),
✔append(int),
✔append(float),
✔append(double) etc.

public synchronized StringBuffer insert(int offset, String s) Is used to insert the specified string with this string at the specified position.
The insert() method is overloaded like:
✔insert(int, char), insert(int, boolean),
✔insert(int, int), insert(int, float), insert(int, double) etc.

public synchronized StringBuffer replace(int startIndex, Is used to replace the string from specified startIndex and endIndex.
int endIndex, String str)

public synchronized StringBuffer delete(int startIndex, int endIndex) Is used to delete the string from specified startIndex and endIndex.

public synchronized StringBuffer reverse() Is used to reverse the string.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 32


Methods of StringBuffer class
Method Description

public int capacity() Is used to return the current capacity.

public void ensureCapacity(int minimumCapacity) Is used to ensure the capacity at least equal to the given minimum.

public char charAt(int index) Is used to return the character at the specified position.

public int length() Is used to return the length of the string i.e. total number of characters.

public String substring(int beginIndex) Is used to return the substring from the specified beginIndex.

public String substring(int beginIndex, int endIndex) Is used to return the substring from the specified beginIndex and
endIndex.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 33


StringBuilder class

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 34


Modifying Strings in the StringBuilder
▪ Can append, insert, delete, replace on StringBuilder
▪ Overloaded append methods for various types like boolean, char, double, String etc.

StringBuilder stringBuilder = new StringBuilder sb = new


StringBuilder(); StringBuilder("Hello World");
stringBuilder.append("Welcome"); int offset = 6;
stringBuilder.append(' '); int number = 123;
stringBuilder.append("to");
stringBuilder.append(' '); sb.insert(offset, number);
stringBuilder.append("Java");
//Output: Welcome to Java System.out.println(sb.toString());
stringBuilder.insert(11, "HTML and ");
//Output: Welcome to HTML and Java

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 35


Modifying Strings in the StringBuilder
▪ Delete characters from a string in the builder using the two delete methods
▪ Reverse the string using the reverse method
▪ Replace characters using the replace method,
▪ Set//stringBuilder
a new character= in"Welcome
a stringto Java";
using the setCharAt method.
//1. Changes the builder to Welcome Java.
stringBuilder.delete(8, 11); //2. Changes the builder
to Welcome o Java.
stringBuilder.deleteCharAt(8); //3. Changes the builder to avaJ
ot emocleW.
stringBuilder.reverse(); //4. Changes the builder to
Welcome to HTML
stringBuilder.replace(11, 15, "HTML");//5. Sets the builder to
welcome to Java.
stringBuilder.setCharAt(0, 'w');
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 36
StringBuilder and StringBuffer class
▪ The Java StringBuilder class is same as StringBuffer class except that it is non-synchronized.

No. StringBuffer StringBuilder


1) StringBuffer is synchronized i.e. thread safe. StringBuilder is non-synchronized i.e. not thread safe.
It means two threads can't call the methods of StringBuffer It means two threads can call the methods of StringBuilder
simultaneously. simultaneously.
2) StringBuffer is less efficient than StringBuilder. StringBuilder is more efficient than StringBuffer.

▪ Example:
public class ConcatTest { startTime = System.currentTimeMillis();
public static void main(String[] args) {
long startTime = System.currentTimeMillis(); StringBuilder sb2 = new StringBuilder("Java");
for (int i = 0; i < 1000000; i++) {
StringBuffer sb = new StringBuffer("Java"); sb2.append(" Learning");
for (int i = 0; i < 1000000; i++) { }
sb.append(" Learning");
} System.out.println("Time taken by StringBuilder: "
+ (System.currentTimeMillis() - startTime) + "ms");
System.out.println("Time taken by StringBuffer: " }
+ (System.currentTimeMillis() - startTime) + "ms"); }

❖ Output: Time taken by StringBuffer: 51ms


Time taken by StringBuilder: 26ms

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 37


Using StringTokenizer Class
▪ StringTokenizer can be used to parse a line into words
✔import java.util.*
✔some of its useful methods are shown in the text
• e.g. test if there are more tokens

✔you can specify delimiters (the character or characters that separate words)
• the default delimiters are "white space" (space, tab, and newline)

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 38


Example: StringTokenizer
▪ Display the words separated by any of the following characters: space, new line (\n), period
(.) or comma (,).

String inputLine = keyboard.nextLine();


StringTokenizer wordFinder = new StringTokenizer(inputLine, " \n.,");
//the second argument is a string of the 4delimiters
while(wordFinder.hasMoreTokens()){
System.out.println(wordFinder.nextToken());
}

Question
2b
Entering "Question,2b.or !tooBee." or
gives this output: !tooBee

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 39


Section 3

Regular Expression

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 40


Regular Expression
▪ Regular expression (regex) describes a pattern to match strings
▪ Can match, replace, split strings using a regex pattern
▪ Very useful and powerful

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 41


Matching, Replacing, Splitting by Patterns
▪ matches() method like equals(), e.g.:
"Java".matches("Java");
"Java".equals("Java");

▪ matches() can match patterns, not just fixed strings


✔"Java.*" is a regex describing the pattern: Begins with Java followed by any characters
"Java is fun".matches("Java.*");
"Java is cool".equals("Java.*");
"Java is
powerful".matches("Java.*");
▪ Matching a Date in "YYYY-MM-DD" Format
✔\\d matches a digit String date = "2023-11-29";
String regexDate = "^\\d{4}-\\d{2}-
\\d{2}$";
boolean isMatch =
date.matches(regexDate);

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 42


Matching, Replacing, Splitting by Patterns
▪ replaceAll(), replaceFirst(), split() methods can be used with regexes
✔Replace all digits with “X”:
String text = "The price is $19.99 and the quantity is 25.";
String replacedText = text.replaceAll("\\d", "X");
System.out.println(replacedText); //Output: The price is $XX.XX and the
quantity is XX.
✔Replaces $, +, # with NNN:

String s = "a+b$#c".replaceAll("[$+#]", "NNN");


System.out.println(s); //Output: aNNNbNNNNNNc

✔Replace the first word:


String sentence = "Java11 is a powerful programming language."; // Replace
the first word with "Python"
String replacedText2 = sentence.replaceFirst("\\b\\w+\\b", "Python");
System.out.println("Sentence after replacement: " + replacedText2);

\\b\\w+\\b matches a word boundary (\\b), one or more word characters (\\w+), and another word boundary.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 43


Matching, Replacing, Splitting by Patterns
▪ Can split on regex delimiters:
String[] tokens = "Java,C?C#,C++".split("[.,:;?]");
for (int i = 0; i < tokens.length; i++) {
System.out.println(tokens[i]);
}

✔[.,:;?] matches the punctuation marks


✔Splits into Java, C, C#, and C++

▪ Split a string
Stringon punctuation:
text2 = "Java is a powerful, versatile programming
language; it is used widely.";
// Split the text on punctuation
String[] segments = text2.split("\\p{Punct}+");
System.out.println(Arrays.toString(segments));
//Output: [Java is a powerful, versatile programming language,
it is used widely]
✔The regular expression \\p{Punct}+ matches one or more punctuation characters.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 44


References
▪ https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html
▪ https://docs.oracle.com/javase/tutorial/java/data/strings.html
▪ https://www.javatpoint.com/java-string

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 45


Summary
01. Creating Strings

02. String Immutable

03. String Methods

StringBuilder and StringBuffer


04.

05. Regular Expression

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 46


Questions

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 47


THANK YOU!

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use

You might also like