3 - String and Regex
3 - String and Regex
Instructor: DieuNT1
Creating Strings
▪ The CharSequence interface is used to represent the sequence of characters. String, StringBuffer and
StringBuilder classes implement it.
✔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 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.
▪ 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
String Immutable
if (s1.equal(s2))
System.out.println("s1 and s2 are the same content");
else
System.out.println(“s1 and s2 are different content");
Solution:
s = s.concat(" Academy");
String sentence = "The quick brown fox jumps over the lazy dog.";
String newSentence = sentence.toUpperCase();
System.out.println(sentence == newSentence); // Print 'false'
String Methods
✔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.
✔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.
newStr = str.toUpperCase();
System.out.println(newStr);
▪ Example:
String str = new String(new char[]{'J', 'a', 'v', 'a'});
String str = String.valueOf(new char[]{'J', 'a', 'v', 'a'});
String.format(format, args...)
▪ Example:
String s = String.format("%5.2f", 45.556);
Java Basic
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 27
String/Number casting
▪ Convert a digit sequence to number
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 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.
▪ 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"); }
✔you can specify delimiters (the character or characters that separate words)
• the default delimiters are "white space" (space, tab, and newline)
Question
2b
Entering "Question,2b.or !tooBee." or
gives this output: !tooBee
Regular Expression
\\b\\w+\\b matches a word boundary (\\b), one or more word characters (\\w+), and another word boundary.
▪ 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.