1. Introduction
In this tutorial, We'll learn more about Java Strings. without String usage, there is no application in the java world. Java Strings are very crucial in defining the properties in the model classes. Java String is part of java.lang package. String class is getting added with new methods in java 8, 11, 12 versions. String API has a lot of utility methods.
In this article, we will be exploring different scenarios where String is being used always.
2. Obtaining s Sub Section from a String
We may need to take or retrieve the portion of a String in java in sometimes. String API has a method to get the exact portion of a string that method is substring() method.
This substring() method is overloaded in varients.
public String substring(int beginIndex)
public String substring(int beginIndex, int endIndex)
Use the substring() method to obtain a portion of the string between two different positions or one position that is from the given index to till the end.
Output:
Comparing two string is the most common asked in the java interview. String has a method equals() and equalsIgnoreCase() methods to compare the strings.
Along with that "==" double equal operator also can be used to compare. The interview will ask the differences between == and equals method.
"String equals() method in java with example - Internal Implementation"
"Difference between String.equals() and String.contentEquals() method?"
"Java Compare Two Strings Lexicographically and how compareTo() method works internally"
String substring Example:
package com.javaprogramto.w3schools.engineering.string;
/**
*
* Getting a substring or part of a String.
*
* @version javaprogramto.com
*/
public class StringSubString {
public static void main(String[] args) {
String value = "Welcome to JavaProgramTo.com";
System.out.println("Original String : " + value);
String substring1 = value.substring(0, 7);
System.out.println("Substring one : "+substring1);
String substring2 = value.substring(7, value.length());
System.out.println("Substring Two : "+substring2);
String substring3 = value.substring(10);
System.out.println("Substring Three : "+substring3);
}
}
Output:
Original String : Welcome to JavaProgramTo.com
Substring one : Welcome
Substring Two : to JavaProgramTo.com
Substring Three : JavaProgramTo.com
3. Comparing Strings (Two Strings)
Comparing two string is the most common asked in the java interview. String has a method equals() and equalsIgnoreCase() methods to compare the strings.
Along with that "==" double equal operator also can be used to compare. The interview will ask the differences between == and equals method.
Read more on:
"String equals() method in java with example - Internal Implementation"
"Difference between String.equals() and String.contentEquals() method?"
"Java Compare Two Strings Lexicographically and how compareTo() method works internally"
Example on "==" and equals():
- "==" compares the memory address of the string or any object always.
- equals(Object o) method compares the contents of two string and passed objects mostly will be a string in all cases. But, we can pass any object to it. It internally checks if the passed object is an instance of String or not. If the object does not string then it returns false immediately without checking its contents.
package com.javaprogramto.w3schools.engineering.string;
/**
*
* Comparing two strings with == and equals() method.
*
* @version javaprogramto.com
*/
public class StringCompare {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";
String s3 = "hello";
String s4 = new String("hello");
String s5 = new String("hello");
String s6 = new String("world");
System.out.println("s1 == s2 : " + s1 == s2);
System.out.println("s1 == s3 : " + s1 == s3);
System.out.println("s1 == s4 : " + s1 == s4);
System.out.println("s3 == s4 : " + s3 == s4);
System.out.println("s1.equals(s2) : " + s1.equals(s2));
System.out.println("s1.equals(s3) : " + s1.equals(s3));
System.out.println("s1.equals(s4) : " + s1.equals(s4));
System.out.println("s4.equals(s5) : " + s4.equals(s5));
System.out.println("s4.equals(s6) : " + s4.equals(s6));
StringBuffer buffer = new StringBuffer("hello");
System.out.println("s1.equals(buffer) : " + s1.equals(buffer));
}
}
Output:
false
false
false
false
s1.equals(s2) : false
s1.equals(s3) : true
s1.equals(s4) : true
s4.equals(s5) : true
s4.equals(s6) : false
s1.equals(buffer) : false
4. Trimming the white or blank spaces
If there are blank spaces or white space characters at the beginning or end of the string, then we can remove them using a string class trim() method.
public String trim()Returns a string whose value is this string, with all leading and trailing space removed, where space is defined as any character whose code point is less than or equal to 'U+0020' (the space character).
String trim() Example:
package com.javaprogramto.w3schools.engineering.string;
/**
*
* Trimming the white spaces.
*
* @author javaprogramto.com
*/
public class StringTrim {
public static void main(String[] args) {
String value = " Welcome to JavaProgramTo.com ";
System.out.println("Original String length : " + value.length());
String trimmedStr = value.trim();
System.out.println("Length after trimming : " + trimmedStr.length());
}
}
Output:
Original String length : 33
Length after trimming : 28
See the difference between the original and trimmed string lengths. Trimmed one has no white space characters now.
5. Changing the cases of String
If the string is having the case sensitive values in your application then we need to convert them into either upper case or lowercase before processing to avoid any case sensitive issues.
String classs has toUpperCase() and toLowerCase() methods to work with case conversions.
Case conversion example:
package com.javaprogramto.w3schools.engineering.string;
/**
*
* Trimming the white spaces.
*
* @author javaprogramto.com
*/
public class StringCaseConversions {
public static void main(String[] args) {
String in = "India";
String usa = "Usa";
System.out.println("Converting upper case : " + in.toUpperCase());
System.out.println("Converting upper case : " + usa.toUpperCase());
System.out.println("Converting lower case : " + in.toLowerCase());
System.out.println("Converting lower case : " + usa.toLowerCase());
}
}
Output:
Converting upper case : INDIA
Converting upper case : USA
Converting lower case : india
Converting lower case : usa
Conversions with Locale:
System.out.println(in.toUpperCase(Locale.CHINA));
System.out.println(in.toUpperCase(new Locale("it","US")));
System.out.println(in.toLowerCase(new Locale("fr", "CA")));
Output:
INDIA
INDIA
india
6. Conclusion
In this article, We have seen some of the common use cases for String api methods as retrieving a portion of a string, comparison, removing white spaces and case conversions.
We will be exploring some of the things such as how to concatenate strings, a string to numbers, replacing patterns, file name ends with a specific format.. etc..
0 Comments