String Concatenation by Concat : Public
String Concatenation by Concat : Public
The String concat() method concatenates the specified string to the end of
current string.
Example9
Output: javapython
Although it is important to note that Strings are immutable. Now lets see
what does this mean.
Once string object is created its data or state can't be changed but a new string
object is created.
Let's try to understand the immutability concept by the example given below:
Example10
Output:
In java whenever an immutable string is attempted to be modified then that string will not be
modified, instead another string is created in non-constant pool with new reference. And as of
now no object is assigned to store the new reference, which is why we cannot see the
concatenated string.
Previously we had a reference for the concatenated string but we did not
assign it to anything, whereas here we are assigning it back to s1 so that now
s1 is pointing to the new concatenated string.
Going further.. If you think we forgot about the third way of comparing
strings that is with the help of compareTo() then certainly we haven’t….So
let’s see how to do it.
s1 == s2 :0
s1 > s2 :positive value
s1 < s2 :negative value
S1 = “SACHIN”
S2 =“SAURAV”
S A C H I N
S A U R A V
Each character is compared in lexical order. In the above example S,A are same
in both string but when C and U are compared we know that C comes before U
therefore s1 is considered lesser than s2. Which will give negative number
when printed on screen.
Case2:
S1= “SAURAV”
S2= “SACHIN”
S A U R A V
S A C H I N
In the above example S,A are same in both string but when U and C are
compared we know that U comes after C therefore s1 is considered greater
than s2. Which will give positive number when printed on screen.
Case3:
S1 = “SACHIN”
S2 =“SACHIN”
S A C H I N
Above we see that all the characters are same in both the strings. Therefore
when compared character by character there is no character greater or
smaller than the other which means it will give the result as 0 when printed on
the screen.
Case4:
S1= “JAVA”
S2= “JAVAC”
Let’s now see what happens when the size of the string is different.
J A V A
J A V A C
We can see that both the strings contain “JAVA” but the second string has
extra character C. Therefore second string is considered to be greater than
the first. And when printed on screen we will get negative number.
Parameters:
ch: char value i.e. a single character e.g. 'a'
fromIndex: index position from where index of the char value or
substring is retured
substring: substring to be searched in this string
The java string charAt() method returns a char value at the given index
number.
The index number starts from 0 and goes to n-1, where n is length of the
string. It returns StringIndexOutOfBoundsException if given index number is
greater than or equal to this string length or a negative number.
The java string startsWith() method checks if this string starts with given
prefix. It returns true if this string starts with given prefix else returns false.
Syntax :
public boolean startsWith(String prefix)
public boolean startsWith(String prefix, int offset)
substring() :
We pass begin index and end index number position in the java substring
method where start index is inclusive and end index is exclusive. In other
words, start index starts from 0 whereas end index starts from 1.
Syntax:
parameters:
The java string toLowerCase() method returns the string in lowercase letter. In
other words, it converts all characters of the string into lower case letter.
Syntax:
The second method variant of toLowerCase(), converts all the characters into
lowercase using the rules of given Locale.
toUpperCase() :
The java string toUpperCase() method returns the string in uppercase letter. In
other words, it converts all characters of the string into upper case letter.
Syntax:
The second method variant of toUpperCase(), converts all the characters into
uppercase using the rules of given Locale.