How to split a Java String – J052

by Dec 12, 2016




DeegeU Java Course

The “How to split a string in Java” video is part of a larger free online class called “Free Java Course Online”. You can find more information about this class on “Free Java Course Online” syllabus.

Transcript – How to split a string in Java

Hi there! In this tutorial we’re going to look at how to split a string in Java. We can split the string by character or split the string by words. We’ll look at both!

We’re going to look at simple string manipulation because it’s a common task. Quite often we’re asked to take a string and manipulate it character by character. That means we need to know how to how to split a string character by character in Java.

Or more often we’re asked to split a Java string representing many words, and we’ll need to look at it word by word. The classic example for this is CSV files. The CSV stands for “character separated values”. Sometimes you’ll hear it called “comma separated values”. Either way, we need a method to split Java strings.

Splitting Java strings by characters

First let’s look at splitting Java strings by character. What that means is if we’re given the word “hello”, we want to access each character. That’s “h”, “e”, “l”, “l”, and “o”.

We’ll start by creating a string with the literal value “hello”. To get an array of characters from this string, we’ll call the method toCharArray(). This takes our string, and returns an array of characters.

char[] characters = "hello".toCharArray();

Reverse a Java string

Let’s assume we’re asked to reverse a string, and for this exercise let’s assume we don’t know about the reverse method in StringBuilder. If we want to do it in a production program, we’ll do something like this. Let’s do it manually, so we can get a feel for manipulating strings by character.

public String reverseString(String s) {
    char[] chars = s.toCharArray();
    int half = s.length() / 2;
    int last = s.length() - 1;
    for (int index=0; index<half; index++)="" {="" char="" temp="chars[index];" chars[i]="chars[last" -="" index];="" chars[last="" index]="temp;" }="" return="" new="" string(chars);="" [="" code]="" the="" first="" thing="" we="" want="" to="" do="" is="" convert="" our="" string="" an="" array="" of="" characters.="" now="" can't="" manipulate="" string,="" since="" it's="" immutable,="" but="" can="" move="" things="" in="" place="" array.="" plan="" this.="" we'll="" take="" character,="" and="" swap="" it="" with="" last="" character.="" keep="" moving="" index="" array,="" position="" minus="" index.="" so="" 0="" gets="" swapped="" position,="" 1="" second="" on.="" if="" don't="" stop="" half="" way,="" reverse="" then="" again.="" need="" middle.="" get="" index,="" which="" length="" one="" we're="" working="" a="" zero="" based="" middle="" just="" that.="" iterate="" over="" swapping="" finally="" make="" character="" it.="" start="" "this="" test",="" method="" will="" "tset="" si="" siht".="" also="" access="" characters="" java="" using="" charat="" method.="" returns="" at="" specified="" string.="" could="" simplify="" by="" iterating="" backwards,="" add="" each="" empty="" this="" doesn't="" swap,="" bit="" easier="" read.="" ="" public="" reversestring(string="" s)="" char[]="" chars="new" char[s.length()];="" for(int="">=0; index--) {
        chars[(s.length()-1) - index]=s.charAt(index);
    }
    return new String(chars);
}

Splitting a Java string into words by space

Now lets look at bigger things. Let’s split a Java string!

Assume we want to remove stop words from a sentence. Stop Words are words which aren’t significant for use in search queries. To make our example simple, we want to remove the word “the”, from the sentence “The quick brown fox jumped over the lazy dog”. Our result should be “quick brown fox jumped over lazy dog”.

To break up a Java string, we need to call the split method. This splits the Java string into a string array. We’re passing a space character to the method, which is telling Java we want to split our Java string by spaces.

public String removeStops(String s) {
    String[] array = s.split(" ");
    StringBuilder builder = new StringBuilder();
    for (String current : array) {
        if (!"the".equals(current)) {
            builder.append(current).append(" ");
        }
    }
    return builder.toString();
}

Next we’ll add each word that’s not “the” to a StringBuilder. If we had more stop words we’d test each word against a set, but for our simple example we just want to manipulate the sentence one word at a time.

When we’re done, we’ll ask our builder to build a Java string and return it. This removes our stop word from our sentence.

Going back to our original thought, how do we split a Java string by character, say a comma or a pipe. That’s what we’d need for CSV parsing.

Splitting a Java String by any delimiter

Say we have a long string representing a single line in a CSV file. A comma separates every value in the string. How would we split this Java string?

String[] array = s.split(",");

Well we’d do it almost the same way. Remember in the Java string split method, we passed the method a blank space. If we wanted to split the Java string using another character, we’d pass a different character. So in this case, we’d pass a comma to the split method. This would give us our CSV values in an array.

We can split our Java string on any character. If you’re looking closely, we’re passing a string to the method. Not a character. The reason is we’re not passing a string. We’re passing a regular expression. We’ll cover regular expressions in depth later, but what we need to know now is some strings won’t work. For example, if we need to split a Java string by a period or dot. This gets a bit tricky.

String[] array = s.split("\\.");

Periods in regular expressions match any single character. That’s not what we want. If we place a period inside the string and then try to split on it, it won’t work. In this case we need to escape the period like this. The key point to note is this is a regular expression, not just matching a string.

Despite all this, we can pass a string to the method and split on the string. We’re just using an example as our regular expression, not a pattern.

For example in our stop words method, we could pass the stop word “the” to split our string, and then just recombine it like this. Of course it makes the spacing a bit wonky, but you get the idea. In this case we’re breaking the string into two, dividing on the word “the”.

Conclusion

So that’s how we split Java strings. We can access individual characters either with the charAt() method, or splitting the Java string into a character array. And we can divide strings based on regular expressions, usually a simple string. Hopefully you’ll find this useful in your Java programming.

If you have any questions, add them to the comments below. If you got this far, hopefully you liked the video. If so, make sure you like, share and subscribe!

And with that, I’ll see you in the next tutorial!

<p><script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- DeegeU - Right Side -->
<ins class="adsbygoogle" style="display:inline-block;width:336px;height:280px" data-ad-client="ca-pub-5305511207032009" data-ad-slot="5596823779"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></p>

Tools Used

  • Java
  • NetBeans
  • JAX-RS

Media Credits

All media created and owned by DJ Spiess unless listed below.

  • No infringement intended

Get the code

The source code for “Are you ready to tackle the fizzbuzz test in Java?” can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:

 git clone https://github.com/deege/deegeu-java-intro.git

Go to the Support > Getting the Code page for more help.

If you find any errors in the code, feel free to let me know or issue a pull request in Git.

<h2>Don't miss another video!</h2> <p>New videos come out every week. Make sure you subscribe!<br><script src="//apis.google.com/js/platform.js"></script></p> <div class="g-ytsubscribe" data-channel="deegeu" data-layout="full" data-count="default"></div> <p></p>

Comments

comments

DJ Spiess

DJ Spiess

Your personal instructor

My name is DJ Spiess and I'm a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I've programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I've worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.

Pin It on Pinterest

Share This