Searching and Strings. IST 256 Application Programming for Information Systems

Similar documents
CS251L REVIEW Derek Trumbo UNM

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Java Classes and Objects

Java s String Class. in simplest form, just quoted text. used as parameters to. "This is a string" "So is this" "hi"

Programming with Java

CSC Java Programming, Fall Java Data Types and Control Constructs

Intro to Strings. Lecture 7 CGS 3416 Spring February 13, Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, / 16

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

STRINGS AND STRINGBUILDERS. Spring 2019

- Thus there is a String class (a large class)

5/23/2015. Core Java Syllabus. VikRam ShaRma

String. Other languages that implement strings as character arrays

Exceptions and Design

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Key Differences Between Python and Java

More on Strings. Lecture 10 CGS 3416 Fall October 13, 2015

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Introductory Mobile Application Development

CS 302: Introduction to Programming in Java. Lecture 12

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

CITS1001 week 6 Libraries

Programming Language 2 (PL2)

PROGRAMMING FUNDAMENTALS

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 Advance mode: Auto

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

Java Fall 2018 Margaret Reid-Miller

SELECTION. (Chapter 2)

CMSC 341 Lecture 7 Lists

Comparing Objects 3/28/16

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

CS 101 Fall 2006 Midterm 1 Name: ID:

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Index COPYRIGHTED MATERIAL

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

CMPSCI 187: Programming With Data Structures. Lecture #21: Array-Based Lists David Mix Barrington 26 October 2012

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Character Strings COSC Yves Lespérance. Lecture Notes Week 6 Java Strings

Java Identifiers, Data Types & Variables

Javascript Methods. concat Method (Array) concat Method (String) charat Method (String)

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Lab 14 & 15: String Handling

Problem 1: Building and testing your own linked indexed list

Overview. Iteration 4 kinds of loops. Infinite Loops. for while do while foreach

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Main loop structure. A Technical Support System. The exit condition. Main loop body

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Chapter Ten String Manipulation and Menus

Announcements/Follow-ups

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

AP Computer Science A

QUEEN MARY, UNIVERSITY OF LONDON DCS128 ALGORITHMS AND DATA STRUCTURES Class Test Monday 13 th February

Handout 3 cs180 - Programming Fundamentals Fall 17 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

CSE 142 Su 04 Computer Programming 1 - Java. Objects

If (Condition) Then. Statements executed if condition is true Else If Statements executed else if condition is false Else

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Intro to Strings. Lecture 7 COP 3252 Summer May 23, 2017

CS 106 Introduction to Computer Science I

Using Java Classes Fall 2018 Margaret Reid-Miller

Model Solutions. COMP 102: Test March, 2015

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

CGS 3066: Spring 2015 JavaScript Reference

Comments in a Java Program. Java Overview. Identifiers. Identifier Conventions. Primitive Data Types and Declaring Variables

Week 4. Strings, if/else, return, user input

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Mastering Modern Linux by Paul S. Wang Appendix: Pattern Processing with awk

Class Library java.lang Package. Bok, Jong Soon

EXAM Computer Science 1 Part 1

Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

Following is the general form of a typical decision making structure found in most of the programming languages:

CST242 Strings and Characters Page 1

ECE 122. Engineering Problem Solving with Java

COMP-202 Unit 4: Programming with Iterations

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Quiz 1. In class on Tuesday (April 14) Review learning goals (from lecture) Review lectures 1 4, PSA 1 You should be able to:

Lab5. Wooseok Kim

DECISION MAKING STATEMENTS

Chapter 10: Text Processing and More about Wrapper Classes

Arrays, Strings and Collections

CompSci 125 Lecture 11

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers-Weeda Sjaak Smetsers Ana Tanase

Announcements. Representation Invariants and Abstraction Functions. Announcements. Outline

Lec 3. Compilers, Debugging, Hello World, and Variables

McGill University School of Computer Science COMP-202A Introduction to Computing 1

