Strings in java
1. Introduction
Strings are objects in java which uses char array to store characters of a string. A string is immutable in nature. Whenever any change is made to a string object an entirely new object is created.
2. Creating strings
2.1. Using string literal
This is the most common way of creating a string object in java.
// Using string literal
String s1 = "this is a string";
2.2. Using new keyword
Since string is a class in java, we can also use new keyword to create a string object.
// Using new keyword
String s1 = new String("this is a string");
3. Comparing strings
To compare two strings always use the “equals” or “equalsIgnoreCase” methods of String class Instead of “==” or “!=” operators. Because these operators compare the references of the objects and it is possible for two or more string objects to have same value, which can result in wrong answer. Lets see the example where we compare two string object which are created using new keyword and then compare them :
public class Main {
public static void main(String[] args) {
String s1 = new
String("first");
String s2 = new String("first");
System.out.println("Using == operator");
System.out.println(s1==s2);
System.out.println("Using equals method");
System.out.println(s1.equals(s2));
}
}
Ouptut:
Using == operator
false
Using equals method
true
As you can see although the strings were same “==” operator returned false because it compared only the references of the strings.
Let’s take a look at another example:
public class Main {
public static void main(String[] args) {
String s1 = "first";
String s2 = "first";
System.out.println("Using == operator");
System.out.println(s1==s2);
System.out.println("Using equals method");
System.out.println(s1.equals(s2));
}
}
Output:
Using == operator
true
Using equals method
true
here both of them returned true but why?
Reason
In both the example we compared the same strings but the results were different. In the first example we created the string objects using new keyword whereas in second example we used string literals. <mark class="cdx-marker">When we create a string object using string literal the JVM checks for a string with same value in the string pool if it founds the string with string with same value it returns its reference or else it creates a new string object in string pool and then returns its reference.</mark> Whereas whenever new keyword is used a new string object is created.
4. Case changing in strings
Java gives us two methods to change the case of string which are present in String class.
toUpperCase()
: This method converts
all the characters to upper case of a given string.
toLowerCase()
: This
method converts all the characters to lower case of a given string.
public class Main {
public static void main(String[] args) {
String test = "Test String";
System.out.println("Original : "+test);
System.out.println("Upper Case : "+test.toUpperCase());
System.out.println("Lower Case : "+test.toLowerCase());
}
}
Output:
Original : Test String
Upper Case : TEST STRING
Lower Case : test string
5. String inside a string
String.contains()
can
be used to check if a string is present
within a string.
public class Main {
public static void main(String[] args) {
String test = "hello world";
System.out.println(test.contains("world"));
}
}
Output:
true
Keep in mind that contains() is case-sensetive.
System.out.println(test.contains(“World”));
This will return false.
String.indexOf()
is
used to find the index within a string from where the given string starts.
public class Main {
public static void main(String[] args) {
String test = "hello world";
System.out.println(test.indexOf("world"));
}
}
Output:
6
String.indexOf()
is also case-sensetive.
System.out.println(test.indexOf("World"));
This will give -1.
You can use toLowerCase() or toUpperCase() on both the strings before applying case-sensetive methods on them so that both the strings are of same case.
public class Main {
public static void main(String[] args) {
String test = "hello world";
String s = "World";
System.out.println(test.toLowerCase().indexOf(s.toLowerCase()));
}
}
Output:
6
6. Splitting strings
You can split a string using String.split()
to split a
string on a particular character or a regular expression. This method returns
an array of strings which contains the substrings of target string.
public class Main {
public static void main(String[] args) {
String test = "first,second,third,fourth,fifth";
String [] result = test.split(",");
for(String s : result){
System.out.print(s+" ");
}
}
}
Output:
first second third fourth fifth
7. Substrings
Java has String.substring()
method which takes one or two
parameters to get a part of target string.
public class Main {
public static void main(String[] args) {
String test = "This is the testing string";
String sub1 = test.substring(12); // returns string starting from index 12 till the end
String sub2 = test.substring(12, 19); // returns string from index 12 to 19
System.out.println(sub1);
System.out.println(sub2);
}
}
Output:
testing string
testing
8. Replacing parts of string
String.replace()
is used to replace parts of a given string.
This method takes two parameters target string and replacement string. Target
is replaced by the given replacement string
public class Main {
public static void main(String[] args) {
String test = "This is the testing string";
System.out.println(test.replace("This","that"));
}
}
Output:
that is the testing string
9. Summary
In this article we discussed about how to create strings in java , how to compare two strings, how to find strings within a string, how to change case of a string, how to split a string, substrings in a string and how to replace parts of string.