Performing Advanced String Manipulation - Java eLearning Kit For Dummies (2014)

Java eLearning Kit For Dummies (2014)

Chapter 11. Performing Advanced String Manipulation

· Create strings from other sorts of data and create other sorts of data from strings by using the conversion methods supplied as part of the Java API.

· Locate specific data in strings by using searches, comparison, and substrings.

· Perform string modifications by replacing string content, extracting characters, and splitting strings.

· Create the perfect output for your application by relying on a wealth of formatting techniques.

lingo_fmt.tif

1. Is it possible to convert any data to a string?

Yes, all you need is to use the correct data conversion method, as described on page 340

2. Will my application be able to create other kinds of data from a user’s string input?

Parsing strings makes it possible to perform a wealth of data conversions to other types, as shown on page 342

3. How do I find the sorts of information I need in longer Java strings?

It’s possible to use a number of search techniques, including direct searches, string comparison, and working with substrings, as explained on page 344

4. How do I replace user content with content my application will understand when working with strings?

It’s possible to replace character sequences in a string with other character sequences, as described on page 346

5. Is it possible to divide a string into parts when working with specially formatted strings?

Yes, in fact, splitting strings using a separator character is a common practice in Java that you’ll see demonstrated on page 348

6. What do I need to do to see string output in the form I want?

Java provides an incredible array of string formatting techniques for your string output, many of which appear on page 352

Most applications make heavy use of strings because strings are a data form that humans understand easily. In addition, strings offer flexibility that other data types don’t offer. For example, when working with a string, you can mix numbers and text together. A string can contain any sort of information you like. In addition, a string can even include escape characters to make the string’s content appear in a way that you like. In short, humans understand strings, and strings are incredibly flexible, making them a popular way to store data.

Unfortunately, the computer doesn’t understand string content nearly as well as you do. For example, if you want to perform math-related tasks in your application, you must convert numbers in string format to an actual numeric primitive or object type. Otherwise, the computer will have no idea of how to interact with the data. Fortunately, Java makes the task of converting to and from strings easy.

Strings can become quite long. When they become long enough, you may need to search them in various ways. In addition to looking for specific characters, words, and phrases, you may need to find control characters or perform other tasks that relate to just part of the string. When working with Java, you can search for information at the beginning or end of the string. You can also compare two strings to determine which one contains the sort of information you need. Finally, you can work with substrings, which are parts of strings — a string broken into pieces.

The ability to manipulate strings is also essential. For example, you might want to replace every occurrence of Jack with Jane in a sentence. You can also extract specific characters (such as escape characters) from a sentence or split the string into smaller pieces by using a separator character (a special kind of character that separates one part of the string from another part). Manipulating strings also includes formatting them. You’ve already performed formatting by using escape characters, as described in the section on escape sequences in Chapter 4. This chapter tells you about a few additional tricks you can use to format strings and make them look nice onscreen.

Converting Data to and from Strings

Data conversion is an essential part of Java programming. Humans understand strings far better than they understand the abstractions of other data types that Java uses. In addition, strings let you combine disparate types such as numbers and Boolean values together. However, you can’t use strings to perform computations or assess the truth value of an expression. In short, you need some way to convert between data types.


LINGO

Data conversion defines a method for changing data from one type to a completely different type. For example, using data conversion, you can change a string into a number. You use data conversion when working with different types, which contrasts with casting, where you convert between similar types, such as between int and byte. You saw the first example of casting in the section on bytes in Chapter 3.



EXTRA INFO

Java 8 makes it possible to use unsigned integer and long values. Normally an integer value ranges between –2,147,483,648 to 2,147,483,647. Likewise, a standard long ranges in value from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. However, when working with unsigned values, an integer ranges from 0 to 4,294,967,295, and a long ranges from 0 to 18,446,744,073,709,551,615. This extended positive range can make it possible to perform all sorts of tasks that wouldn’t be possible with signed integers and longs, but you can’t use negative numbers. To make unsigned values work, you need to use unsigned methods such as parseUnsignedInt(), toUnsignedLong(), toUnsignedString(), and parseUnsignedLong() for data conversion. Even though addition, subtraction, and multiplication work the same, division also requires the special divideUnsigned() and remainderUnsigned() methods. You can read more about the special unsigned methods athttp://download.java.net/jdk8/docs/api/java/lang/Integer.html and http://download.java.net/jdk8/docs/api/java/lang/Long.html.


