next up previous
Next: Writing Good Documentation Up: The Frightened Freshers Guide Previous: HashMaps

More Fun With The Java API And Strings

Although we have been using Strings quite a bit in this little tutorial, I have not introduced the Java API for Strings. Strings have many methods that you can execute on them many of which you will need to explore and look at yourself to see exactly how they work. There is one VERY useful thing in the Java API for Strings which is the split method.

From the Javadoc:
\begin{lstlisting}
public String[] split (String regex)
\end{lstlisting}

What the hecks that mean?! Well, it means that the function will return an array of Strings split by the regular expression named regex. Regular expressions are very, very powerful but I do not know how to use them so I will not try. Anyways, here is a short example of using the split method.


\begin{lstlisting}
String exampleString = ''This is just an example'';
String[] splitBySpaces = exampleString.split('' '');
\end{lstlisting}

This is basically saying that wherever there is a space, split up the String and return an array with all the split up Strings inside of it. After being executed the array splitBySpaces would contain the elements:

 Array Element Contents
 splitBySpaces[0] "This
 splitBySpaces[1] "is"
 splitBySpaces[2] "just"
 splitBySpaces[3] "an"
 splitBySpaces[4] "example"

There are some more examples of this in the lecture slides for CO320, look for lecture 14 (Week 9, Monday 21st November).


next up previous
Next: Writing Good Documentation Up: The Frightened Freshers Guide Previous: HashMaps
Tom Carlson 2006-01-10