ICSE Class 10 Computer Applications ( Java ) 2014 Solved Question Paper

Language Reference Manual

USING LIBRARY CLASSES

AP CS Fall Semester Final

Iteration and Arrays Dr. Abdallah Mohamed

Transcription:

Searching and Strings IST 256 Application Programming for Information Systems

Searching for Strings In an array, we do a simple linear search for an item by going through the array in order from the first and comparing each item to the search string. String [ ] names = new String [20]; String searchname = ; // get a value to search for for (int i = 0; i < names.length; i ++) { if ( names[i].equals(searchname)) { //found one! 2

Searching for Strings If we want to find only one (the first one), we can save it and exit from the loop: String [ ] names = new String [20]; String searchname = ; // get a value to search for String resultname = ; for (int i = 0; i < names.length; i ++) { if ( names[i].equals(searchname)) { resultname = names[i]; break; // display the result break causes the program to break out of the loop; immediately jump to the end 3

Searching for Strings But if we don t find one, we can display that, too: String [ ] names = new String [20]; String searchname = ; // get a value to search for String resultname = ; for (int i = 0; i < names.length; i ++) { if ( names[i].equals(searchname)) { resultname = names[i]; break; // if still empty result, then it was not found if (resultname.equals( ) { resultname = No match found. // display resultname 4

Searching for Strings If there s more than one match, we can find them all: String [ ] names = new String [20]; String searchname = ; // get a value to search for String resultheader = <html>result: <p>: ; String resultnames = ; for (int i = 0; i < names.length; i ++) { if ( names[i].equals(searchname)) { resultnames = resultnames + <p> + names[i]; // if still empty result, then none was found if (resultnames.equals( ) { resultnames = No match found. // display resultheader + resultnames 5

Search Criteria So far, we have used an exact string match with the string function equals names[i].equals(searchname) Suppose that we want to find names where the search name is the start of the name (e.g. Alex Brown and we search for Al or Alex ) names[i].startswith(searchname) uses the String function startswith 6

Search Criteria Or suppose that we want the strings to match regardless of capitalization. This is called ignoring case, and there is another equals method that will ignore case in doing the comparison: names[i].equalsignorecase(searchname) For other comparisons, such as startswith(), use the.tolowercase() method to convert both strings to lower case and then compare them. names[i].tolowercase().startswith( searchname.tolowercase()) 7

Search Criteria Or suppose that we want to find the search string as a substring of the name, anywhere that it occurs. There is a String function contains that tests if a substring occurs anywhere in the string names[i].contains(searchname) 8

Search Criteria Here is an alternate way to find if the search name is a substring of the name. There is a String function indexof that finds a substring and returns an integer that is the number of the character where the substring occurs (starting from 0) if searchname is Alex, searchname.indexof( ex ) returns the integer 2, the position where ex is in Alex But if the substring is not found, indexof returns -1, so our search criteria becomes names[i].indexof(searchname)!= -1 9

Other String Methods Other useful functions include: endswith() lastindexof() touppercase() substring(startindex, stopindex) returns the substring that starts at the character at startindex and ends one character before the stopindex. compareto(comparestring) is True if the String is less than the comparestring and false otherwise. This is useful for sorting arrays in alphabetic order by name. 10

Finding more String methods A complete list is found in the API under the String class: http://docs.oracle.com/javase/7/docs/api/ Where you scroll down to find the String class in the lower left pane And note that String implements the CharSequence class, so you are allowed to pass a String to any method that asks for a CharSequence parameter. 11

Digression on primitive types vs. classes In Java, primitive types (int, double, boolean) use operators, while classes have fields and methods. But arrays and Strings have a mix: Primitive types: int, double, boolean Operators Yes +, -, *, /, &&, Use new constructor Use fields and methods No No Arrays Indexing: [ ] Mostly: new int [ ].length Strings Catenation: + No Mostly:.equals, indexof, etc. Everything else No Yes Yes 12