Updated March 23, 2023
Overview of Java String Operators
The following article, Java String Operators, provides an outline of the operators and methods used in Java String. A string is usually a sequence of characters, either as a literal constant or some kind of variable. In Java, strings are treated as objects, and the Java platform provides the String class to create and manipulate such strings. Java String is one of the most widely used classes and is defined in java.lang package. An operator is usually a symbol that asks the compiler to perform specific mathematical or logical manipulations. They take inputs as operands and return some value as an output. An operator in java is also like a symbol that is used to perform operations. For example: +, -, *, / etc.
Java String Operations
The string is associated with a string literal in the form of double-quoted texts such as “Hello, world!”. Thus, we can directly assign a string into a String variable instead of calling the constructor to create a String instance.
In Java, String is the only class where operator overloading is allowed. For example, we can concat two strings using the + operator.
For example:
"a"+"b"=" ab"
Two ways to create a Java string object:-
1. Using string literal: Java String literal can be created by using double quotes. For example:
String s="Hello";
2. Using a new keyword: Java String can also be created by using the keyword “new”. For example:
String s=new String("Hello");
Java String class implements three interfaces, namely – Serializable, Comparable and CharSequence. As the Java String is immutable and fixed, we need to create a new string whenever we need to do string manipulation. And since string manipulation consumes resource, java provides two utility classes: StringBuffer and StringBuilder. Using these two utility classes, it becomes easier to manipulate the java string. Let’s see some examples.
Methods in the String Class
Let us see the methods in the String Class.
1. String Length
The Java String is basically an object, which used to have methods that perform some operations on the strings. As an example, with the help of the method “length()”, we can find the length of a string. Example:
public class MyClass
public static void main(String[] args) {
String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int len = txt.length();
System.out.println("The length of the txt string is: " + len);
}
}
Output:
2. Upper Case & Lower Case
To make an string uppercase & lowercase the string methods are: toUpperCase() and toLowerCase()
Example:
public class MyClass {
public static void main(String[] args) {
String txt = "Hello World";
System.out.println(txt.toUpperCase());
System.out.println(txt.toLowerCase());
}
}
Output:
3. Finding Index in a given String
We can find the index of a given string in Java using the method “indexOf()”. It returns the index position of the 1st occurrence of the specific text, which includes the white spaces as well.
Code:
public class MyClass {
public static void main(String[] args) {
String txt = "Please locate where 'locate' occurs!";
System.out.println(txt.indexOf("locate"));
}
}
Output:
N.B: In Java, the counting position is considered to be from zero (0). That means “0” is the position one or first position in a given string and “1” is considered to be the second position, “2” is considered to be the 3rd position and it goes soon.
4. Java String Concatenation
If we consider the operator “+”, which is used for number addition (in java string concatenation), which means” together”, then in Java for string, we can use the same for the addition of strings to create a new string. And the operation is known as string concatenation.
Example #1
Code:
public class MyClass {
public static void main(String[] args) {
String firstName = "Raju";
String lastName = "Raj";
System.out.println(firstName + " " + lastName);
}
}
Output:
We can also use the concat() method to concatenate two strings:
Example #2
Code:
public class MyClass {
public static void main(String[] args) {
String firstName = "Raju ";
String lastName = "Raj";
System.out.println(firstName.concat(lastName));
}
}
Output:
5. Trimming a String
If we have a string with spaces at starting and ending, this method will help us remove those.
Example:
Code:
class StringTrim{
public static void main(String args[]){
String s1 = new String(" AbodeQA ");
s1 = s1.trim();
System.out.println(s1);
}
}
Output:
Java String Class Methods
The java.lang.string class provides many useful methods to perform operations.
Below are some of the methods which are supported by the String class:
Method |
Description |
char charAt(int index) | Returns char value for the index |
String concat(String str) | It concatenates the string to the end |
boolean equals(Object another) | Checks the equality of string with the given |
int compareTo(Object o) | Compares the string to other objects |
static String format(String format, Object… args) | It returns a string that is formatted |
boolean endsWith(String suffix) | For testing the string if it ends with suffix or not |
byte getBytes() | Encodes a string to a sequence of bytes |
int indexOf(int ch) | Returns the char value index |
boolean isEmpty() | It checks if the string is empty or not |
int lastIndexOf(String regex) | Returns index of the rightmost occurrence |
String intern() | It returns interned string |
int length() | Returns length of the sting |
int hashCode() | It returns the hash code |
boolean matches(String regex) | Checks if a string matches the regular expression |
String trim() | It removes the starting & ending spaces of the string |
String[] split(String regex) | It returns a split string to matching a regex |
String toLowerCase() | Returns string to lowercase |
String substring(int beginIndex) | It returns the substring for the starting index |
String toUpperCase() | Returns string to uppercase |
String replace(char old, char new) | It replaces the occurrences of the char value |
Recommended Articles
This has been a guide to Java String Operators. Here we discussed basic concept, methods, how to use String Operators in Java programming, and the help of examples. You can also go through our other suggested articles to learn more–