SLIDE 1
Working with Strings Data Types: A string is a collection of one or - - PowerPoint PPT Presentation
Working with Strings Data Types: A string is a collection of one or - - PowerPoint PPT Presentation
Working with Strings Data Types: A string is a collection of one or more characters that can be output (printed) or input (via the keyboard). Although we typically think of strings as names, words, and punctuation, they can also refer to
SLIDE 2
SLIDE 3
Strings as Arrays
Java has several primitive data types, such as int, double, char, and boolean. Strings (data type String) are not a primitive data type. They are actually a data structure provided as part of the Java language. In many ways, a String is like an array of characters (data type char).
SLIDE 4
Strings as Arrays
Some (older) programming languages do not include a String data type. Instead, the programmer (or community) creates an array of characters:
char[] wordArray = {'h','e','l','l','o'}; String word = "hello";
Java also provides a number of methods for dealing with Strings, and it is useful to remember the array of characters to help understand them.
SLIDE 5
Strings – Length
The ability to convert any data type to a string has
- advantages. It is possible to manipulate strings
character by character. This can be useful with problems involving words as well as just numbers.
String quote = "To be or not to be"; int quoteLen = quote.length(); // output that it is 18 characters long println(quote + " : " + quoteLen);
SLIDE 6
Strings – Display an Index Location
If the String is an array, we should be able to access individual elements at a particular index.
String word = "hello"; char ch; ch = word.charAt(1); // the letter 'e' System.out.println(ch);
SLIDE 7
Strings – Looking for a Character
It is possible to determine the index of a particular character (remember to start counting from zero) in a string. If there are multiple characters, the location of the first is the result. If the character is not found, the result is -1.
String word = "hello"; int location; location = word.indexOf('l'); System.out.println(location); // 2
SLIDE 8
Strings – Accessing Substrings
A substring is a String (not char) data type, and is
- ne or more symbols from the String. The default
use returns the substring from the specified index to the end of the string."
String quote = "To be or not to be."; //0123456789012345678 String sub = quote.substring(6); println(sub); // outputs "or not to be." println(quote.substring(16)); // "be."
SLIDE 9
Strings – Accessing Substrings
It is also possible to specify both the start and end
- f the substring, but the format can be confusing
(but it does make sense).
String quote = "To be or not to be."; //0123456789012345678 String sub = quote.substring(6,19); // sub is "or not to be." // use 19 because string is 19 long sub = quote.substring(6,quote.length());
SLIDE 10
Strings – Accessing Substrings
It is also possible to specify both the start and end
- f the substring, but the format can be confusing
(but it does make sense).
String sub; String quote = "To be or not to be."; //0123456789012345678 sub = quote.substring(6,8); // "or" sub = quote.substring(9,12); // "not"
SLIDE 11
Strings – Looking for a Substring
It turns out that the indexOf() method will accept a String parameter as well, which allows for searching for the index of a (multi-character) string:
String word = "hello"; // 01234 println(word.indexOf('l')); // 2 println(word.indexOf("l")); // 2 println(word.indexOf("ell")); // 1
SLIDE 12