Introduction to String Methods
As you may recall, a
String
, which is widely used in Java, is an object that represents a sequence of characters. It is a great way to store information.
Because character strings are so vital to programming, Java dedicated an entire class to them. This is great news for us because the
String
class provides a lot of useful methods to help us perform operations on strings and data manipulation.
In this lesson, we will go over several string methods:
length()
concat()
equals()
indexOf()
charAt()
substring()
toUpperCase()
/toLowerCase()
Let’s get started :)
length()
In Java, the
length()
string method returns the length — total number of characters — of a String
.
Suppose we have a
String
called str
, str.length()
would return its length.
Take a look at this code for example:
String str = "Hello World!";
System.out.println(str.length());
12
would be printed because str
has 12 characters:H
e
l
l
o
_
W
o
r
l
d
!
In theory, the length of a
String
is the same as the Unicode units of the String
. For example, escape sequences such as \n
count as only one character.concat()
The
concat()
method concatenates one string to the end of another string. Concatenation is the operation of joining two strings together.
Suppose we have a
String
called str1
and another String
called str2
, using str1.concat(str2)
would return str1
with str2
appended to the end of it.
For example:
String name = "tushargahora";
name = name.concat("blogspot");
//follow - tushargahora.blogspot.com ;)
System.out.println(name);
tushargahorablogspot
would be printed.
In the code block above, we changed the value of
name
by reassigning it with a new value. However, concat()
it doesn’t actually change the value of the original string.
Suppose we do something slightly different:
String name = "Momo";
name.concat("IsMyLove");
System.out.println(name);
Momo
would be printed instead.equals()
With objects, such as
String
s, we can’t use the primitive equality operator ==
to check for equality between two strings. To test equality with strings, we use a built-in method called equals()
.
For example:
String flavor1 = "Orange";
String flavor2 = "Vanila";
System.out.println(flavor1.equals("Orange"));
// prints true
System.out.println(flavor2.equals("Orange"));
// prints false
Side note, there’s also a
equalsIgnoreCase()
the method that compares two strings without considering upper/lower cases.indexOf()
If we want to know the index of the first occurrence of a character in a string, we can use the
indexOf()
method on a string.
Remember that the indices in Java start with 0:
String letters = "ABCDEFGHIJKLMN";
System.out.println(letters.indexOf("C"));
This would output
2
.
Although
C
is the third letter in the English alphabet, it is located in the second index of the string.
Suppose we want to know the index of the first occurrence of an entire substring. The
indexOf()
an instance method can also return where the substring begins (the index of the first character in the substring):
String letters = "ABCDEFGHIJKLMN";
System.out.println(letters.indexOf("EFG"));
This would output
4
, because EFG
starts at index 4.
If the
indexOf()
doesn’t find what it’s looking for, it’ll return a -1
.charAt()
The
charAt()
method returns the character located at a String
‘s a specified index.
For example:
String str = "Butter";
System.out.println(str.charAt(2));
It would output t because that’s what’s at index 2. (Once again, indices start with 0.)
substring()
There may be times when we only want a part of a string. In such cases, we may want to extract a substring from a string.
The
substring()
the method does exactly that. For example:
String line = "The Heav'ns and all the Constellations rung";
System.out.println(line.substring(23));
It would output
Constellations rung
because that’s what begins at index 23 and ends at the end of line
. The substring begins with the character at the specified index and extends to the end of the string.
But suppose we want a substring in the middle of the string. We can include two arguments with this string method. For example:
String line = "The Heav'ns and all the Constellations rung";
System.out.println(line.substring(23, 38));
It would output
Constellations
because that’s the substring that begins at index 23 and ends at index 38.toUpperCase() / toLowerCase()
There will be times when we have a word in a case other than what we need it in. Luckily, Java has a couple
String
methods to help us out:toUpperCase()
: returns the string value converted to uppercasetoLowerCase()
: returns the string value converted to lowercase
For example:
String input = "Football!";
String upper = input.toUpperCase();
// stores "FOOTBALL!"
String lower = input.toLowerCase();
// stores "football"
Good use of this functionality is to ensure consistency of the data you store in a database. Making sure all of the data you get from a user is lowercase before you store it in your database will make your database easier to search through later.
Congratulations! 😃
We have learned some of the string methods that come with the
String
class:length()
concat()
indexOf()
charAt()
equals()
/equalsIgnoreCase()
substring()
toUpperCase()
/toLowerCase()
"Sharing is Caring..." Share it with your friends also.
No comments: