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 numbers, mathematical symbols, or other special characters.
String Examples String example; example = "Hello"; example = "123456"; example = "123 Brookfield Road"; example = "9 + 3 * 4 – 12 / 6";
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 ).
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.
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);
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);
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
Strings – Accessing Substrings A substring is a String (not char) data type, and is one 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."
Strings – Accessing Substrings It is also possible to specify both the start and end of 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());
Strings – Accessing Substrings It is also possible to specify both the start and end of 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"
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
Strings – Joining Strings Together (Concatenation) We have already output strings together using the put command. In order to combine strings and save the result in a variable, use the '+' operation to concatenate the strings. quote1 := “To be or not to be,” quote2 := “ that is the question.” quote3 := quote1.concat(quote2); % variable 'quote3' now has full quote
Recommend
More recommend