Java String Examples – Comparing, Finding, and Extracting

[ad_1]

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:

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

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:

if (s1.equals (s2)) {// do something here} 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:

if (toppings.indexOf ("pepperoni") & gt; = 0) {// your logic here}

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:

if (toppings.toLowerCase (). indexOf ("pepperoni") & gt; = 0) {// your logic here}

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

if (toppings.matches (". * pepperoni. *")) System.out.println ("found a match");

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:

". * pepperoni. *"

instead of this, which would not work:

"pepperoni" 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:

String address = "Four score and seven years ago";

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

String sub = address.substring (5);

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

score and seven years ago

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

String sub = address.substring (5, 20);

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:

score and seven

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:

String [] toppings = new String [20];

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:

String [] toppings = {"Cheese", "Pepperoni", "Black Olives"};

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

[ad_2]

Source by Alvin Alexander

View all VPN Deals

Trusted Coupon
Logo
Compare items
  • Total (0)
Compare