Java Basics:String Methods-6
Java String hashCode()
The Java String hashCode() method returns a hash code for the string.
A hashcode is a number (object's memory address) generated from any object, not just strings. This number is used to store/retrieve objects quickly in a hashtable.
The syntax of the string hashCode()
method is:
string.hashCode()
Here, string is an object of the String
class.
hashCode() Parameters
The matches()
method doesn't take any parameters.
hashCode() Return Value
- returns the hashcode, which is an
int
value, of the string
The hash code is computed using formula:
s[0]*31(n-1) + s[1]*31(n-2) + ... + s[n-1]
where,
s[0]
is the first element of strings
,s[1]
is the second element and so on.n
is the length of the string
Example: Java String hashCode()
class Main {
public static void main(String[] args) {
String str1 = "Java";
String str2 = "Java Programming";
String str3 = "";
System.out.println(str1.hashCode()); // 2301506
System.out.println(str2.hashCode()); // 1377009627
// hash code of empty string is 0
System.out.println(str3.hashCode()); // 0
}
}
Note: For two strings to be equal, their hash code also must be equal.
Java String join()
The Java String join() method returns a new string with the given elements joined with the specified delimiter.
The syntax of the string join()
method is either:
String.join(CharSequence delimiter,
Iterable elements)
or
String.join(CharSequence delimiter,
CharSequence... elements)
Here, ...
signifies there can be one or more CharSequence
.
Note: join()
is a static method. You do not need to create a string object to call this method. Rather, we call the method using the class name String
.
join() Parameters
The join()
method takes two parameters.
- delimiter - the delimiter to be joined with the elements
- elements - elements to be joined
Notes:
- You can pass any class that implements
CharSequence
tojoin()
. - If an iterable is passed, its elements will be joined. The iterable must implement
CharSequence
. - String, StringBuffer, CharBuffer etc. are CharSequence as these classes implement it.
join() Return Value
- returns a string
Example 1: Java String join() With CharSequence()
class Main {
public static void main(String[] args) {
String result;
result = String.join("-", "Java", "is", "fun");
System.out.println(result); // Java-is-fun
}
}
Here, we have passed three strings Java
, is
and fun
to the join()
method. These string are joined using the -
delimiter.
Example 2: Java String join() With Iterable
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
ArrayList<String> text = new ArrayList<>();
// adding elements to the arraylist
text.add("Java");
text.add("is");
text.add("fun");
String result;
result = String.join("-", text);
System.out.println(result); // Java-is-fun
}
}
Here, an ArrayList of String
type is created. The elements of ArrayList are joined using the -
delimiter.
Java String replaceFirst()
The Java String replaceFirst() method replaces the first substring that matches the regex of the string with the specified text.
The syntax of the replaceFirst()
method is:
string.replaceFirst(String regex, String replacement)
Here, string is an object of the String
class.
replaceFirst() Parameters
The replaceFirst()
method takes two parameters.
- regex - a regex (can be a typical string) that is to be replaced
- replacement - the first matching substring is replaced with this string
replaceFirst() Return Value
- The
replaceFirst()
method returns a new string where the first occurrence of the matching substring is replaced with the replacement string.
Example 1: Java String replaceFirst()
class Main {
public static void main(String[] args) {
String str1 = "aabbaaac";
String str2 = "Learn223Java55@";
// regex for sequence of digits
String regex = "\\d+";
// the first occurrence of "aa" is replaced with "zz"
System.out.println(str1.replaceFirst("aa", "zz")); // zzbbaaac
// replace the first sequence of digits with a whitespace
System.out.println(str2.replaceFirst(regex, " ")); // Learn Java55@
}
}
In the above example, "\\d+"
is a regular expression that matches a sequence of digits. To learn more, visit Java regex.
Escaping Characters in replaceFirst()
The replaceFirst()
method can take a regex or a typical string as the first argument. It is because a typical string in itself is a regex.
In regex, there are characters that have special meaning. These metacharacters are:
\ ^ $ . | ? * + {} [] ()
If you need to match substring containing these metacharacters, you can escape these characters using \
.
// Program to the first + character
class Main {
public static void main(String[] args) {
String str = "a+a-++b";
// replace the first "+" with "#"
System.out.println(str.replaceFirst("\\+", "#")); // a#a-++b
}
}
Java String subSequence()
The Java String subSequence() method returns a character sequence (a subsequence) from the string.
The syntax of the subSequence()
method is:
string.subSequence(int startIndex, int endIndex)
Here, string is an object of the String
class.
subSequence() Parameters
The subSequence()
method takes two parameters.
- startIndex - the starting index
- endIndex - the ending index
subSequence() Return Value
- The
subSequence()
method returns aCharSequence
.
Example: Java String subSequence()
class Main {
public static void main(String[] args) {
String str = "Java Programming";
System.out.println(str.subSequence(3, 8)); // a Pro
}
}
Java String toCharArray()
The Java String toCharArray() method converts the string to a char array and returns it.
The syntax of the toCharArray()
method is:
string.toCharArray(int startIndex, int endIndex)
Here, string
is an object of the String
class.
toCharArray() Parameters
The toCharArray()
method doesn't take any parameters.
toCharArray() Return Value
- returns a char array
Example: Java String toCharArray()
class Main {
public static void main(String[] args) {
String str = "Java Programming";
// creating a char array
char[] result;
result = str.toCharArray();
System.out.println(result); // Java Programming
}
}
Here, the length of the char array result will be equal to the length of the string str.
Comments
Post a Comment