Index
- [[#length|
length()
]]- [[#tolowercase|
toLowerCase()
]]- [[#touppercase|
toUpperCase()
]]- [[#charat|
charAt()
]]- [[#substring|
substring()
]]- [[#concat|
concat()
]]
Danger
length()
Description:
The length()
method returns the length of a String
object. The length is equal to the number of 16-bit Unicode characters in the string.
Syntax:
Parameters:
The length()
method does not take any parameters.
Return Value:
This method returns an int
value representing the number of characters in the String
.
Example:
toLowerCase()
Description:
The toLowerCase()
method converts all the characters in a String
to lower case.
Syntax:
Parameters:
The toLowerCase()
method does not take any parameters.
Return Value:
This method returns a String
that represents the original string converted to lowercase.
Example:
toUpperCase()
Description:
The toUpperCase()
method converts all the characters in a String
to upper case.
Syntax:
Parameters:
The toUpperCase()
method does not take any parameters.
Return Value:
This method returns a String
that represents the original string converted to uppercase.
Example:
charAt()
Description:
The charAt()
method returns the character at the specified index in a String
. The index value should lie between 0
and length()-1
.
Syntax:
Parameters:
The charAt()
method takes one parameter:
index
(required): This is the index of the character that you want to retrieve. Indexing starts from0
.
Return Value:
This method returns the char
value at the specified index of this string. The first char
value is at index 0
.
Example:
substring()
Description:
The substring()
method returns a new string that is a substring of the original string. The substring begins with the character at the specified index and extends to the end of the string or up to endIndex - 1, if the second argument is given.
Syntax:
Parameters:
The substring()
method takes one or two parameters:
beginIndex
(required): This is the beginning index, inclusive.endIndex
(optional): This is the ending index, exclusive.
Return Value:
This method returns a new String
that is a substring of this string.
Example:
In subStr1
, we only provide the beginIndex
, so the substring includes all characters from index 7
to the end of the string.
In subStr2
, we provide both beginIndex
and endIndex
, so the substring includes characters from index 7
up to but not including index 12
.
concat()
Description:
The concat()
method concatenates the specified string to the end of this string. If the length of the argument string is 0
, then the original string is returned.
Syntax:
Parameters:
The concat()
method takes one parameter:
s
(required): This is theString
that is concatenated to the end of thisString
.
Return Value:
This method returns a String
that represents the concatenation of this String
object and the String
argument.
Example:
Differences between concat() and +
The
concat()
method and the+
operator both concatenate strings in Java, but there are some differences:
Performance: For concatenating a few strings, there’s not much difference in performance between
concat()
and+
. However, if you’re concatenating many strings in a loop, usingStringBuilder
orStringBuffer
is more efficient.Null Handling: If you use
concat()
and one of the strings isnull
, aNullPointerException
will be thrown. On the other hand, if you use+
and one of the strings isnull
, it will be treated as"null"
.
indexOf()
Description:
The indexOf()
method returns the index within this string of the first occurrence of the specified character. If a character with value c
occurs in the character sequence represented by this String
object, then the index of the first such occurrence is returned.
Syntax:
Parameters:
The indexOf()
method takes one parameter:
c
(required): This is the character for which to find the first occurrence.
Return Value:
This method returns the index of the first occurrence of the character in the character sequence, or -1
if the character does not occur.
Example:
replace()
Description:
The replace()
method returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
Syntax:
Parameters:
The replace()
method takes two parameters:
oldChar
(required): This is the old character.newChar
(required): This is the new character.
Return Value:
This method returns a new String
derived from this String
by replacing every occurrence of oldChar
with newChar
.
Example:
equals()
Description:
The equals()
method compares this string to the specified object. The result is true
if and only if the argument is not null
and is a String
object that represents the same sequence of characters as this object.
Syntax:
Parameters:
The equals()
method takes one parameter:
string2
(required): This is theString
to compare withstring1
.
Return Value:
This method returns a boolean
- true
if the given object represents a String
equivalent to this string, false
otherwise.
Example:
Differences between equals() and ==
In Java,
equals()
and==
operator are used for comparison. However, they work differently when it comes to objects likeString
.
equals()
: This method compares the “content” of the strings. If two strings contain the same characters in the same order,equals()
will returntrue
.
==
operator: This compares the “references” of the strings, not their content. It checks if both strings point to the same object in memory. So, even if two strings contain exactly the same characters,==
will returnfalse
if they are not the same object in memory.Therefore, when comparing
String
values in Java, it’s generally recommended to useequals()
, not==
.