Java String Examples – Comparing, Finding, and Extracting

Posted by


In this article we’ll look at a collection of Java String programming tips. The Java String class is one of the most commonly-used classes, so it’s well worth knowing how to perform some of the most common operations.

Testing String equality

The first important thing to know about working with Java strings is how to compare two strings to each other. Unlike comparing primitives in Java, you cannot compare two Java strings like this:

<br /> if (s1 == s2) { // do something here }<br />

Actually, you can do that, but it would be wrong. The correct way to compare two Java strings is to use the String equals method, like this:

<br /> if (s1.equals(s2)) { // do something here }<br /> Finding one string in another string

A second common String operation is to search one string to see if it contains some other string. For instance, assume you had a string named toppings, and you wanted to see if that string contained the string “pepperoni”. A simple way to do that is to use the indexOf() method, like this:

<br /> if (toppings.indexOf(&#8220;pepperoni&#8221;) &gt;= 0) { // your logic here }<br />

As a precaution, if you don’t know the case of the string you’re comparing to, you’re always better off converting that string to upper- or lower-case, like this:

<br /> if (toppings.toLowerCase().indexOf(&#8220;pepperoni&#8221;) &gt;= 0) { // your logic here }<br />

One final way to do this, which is really very powerful, is to use the matches() method of the Java String class, like this:

<br /> if (toppings.matches(&#8220;.*pepperoni.*&#8221;)) System.out.println(&#8220;found a match&#8221;);<br />

Note that with this method you actually need to pass in a regular expression, that’s why the string in between the parentheses looks like this:

<br /> &#8220;.*pepperoni.*&#8221;<br />

instead of this, which would not work:

<br /> &#8220;pepperoni&#8221;<br /> Extracting a substring

Another very common Java String class operation is to extract a substring from a String. Java includes a substring() method for just this purpose. Let’s look at a few examples.

If we start with this String declaration:

<br /> String address = &#8220;Four score and seven years ago&#8221;;<br />

we can extract everything after the fifth position in the string with this substring statement:

<br /> String sub = address.substring(5);<br />

If we print out the new String named sub, the output will look like this:

<br /> score and seven years ago<br />

With the substring() method, we can also limit the length of the substring we extract, so this statement:

<br /> String sub = address.substring(5, 20);<br />

can be read as, “Starting after the fifth position in the String, extract the next 20 characters.” If we now print the output of the variable sub, it will look like this:

<br /> score and seven<br />

There are more operations you can perform on strings, including working on Java String arrays, but these are some of the most common String methods you’ll need on a daily basis.

Java String array examples

It’s also very common to store strings in Java arrays. There are two main ways to declare a Java String array. In the first approach, you define the array, and give it an initial size, like this:

<br /> String[] toppings = new String[20];<br />

After that, you populate the array later in your code.

In the second approach, you define and populate your Java String array in one statement, like this:

<br /> String[] toppings = {&#8220;Cheese&#8221;, &#8220;Pepperoni&#8221;, &#8220;Black Olives&#8221;};<br />

Either approach will work; it’s just a matter of which array syntax you need for the current situation.


Leave a Reply

Your email address will not be published. Required fields are marked *