The most common conversion between String and another data type is numbers. In some cases, it’s simply easier to manipulate numeric values as strings. However, the following example shows one clear benefit to using a String for user input and then converting the data to an int — error handling. The following example demonstrates a technique for ensuring accurate numeric input from the user.

Files needed:ConvertIntAndString.java

1. Open the editor or IDE that you’ve chosen to use for this book.

2. Type the following code into the editor screen.

// Import the required API classes.
import java.util.Scanner;
import java.lang.String;
import java.lang.Integer;

public class ConvertIntAndString
{
public static void main(String[] args)
{
// Create a variable to hold the input.
String Input = "";

// Create an intermediate variable.
int Convert = 0;

// Create a variable to hold the output.
String Output = "";

// Create the scanner.
Scanner GetInput = new Scanner(System.in);

// Obtain a string for processing.
System.out.print("Type any numeric value: ");
Input = GetInput.nextLine();

try
{
// Convert the String to an int.
Convert = Integer.parseInt(Input);
}
catch (NumberFormatException e)
{
// The user input an incorrect value.
System.out.println("Type an integer value.\n" +
e.getMessage());

// Exit the program.
return;
}

// Change the value of the input.
Convert += 5;

// Check for a number that's too large.
if (Convert < Integer.parseInt(Input))
{
System.out.println("The input number is too
large!");
return;
}

// Convert the int to a String.
Output = String.valueOf(Convert);

// Display the result.
System.out.printf("%s + 5 = %s%n", Input, Output);
}
}

The application begins by creating three variables: Input of type String, Convert of type int, and Output of type String. The user is then asked to provide an integer input. Of course, the user can type any value. That’s why the call to Integer.parseInt() is placed in a try…catchstructure. The only exception that this call generates is the NumberFormatException. When this occurs, it means that the user has input something other than an integer value.


LINGO

When converting from a string to a numeric type, the code will parse the string looking for data of the right type. Parsing is the act of dividing the string into pieces and then looking through each piece for specific information called a token. The string is normally split at spaces, so “This is a String!” becomes four tokens: “This”, “is”, “a”, and “String!” When the right token is found, the code then converts the value of that token into the correct numeric type.


At this point, the code has access to a known good int, so it performs a math operation with it. One of the problems with primitive types in Java is that they don’t throw an exception when an overflow occurs (when the application places more information in the variable than it can hold), so this example includes specific code to check for the condition. When the condition occurs, the application outputs an error message and exits.

When the math operation succeeds, the code converts the numeric value in Convert to a String. At this point, you could easily format the value to provide a pleasing appearance onscreen, but the example leaves that task for the “Formatting String Output” section of the chapter.

3. Save the file to disk using the filename ConvertIntAndString.java.

4. In your command prompt, type javac ConvertIntAndString.java and press Enter.

The compiler compiles your application and creates a .class file from it.

5. Type java ConvertIntAndString and press Enter.

The application asks the user to provide a numeric input.

6. Type Hello and press Enter.

The application displays the expected error message of “Type an integer value.”

7. Perform Steps 5 and 6 using 5.5 as an input value.

The application correctly detects that this is a floating point number and not an integer, so it displays the error message yet again.

8. Perform Steps 5 and 6 using 2147483648 as an input value.

This is an integer value, but it’s one more than the value an int can hold, so the application displays an error message.

9. Perform Steps 5 and 6 using 2147483647 as an input value.

This value will fit in an int, but it causes an overflow, so the application displays an error message.

10. Perform Steps 5 and 6 using 1,555 as an input value.

This is one case where the application could provide additional handling to allow the user to enter the value. The “Extracting characters” section of the chapter tells you how to work around this particular issue. For now, the application should work with a value of 1,555, but it doesn’t because of the comma in the input.

11. Perform Steps 5 and 6 using 15 as an input value.

You see the expected output shown in Figure 11-1. The use of a string prevents all sorts of incorrect entries. Additional error handling also prevents overflows, but the focus here is that the String to int and int to String data conversion serves a useful purpose.

9781118098783-fg1101.tif

Figure 11-1:

practice_fmt.eps Change all the data conversion calls from parseInt() to parse
UnsignedInt(). In addition, you must change String.valueOf
(Convert) to read Integer.toUnsignedString(Convert). Perform Steps 3 through 10 again to see how the behavior of the application changes. Try inputting a negative number to see what happens.

Finding Information in Strings

Strings are extremely flexible and can contain a lot of information. That makes working with the String type different from other data types you use because a String can actually contain so much information that it can be hard to find exactly the bit of information you want. Fortunately, the String data type also comes with a wealth of methods you can use to locate specific bits of information. The following sections describe these methods and show you how to use them.

Looking at the beginning or end of the string

Sometimes it’s helpful to look at the beginning or end of a string for specific information. You may not care what the center of the string contains; only that it starts (or ends) the right way. In the following example, you see how to look for specific information at the beginning and end of a string.

Files needed: StartAndEnd.java

1. Open the editor or IDE that you’ve chosen to use for this book.

2. Type the following code into the editor screen.

// Import the required API classes.
import java.util.Scanner;
import java.lang.String;

public class StartAndEnd
{
public static void main(String[] args)
{
// Create a variable to hold the input.
String Input = "";

// Create the scanner.
Scanner GetInput = new Scanner(System.in);

// Obtain a string for processing.
System.out.print("Type the secret string: ");
Input = GetInput.nextLine();

// Check the beginning and end of the string.
if ((Input.toUpperCase().startsWith("HELLO"))
&& (Input.toUpperCase().endsWith("WORLD")))
System.out.println("You typed the secret
string!");
else
System.out.println("Try again.");
}
}

The example asks for an input string from the user. It then converts this string to uppercase and looks at both the beginning and the end of the string for a specific value. In this case, the beginning of the string must contain the word HELLO, and the end of the string must contain WORLD. Using the toUpperCase() method ensures that the user can type the input in any case and still succeed. When the input doesn’t match, the application outputs an error message.

3. Save the file to disk using the filename StartAndEnd.java.

4. In your command prompt, type javac StartAndEnd.java and press Enter.

The compiler compiles your application and creates a .class file from it.

5. Type java StartAndEnd and press Enter.

The application asks the user to provide a string.

6. Type Some String and press Enter.

The application displays the expected error message.

7. Perform Steps 5 and 6 using Hello Beautiful World as an input value.

You see the success message, as shown in Figure 11-2. As you can see, the center of the string doesn’t matter.

9781118098783-fg1102.tif

Figure 11-2:

Working with substrings

Substrings are parts of a string. You choose a section of string at the beginning, middle, or end of a string and use it as a separate entity. Developers use substrings for all sorts of tasks. For example, many sorting routines are based on substrings, as are many search techniques. The following example shows a simple use of substrings.


LINGO

A substring is any set of contiguous characters found in a larger string. An application can take a series of contiguous characters out of the beginning, middle, or end of a string and use it as a substring. The characters in a substring are always contiguous — you can’t take a character here and a character there and call it a substring. For example, in the string “Hello World,” one potential substring is the word Hello.


Files needed: Substrings.java

1. Open the editor or IDE that you’ve chosen to use for this book.

2. Type the following code into the editor screen.

// Import the required API
// classes.
import java.util.Scanner;
import java.lang.String;

public class Substrings
{
public static void main(String[] args)
{
// Create a variable to hold the input.
String Input = "";

// Create the scanner.
Scanner GetInput = new Scanner(System.in);

// Obtain a string for processing.
System.out.print("Type the secret string: ");
Input = GetInput.nextLine();

// Look for the search string.
int Start = Input.toUpperCase().indexOf("HELLO");

// Determine whether the string exists.
if (Start != -1)
System.out.println(
"The string contains " +
Input.substring(Start, Start + 5) +
" at position: " + Start);
else
System.out.println("The string doesn't
contain HELLO.");
}
}

The example asks for an input string from the user. It then converts this string to uppercase and looks for the word HELLO anywhere in the string. The indexOf() method returns a number that specifies the location of HELLO in the string. If HELLO doesn’t appear in the string, thenindexOf() returns a value of –1. The if statement detects this value and provides the required output.

When the user has entered the word HELLO somewhere in the string, the application uses the substring() method to extract the word from Input. This example uses both a starting and an ending index. You can also call the substring() method with just a starting index, which returns the entire string from that point on.

3. Save the file to disk using the filename Substrings.java.

4. In your command prompt, type javac Substrings.java and press Enter.

The compiler compiles your application and creates a .class file from it.

5. Type java Substrings and press Enter.

The application asks the user to provide a string.

6. Type Hello World and press Enter.

The application displays the expected success message.

7. Perform Steps 5 and 6 using Yellow Hello Goodbye as an input value.

You see the success message again as shown in Figure 11-3. As you can see, the position of HELLO in the string doesn’t matter.

9781118098783-fg1103.tif

Figure 11-3:

Modifying String Content

In many cases, you need to change the content of a string in some way. For example, you might want to replace every occurrence of the letter T with a letter U. It’s also possible to remove characters and to split strings into pieces as needed. Some of these techniques see a lot of use in applications because they’re so handy. For example, you’ll find that sometimes you must split a path to a file on disk into its component parts in order to actually locate the file. The following sections explain how to modify string content.

Extracting characters

Users can input data in a range of formats. In some cases, you can prevent an error by extracting excess characters from the string. For example, when a user types 1,555 in place of 1555, the application can simply remove the comma (,) before it processes the input. The following example builds on the ConvertIntAndString example shown in the “Converting Data to and from Strings” section of the chapter to demonstrate character extraction techniques.

Files needed: ExtractCharacter.java

1. Open the editor or IDE that you’ve chosen to use for this book.

2. Type the following code into the editor screen.

// Import the required API classes.
import java.util.Scanner;
import java.lang.String;
import java.lang.Integer;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

public class ExtractCharacter
{
public static void main(String[] args)
{
// Create a variable to hold the input.
String Input = "";

// Create an intermediate variable.
int Convert = 0;

// Create a variable to hold the output.
String Output = "";

// Create the scanner.
Scanner GetInput = new Scanner(System.in);

// Obtain a string for processing.
System.out.print("Type any integer value: ");
Input = GetInput.nextLine();

// Obtain the thousands separator for the

 // current locale.

DecimalFormatSymbols DFS =

DecimalFormatSymbols.getInstance(Locale.
getDefault());
char Thousands = DFS.getGroupingSeparator();

// Remove the thousands separator from the

 // string.
String TempString = "";
for (int Index = 0; Index < Input.length(); Index++)
if (Input.charAt(Index) != Thousands)
TempString += Input.charAt(Index);

// Place the remainder in Input.
Input = TempString;

try
{
// Convert the String to an int.
Convert = Integer.parseInt(Input);
}
catch (NumberFormatException e)
{
// The user input an incorrect value.
System.out.println("Type an integer value.\n" +
e.getMessage());

// Exit the program.
return;
}

// Change the value of the input.
Convert += 5;

// Check for a number that's too large.
if (Convert < Integer.parseInt(Input))
{
System.out.println("The input number is too
large!");
return;
}

// Convert the int to a String.
Output = String.valueOf(Convert);

// Display the result.
System.out.printf("%s + 5 = %s%n", Input, Output);
}
}

This example works much the same as the ConvertIntAndString example. However, notice that after the application obtains input from the user, it begins a process of checking for the thousands separator that the user could have added between groups of numbers. The DFS, of typeDecimalFormatSymbols, provides access to a number of formatting symbols, including the thousands separator obtained with a call to getGroupingSeparator().

remember.eps Whenever you work with potentially regional information, such as a thousands separator, make sure you obtain the correct information for the desired locale. In this case, the application obtains the default locale for the system by calling Locale.getDefault(). However, you can also obtain specific locales as needed by accessing other members of the Locale class.

Now that the application knows which character represents a thousands separator, it uses a for loop to remove this character from Input. The code relies on a temporary String, TempString to aid in the task. The actual character extraction is performed using the Input.charAt()method, which allows the loop to examine one character at a time in the string. Whenever the current character doesn’t equal the value in Thousands (in other words, it isn’t a thousands character), it gets added to TempString. Of course, you could use the same method to perform any sort of extraction.

3. Save the file to disk using the filename ExtractCharacter.java.

4. In your command prompt, type javac ExtractCharacter.java and press Enter.

The compiler compiles your application and creates a .class file from it.

5. Type java ExtractCharacter and press Enter.

The application asks the user to provide a numeric input.

6. Type 1,555,212 and press Enter.

You see the expected output shown in Figure 11-4.

9781118098783-fg1104.tif

Figure 11-4:

Splitting strings

Splitting strings using a particular separator character, such as a space or a slash, is a common practice in Java programming. Developers often receive strings that contain a number of pieces of information that are combined in some way. Using splitting techniques lets the developer work with individual string pieces to coax the information needed by the application out of the string. The following example demonstrates how to use string splitting.

Files needed: Splitter.java

1. Open the editor or IDE that you’ve chosen to use for this book.

2. Type the following code into the editor screen.

// Import the required API classes.
import java.util.Scanner;
import java.lang.String;

public class Splitter
{
public static void main(String[] args)
{
// Create a variable to hold the input.
String Input = "";

// Create the scanner.
Scanner GetInput = new Scanner(System.in);

// Obtain a string for processing.
System.out.print("Type a series of words: ");
Input = GetInput.nextLine();

// Split the string into pieces.
String[] Output = Input.split(" ");

// Display the strings on screen.
for (String Item : Output)
System.out.println(Item);
}
}

The example begins by obtaining a series of words from the user. Actually, any series of characters of any type separated by spaces will do. The code then calls the split() method to divide the individual words in the string into a String array, Output. At this point, the code can output the separate words using a for-each loop.

3. Save the file to disk using the filename Splitter.java.

4. In your command prompt, type javac Splitter.java and press Enter.

The compiler compiles your application and creates a .class file from it.

5. Type java Splitter and press Enter.

The application asks the user to provide a string.

6. Type This is a string to split. and press Enter.

You see the string split into separate words, as shown in Figure 11-5.

9781118098783-fg1105.tif

Figure 11-5:

Formatting String Output

The ConvertIntAndString example discussed in the “Converting Data to and from Strings” section of the chapter lacked some features. The ExtractCharacter example added the ability to type numbers that included thousands separators. The following example completes the application by providing nicely formatted output.

Files needed: FormatNumbers.java

1. Open the editor or IDE that you’ve chosen to use for this book.

2. Type the following code into the editor screen.

// Import the required API classes.
import java.util.Scanner;
import java.lang.String;
import java.lang.Integer;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

public class FormatNumbers
{
public static void main(String[] args)
{
// Create a variable to hold the input.
String Input = "";

// Create an intermediate variable.
int Convert = 0;

// Create a variable to hold the output.
String Output = "";

// Create the scanner.
Scanner GetInput = new Scanner(System.in);

// Obtain a string for processing.
System.out.print("Type any integer value: ");
Input = GetInput.nextLine();

// Obtain the thousands separator for the

 // current locale.

DecimalFormatSymbols DFS =

DecimalFormatSymbols.getInstance(Locale.
getDefault());
char Thousands = DFS.getGroupingSeparator();

// Remove the thousands separator from the 

// string.
String TempString = "";
for (int Index = 0; Index < Input.length(); Index++)
if (Input.charAt(Index) != Thousands)
TempString += Input.charAt(Index);

// Place the remainder in Input.
Input = TempString;

try
{
// Convert the String to an int.
Convert = Integer.parseInt(Input);
}
catch (NumberFormatException e)
{
// The user input an incorrect value.
System.out.println("Type an integer value.\n" +
e.getMessage());

// Exit the program.
return;
}

// Change the value of the input.
Convert += 5;

// Check for a number that's too large.
if (Convert < Integer.parseInt(Input))
{
System.out.println("The input number is too
large!");
return;
}

// Convert the int to a String.
Output = String.valueOf(Convert);

// Add the thousands separators to Input.
int Compare = (Input.length() - 1) % 3;
TempString = "";
for (int Index = Input.length() - 1; Index >= 0; Index--)
if (((Index % 3) == Compare) && (Index != Input.length() - 1))
TempString = Input.charAt(Index) +
String.valueOf(Thousands) + TempString;
else
TempString = Input.charAt(Index) +
TempString;
Input = TempString;

// Add the thousands separators to Output.
Compare = (Output.length() - 1) % 3;
TempString = "";
for (int Index = Output.length() - 1; Index >= 0;
Index--)
if (((Index % 3) == Compare) && (Index !=
Output.length() - 1))
TempString = Output.charAt(Index) +
String.valueOf(Thousands) + TempString;
else
TempString = Output.charAt(Index) +
TempString;
Output = TempString;

// Display the result.
System.out.printf("%s + 5 = %s%n", Input, Output);
}
}

This example adds on to the ExtractCharacter example. In order to provide formatted output, you need to know which thousands separator character to use. Early in the code, the application uses the DecimalFormatSymbols object, DFS, to store the thousands separator in the charvariable, Thousands, by calling getGroupingSeparator().

The code used to format a string with thousands separators appears near the end of the code. It begins by creating an int variable named Compare that holds a comparison value. This variable tells Java when to place a comma in the output string. Notice that this is one of the times when you must use the modulus operator (%). Suppose that the user types four characters as numbers, which would make the output of Input.length() 4. Subtract 1 from this value, and the value is now 3. When you divide that value by 3, the remainder is 0, which is what’s stored inCompare.

Formatting a string this way means working backward through the string. Some formatting simply works that way. You count off three numbers from the right and add a comma before you start the next series of three numbers, working right to left as you go. It helps to think about how you perform this specific task. In this case, the for loop begins at the right end of the string and progresses toward the left end, just as you’d perform the task.

Every time the for loop processes another character, it must make a decision about inserting a thousands separator. If the code is already looking at the right end of the string, there isn’t any reason to add a thousands separator, which is what the (Index != Input.length() - 1) part of the if statement is all about.

Figuring out when you’re at the third character from the right is a little harder. Index starts at the length of the string minus 1. So, when you have a string with four characters, Index starts with a value of 3. Dividing 3 by a value of 3 leaves a remainder of 0, which is the same number that was placed in Compare. Only the second part of the if statement makes this first pass through the for loop false. Three characters later, Index has a value of 0, which, when divided by 3, produces a remainder of 0, which again equals Compare. This time the code adds a thousands separator because the code isn’t looking at the right end of the string.

There’s one last quirk to notice in this example. Instead of using Thousands directly, the code uses String.valueOf(Thousands). Remember that Thousands is a char and so is the output of Input.charAt(Index). If you add these two values together, Java will add their values, rather than concatenate them as strings.

The loop for processing Output is the same as the loop for processing Input. Consequently, you could create a method from this code and call it from main() as needed. The example uses this approach for the sake of clarity.

3. Save the file to disk using the filename FormatNumbers.java.

4. In your command prompt, type javac FormatNumbers.java and press Enter.

The compiler compiles your application and creates a .class file from it.

5. Type java FormatNumbers and press Enter.

The application asks the user to provide a numeric input.

6. Type 1,555,212 and press Enter.

You see the expected output shown in Figure 11-6.

9781118098783-fg1106.tif

Figure 11-6:

summingup_fmt.eps Summing Up

Here are the key points you learned about in this chapter:

· Converting data from one type to another means changing the actual presentation and storage of the data, rather than casting it as a larger form of the same type.

· The String type provides a number of ways to find information, including looking at a particular part of the string, comparing two strings, and working with substrings.

· Modifying a String includes replacing string elements, extracting characters from the string, and splitting the string into pieces.

· Formatting a String makes it more presentable to the user and also makes your application friendlier by displaying data in a form the user understands.

Try-it-yourself lab

For more practice with the features covered in this chapter, try the following exercise on your own.

1. Open the FormatNumbers.java file supplied with the source code for this book.

2. Save the file as FormatNumbers2.java.

3. Change the class name to FormatNumbers2.

The class name must always match the filename. Otherwise, Java won’t compile the file for you.

4. Create a method called FormatThousands().

5. Create a generic form of the code used to add the thousands separators to the Input and Output strings.

This step means adding input arguments and adding a return value to the FormatThousands() method.

6. Remove the specific code used to add the thousands separators from the example.

7. Add calls to the FormatThousands() method as needed.

8. Compile the application.

9. Run the application.

10. Provide the input required by the prompt.

Does the application output the correct results? Does using the FormatThousands() method make the code easier to understand?

Know this tech talk

· data conversion: A method of converting data between unlike types, such as String and int. When performing data conversion, Java must actually change the representation of the data so that it fits into the new type.

· locale: The characteristics of application output that define a particular language. For example, some languages use a period for the decimal marker, while others use a comma. Formatting data with locale in mind makes it easier for people who speak other languages to use it.

· parse: The act of breaking up a string into pieces called tokens, normally at spaces, and then analyzing each token for specific information. For example, “Java is a great language.” has five tokens, “Java”, “is”, “a”, “great”, and “language.”

· substring: A string of characters taken from the beginning, middle, or end of a string and used as a separate entity in an application.

· token: A single identifiable piece of information within a string. Generally, the sentence is separated by spaces into tokens, so the sentence “Sunny days are nice!” has four tokens in it, “Sunny”, “days”, “are”, and “nice!”.