working with strings
play

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


  1. 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.

  2. String Examples String example; example = "Hello"; example = "123456"; example = "123 Brookfield Road"; example = "9 + 3 * 4 – 12 / 6";

  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 ).

  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.

  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);

  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);

  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

  8. 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."

  9. 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());

  10. 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"

  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

  12. 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend