Java Strings Slides provided by the University of Washington Computer Science & Engineering department.

Slides:



Advertisements
Similar presentations
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
Advertisements

Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
String class  Construct a string  String str = new String(“welcome”);  Char[] charr = {‘G’, ‘o’, ‘o’, ‘d’};  String mes = new String(charr);  A full.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.
Topic 13 procedural design and Strings Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Copyright 2006 by Pearson Education 1 Building Java Programs Chapters 3-4: Using Objects.
String Class in Java java.lang Class String java.lang.Object java.lang.String java.lang.Object We do not have to import the String class since it comes.
Strings.
Objects and Classes; Strings. 2 Classes and objects class: A program entity that represents either 1.A program / module, or 2.A type of objects* –A class.
From C++ to Java A whirlwind tour of Java for C++ programmers.
Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE STRING CLASS.
Strings Mr. Smith AP Computer Science A. What are Strings? Name some of the characteristics of strings: A string is a sequence of characters, such as.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE STRING CLASS.
CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
String Class in Java java.lang Class String java.lang.Object java.lang.String java.lang.Object We do not have to import the String class since it comes.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
CHAPTER 6 GC Strings. THE CLASS STRING  Contains operations to manipulate strings.  String:  Sequence of zero or more characters.  Enclosed.
Copyright 2010 by Pearson Education Building Java Programs Chapter 4 Lecture 4-3: Strings, char reading: 3.3, 4.3.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
1 reading: 3.3 Using objects. Objects So far, we have seen: methods, which represent behavior variables, which represent data (categorized by types) It.
Chapter 8 String Manipulation
© A+ Computer Science - Magpie Magpie is a lab that focuses on classes, randomness, and Strings. This lab will make sure that you.
The Methods and What You Need to Know for the AP Exam
Introduction to programming in java
Information and Computer Sciences University of Hawaii, Manoa
Lecture 24: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Lecture 8: The String class
© A+ Computer Science - Magpie Chatbot Lab © A+ Computer Science -
Building Java Programs
CSCI 161 – Introduction to Programming I William Killian
Building Java Programs
Chapter 6 GC 101 Strings.
Programming in Java Text Books :
Topic 13 procedural design and Strings
Building Java Programs
CS 106A, Lecture 9 Problem-Solving with Strings
Chapter 7: Strings and Characters
Adapted from slides by Marty Stepp, Stuart Reges & Allison Obourn.
String Methods: length substring
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2018 Lecture 14: Booleans and Strings
CMSC 202 Java Primer 2.
Lecture 8: The String Class and Boolean Zen
Conditional Logic Presentation Name Course Name
Control Structure Chapter 3.
Recursion Problems.
Lecture 14: Strings and Recursion AP Computer Science Principles
Lecture 19: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Building Java Programs
Using Objects (continued)
Introduction to Computer Science
String methods 26-Apr-19.
Strings in Java.
Building Java Programs
Control Structure.
In Java, strings are objects that belong to class java.lang.String .
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 1 2 3 4 character A p l e First character’s index: 0 Last character’s index: 1 less than the string’s length

String methods Method name Description int length() number of characters in this string String substring(int from, int to) returns the substring beginning at from and ending at to-1 String substring(int from) returns substring(from, length()) int indexOf(String str) returns the index of the first occurrence of str; returns -1 if not found int compareTo(String other) 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

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 PROBLEM: 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. EXAMPLES: numWords("how many?") Returns 2 numWords(" how about a-b-c and !&%-$!*() ") Returns 5

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;