Java String Manipulation[1]
Java String Manipulation[1]
💡 Note:StringIndexOutOfBoundsException is given when the given specified index number is equal to this string length or
the specified given index number is greater, or it is a
negative number. The first char value is at index 0
public class Main {
public static void main(String[] args) {
String myStr = "Hello Puneet";
// returns character at index 6
char result = myStr.charAt(6);
System.out.println(result);
}
}
💡 Returns
1. The outcome is a negative integer if the first string's lexical length is shorter than the second string's
2. The outcome is a Positive integer if the first string is lexicographically longer than the second string.
💡 3.a ClassCastException
The result is zero if it is lexically equal to the second string.If it is impossible to compare this item to the specified object,
is thrown.If the specified object is null, a NullPointerException is thrown.
💡 Returns
1. The outcome is a negative integer if the first string's lexical length is shorter than the second string's
2. The outcome is a Positive integer if the first string is lexicographically longer than the second string.
3. The result is zero if it is lexically equal to the second string.
💡 Exceptions
If it is impossible to compare this item to the specified object, a ClassCastException is thrown.
If the specified object is null, a NullPointerException is thrown.
System.out.println(s1.compareToIgnoreCase(s2));
System.out.println(s1.compareToIgnoreCase(s3));
System.out.println(s1.compareToIgnoreCase(s4));
System.out.println(s1.compareToIgnoreCase(s5));
System.out.println(s1.compareToIgnoreCase(s6));
System.out.println(s1.compareToIgnoreCase(s7));
System.out.println(s1.compareToIgnoreCase(s8));
System.out.println(s1.compareToIgnoreCase(s9));
System.out.println(s1.compareToIgnoreCase(s10));
}
💡 Returns
A string that represents the concatenation of this object's characters followed by the string argument's characters.
💡 Note:
The CONCAT() function
concatenates two strings only. If you want to concatenate more than two
strings, you need to apply the CONCAT() function multiple times.
💡 Returns
true if this string contains s, false otherwise
💡 Note:
The Java String contains()
method checks whether a string contains a sequence of characters and
Returns true if the characters exist and false if not.
💡 Returns
true if this String represents the same sequence of char values as the specified sequence, false otherwise.
💡 Note:
String comparison in Java is done using the equals() and contentEquals() methods of the String class.
.
💡 Returns
true if the character sequence
represented by the argument is a suffix of the character sequence
represented by this object; false otherwise.
💡 Note:
That the result will be true
if the argument is the empty string or is equal to this String object as
determined by the equals(Object) method.
💡 Returns
true if the given object represents a String equivalent to this string, false otherwise
💡 Note:
To check the equality of string contents, We use the equals() function.
💡 Returns
A formatted string using the specified format string and arguments.
💡 Note:
If there are more arguments
than format specifiers, the extra arguments are ignored. The number of
arguments is variable and may be zero.
💡 Returns A formatted string using the specified format string and arguments.
💡 Note: If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is
variable and may be zero.
💡 Returns
The resultant byte array
💡 Note:
The encoding of the string into a series of bytes is performed by the Java String class's
getBytes() function, which stores the result in an array of bytes.
💡 Note:
using int arithmetic, where
s[i] is the ith character of the string, n is the length of the string,
and ^ indicates exponentiation. (The hash value of the empty string is
zero.)
💡 Returns
The index of the first
occurrence of the character in the character sequence represented by
this object, or -1 if the character does not occur.
💡 Note:
If a character with value ch
occurs in the character sequence represented by this String object, then
the index of the first such occurrence is returned.
public class Main {
public static void main(String[] args) {
String myStr = "Hello Java.";
System.out.println(myStr.indexOf("a", 10));
}
}
💡 Note:
It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.
💡 Returns
true if length() is 0, otherwise false
💡 Note:
Since JDK 1.6, the Java String class's isEmpty () method has been a part of Java strings
💡 Returns
a new String that is composed of the elements separated by the delimiter
💡 Throws:
NullPointerException - If delimiter or elements is null
public class StringJoinExample {
public static void main(String[] args) {
String[] words = {"Hello", "World", "Welcome", "to", "Java"};
String sentence = String.join(" ", words);
System.out.println(sentence);
}
}