Basic String class
Basic String class
Strings, which are widely used in Java programming, are a sequence of characters. In Java programming
language, strings are treated as objects.
The Java platform provides the String class to create and manipulate strings.
Creating Strings
Whenever it encounters a string literal in your code, the compiler creates a String object with its value
in this case, "Hello world!".
As with any other object, you can create String objects by using the new keyword and a constructor.
The String class has 11 constructors that allow you to provide the initial value of the string using
different sources, such as an array of characters.
Note − The String class is immutable, so that once it is created a String object cannot be changed. If
there is a necessity to make a lot of modifications to Strings of characters, then you should use String
Buffer & String Builder Classes.
String Length
Methods used to obtain information about an object are known as accessor methods. One accessor
method that you can use with strings is the length() method, which returns the number of characters
contained in the string object.
The following program is an example of length(), method String class.
This returns a new string that is string1 with string2 added to it at the end. You can also use the concat()
method with string literals, as in −
"My name is ".concat("Zara");
boolean contains(CharSequence s)
8
This method ceturns true if and only if this string contains the specified sequence of char values.
byte[] getBytes()
18 This method encodes this String into a sequence of bytes using the platform's default charset,
storing the result into a new byte array.
int hashCode()
22
This method returns a hash code for this string.
String intern()
27
This method returns a canonical representation for the string object.
boolean isEmpty()
28
This method returns true if, and only if, length() is 0.
int length()
33
This method returns the length of this string.
boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
36
This method tests if two string regions are equal with case ignored.
char[] toCharArray()
49
This method converts this string to a new character array.
String toLowerCase()
50 This method converts all of the characters in this String to lower case using the rules of the
default locale.
String toString()
52
This method returns the string itself.
String toUpperCase()
53 This method converts all of the characters in this String to upper case using the rules of the
default locale.
String trim()
55
This method returns a copy of the string, with leading and trailing whitespace omitted.
The Java StringBuffer class is used to create mutable (modifiable) String objects. The StringBuffer class
in Java is the same as the String class except it is mutable, i.e., it can be changed.
Note: Java StringBuffer class is thread-safe, i.e., multiple threads cannot access it simultaneously. So, it
is safe and will result in an order.
public synchronized insert(int offset, String s) It is used to insert the specified string into this
StringBuffer 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 replace(int startIndex, int It is used to replace the string from the
StringBuffer endIndex, String str) specified startIndex and endIndex.
public synchronized delete(int startIndex, int It is used to delete the string from the
StringBuffer endIndex) specified startIndex and endIndex.
public int length() It is used to return the length of the string, i.e.
total number of characters.
public String substring(int beginIndex) It is used to return the substring from the
specified beginIndex.
public String substring(int beginIndex, It is used to return the substring from the
int endIndex) specified beginIndex and endIndex.
class Main {
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello ");
sb.append("Java");//now original string is changed
System.out.println(sb);//prints Hello Java
} }
Output:
Hello Java
HJavaello
class Main {
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
sb.replace(1,3,"Java");
System.out.println(sb);//prints HJavalo
}
}
Output:
HJavalo
class Main {
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
sb.delete(1,3);
System.out.println(sb);//prints Hlo
} }
Output:
Hlo
5) StringBuffer reverse() Method
The reverse() method of the StringBuffer class reverses the current String.
class Main {
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
sb.reverse();
System.out.println(sb);//prints olleH } }
Output:
olleH