Initial String handling examples on a training course are sometimes a bit mundane - and that's because we have to cover some of the basics like "what is a string" before we start reading them in useful quantities from files, databases, or web resources. But it's important to get those basics understood early - questions like "how do we test for two strings being equal?
Now ... in most languages, you may already know that two equals signs checks equality ... and so you would expect you use that symbol for string equality. And it's surprising how often you would be wrong.
will work very nicely in PHP and in Python
is what you use in an SQL WHERE clause
or (more modern) ~~
is what you need in Perl
and some language have functions or methods for the job:
is the function in C
is the function in Java ...
and that's because these are pointer based languages,and if you compare with == you're checking for two names pointing at the same memory address, and not looking at two different locations to see if they contain the same thing.
There's a Java example that shows how string comparisons work - [here - ashton.java]
- with an explanation too. A further example - [here - purton.java]
- shows you a number of other tests on strings in Java - testing to see if they're qual (but ignoring case) or the start or the end of the string. And when we start to manipulate strings, we use functions like indexOf and substring - a further example [here - oaksey.java]
and another [here - minety.java]
Moving on, then, really practical
and useful string demonstations start once you get on to applications / programs that have a significant flow of data being read - and there's a new Java example from last week [here - Vest.java]
that shows that. We've used a BufferedReader
objects to get a file handle to the data source, line by line, and the readLine
method to read the data itself. We're then using indexOf
again - but this time with practical good cause - to manipulate the data.
Moving on further, you can start using utility classes such as ArrayList
s and HashMap
s to hold, count, and manage strings - the example above has been extended to show this [here - Tshirt.java]
(I could usefully have demonstrated a StringTokenizer too!) and then to add some good validation checks using excpetions [here - Bikini.java]
. (written 2010-11-13)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesJ707 - Java - Strings 
Length, size or capacity in Java? - (2010-02-24) 
Trying out our Java examples on our web site - (2008-02-27) 
An answer to a student asking 'Help' - (2007-11-27)J712 - Java - Exceptions 
After Course Resources - do we publish sample answers. Example from Java Exceptions module. - (2010-11-13) 
Fail Safe Error Handling in Java via Exceptions - (2010-07-09) 
Handling unusual and error conditions - exceptions - (2010-02-03) 
Exceptions in Java - why and how - (2009-09-24) 
What are exceptions - Python based answer - (2008-11-08) 
Final, Finally and Finalize - three special words in Java - (2007-02-05)J714 - Java - Fundamental classes 
Sorting - naturally, or into a different order - (2010-08-14) 
for and foreach in Java - (2010-04-22) 
Sorting Collections of Objects in Java - (2009-09-25) 
Viv.java uses unchecked or unsafe operations - explanation and cure - (2009-09-24) 
Java Collection Objects in the java.util package - (2009-08-05) 
Java - Generics - (2008-11-27) 
Java, sorting, ArrayList example, generics - (2008-01-11) 
Java sorting - comparable v comparator - (2007-02-02) 
Do languages change? - (2004-09-08)
Some other Articles
Getting your C++ program to runPositively reforming the system - could it be done?Views of Melksham - November 2010Computer Graphics is fun - even if Java Applets are Old HatString handling - from first steps to practical examplesWhat is a universal superclass? Java / Perl / Python / Other OO languagesJava Beans, tag libraries and JSPs - what and why.Changing a Servlet - more that just editing and compilingGathering information - logging - with log4j. First steps.