The String Constructors, Various String Operations
The String Constructors, Various String Operations
The intern() method ensures that the string is added to the string pool and returns the reference from the
pool, allowing more efficient memory use by avoiding duplicate string objects.
Example:
// Create a string using 'new', which creates a new object in the heap
String str1 = new String("Hello");
// Interning str1, which means str1 will now refer to the string from the pool
str1 = str1.intern();
Creates a string from the char[] chars = {'H', 'e', 'l', 'l', 'o'};
String(char[] value) String str = new String(chars);
specified character array.
// Output: "Hello"
Creates a string from the byte[] bytes = {72, 101, 108, 108,
String(byte[] bytes) 111}; String str = new String(bytes); //
specified byte array.
Output: "Hello"
Example:1
import java.util.regex.*;
if (matcher.find()) {
System.out.println("Found literal 'hello' in the text."+m.group());
} else {
System.out.println("Literal 'hello' not found.");
}
}
}
Example: 2
import java.util.regex.*;
public class RegexDigits {
public static void main(String[] args) {
String text = "Arun21BCE0001 bala24MIC0025";
Pattern p= Pattern.compile(pattern);
Matcher matcher = p.matcher(text);
while(matcher.find()) {
System.out.println("complete Register no: "+matcher.group(0));
System.out.println("Batch: "+matcher.group(1));
System.out.println("Programmee: "+matcher.group(2));
System.out.println("Register no: "+matcher.group(3));
}
}
}