Java Strings
In this set of Java slides, explore the fundamentals of working with strings, including creating strings, understanding string indexes, common string methods, and using the equals method for string comparison. Dive into examples and learn how to manipulate strings effectively in Java programming.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Java Strings Slides provided by the University of Washington Computer Science & Engineering department.
Strings - string: an object storing a sequence of text characters. - Creating a string: String name = "text"; String name = expression; - Examples: String name = "Marty Stepp"; int x = 3; int y = 5; String point = "(" + x + ", " + y + ")";
String indexes - Characters of a string are numbered with 0-based indexes: String fruit = "Apple"; index 0 1 2 3 4 character A p p l e - First character s index: 0 - Last character s index: 1 less than the string s length
String methods Method name Description number of characters in this string int length() returns the substring beginning at from and ending at to-1 String substring(int from, int to) returns substring(from, length()) String substring(int from) returns the index of the first occurrence of str; returns -1 if not found int indexOf(String str) returns a value < 0 if this is less than other returns a value = 0 if this is equal to other returns a value > 0 if this is greater than other int compareTo(String other)
String methods - These methods are called using the dot notation: String veggie = "carrot"; System.out.println(veggie.length()); // 6 - More examples: // index 012345678901 String s1 = "Stuart Reges"; System.out.println(s1.length()); // 12 System.out.println(s1.indexOf("e")); // 8 System.out.println(s1.substring(7, 10)); // "Reg"
String methods - Given the following string: String message = "Hello, world!"; - How would you extract the word world ?
The equals method - Objects are compared using a method named equals. if (string1.equals(string2)) { System.out.println("The strings are equal!"); } - Technically this is a method that returns a value of type boolean, the type used in logical tests.
String question Write a static method numWords that takes a String as a parameter and that returns the number of words in the String. By definition, words are separated by one or more spaces. PROBLEM: EXAMPLES: Returns 2 numWords("how many?") Returns 5 numWords(" how about a-b-c and !&%-$!*() ")
numWords solution 1 - Here s one way to solve the problem: public static int numWords(String s) { int count = 0; boolean insideWord = false; for (int i = 0; i < s.length(); i++) { String current = s.substring(i, i + 1); if (current.equals(" ")) { insideWord = false; } else if (!insideWord) { count++; insideWord = true; } } return count; }
numWords solution 2 - Here s another way to solve the problem: public static int numWords(String s) { int count = 0; String prev = " "; for (int i = 0; i < s.length(); i++) { String current = s.substring(i, i + 1); if (prev.equals(" ") && !current.equals(" ")) { count++; } prev = current; } return count; }