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

Similar documents
String. Other languages that implement strings as character arrays

AP Computer Science. Strings. Credit: Slides are modified with permission from Barry Wittman at Elizabethtown College

Variable initialization and assignment

Lab 14 & 15: String Handling

Selected Questions from by Nageshwara Rao

6.092: Java for 6.170

TEST (MODULE:- 1 and 2)

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

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

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

The data in the table are arranged into 12 rows and 12 columns. The process of printing them out can be expressed in a pseudocode algorithm as

PROGRAMMING FUNDAMENTALS

Netbeans tutorial:

Ohad Barzilay and Oranit Dror

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Java Foundations: Unit 3. Parts of a Java Program

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

CS 177 Spring 2009 Exam I

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

CSE1710. Big Picture. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture.

Honors Computer Programming 1-2. Chapter 5 Exam Prep

CS1150 Principles of Computer Science Math Functions, Characters and Strings (Part II)

Class. Chapter 6: Data Abstraction. Example. Class

COMP6700/2140 Data and Types

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

STUDENT LESSON A10 The String Class

J.43 The length field of an array object makes the length of the array available. J.44 ARRAYS

Tasks for fmri-setting (Tasks of first and second pilot study at the end)

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

Key Points. COSC 123 Computer Creativity. Java Decisions and Loops. Making Decisions Performing Comparisons. Making Decisions

Building Strings and Exploring String Class:

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

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

*Java has included a feature that simplifies the creation of

Lecture Notes K.Yellaswamy Assistant Professor K L University

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

COSC 123 Computer Creativity. Java Decisions and Loops. Dr. Ramon Lawrence University of British Columbia Okanagan

"Hello" " This " + "is String " + "concatenation"

Strings, Strings and characters, String class methods. JAVA Standard Edition

CS 1301 Ch 8, Part A

Chapter 4: Control Structures I

Review Ch 5 static Multiple Choice Test Creating Class Methods

Programming with Java

Chapter 29B: More about Strings Bradley Kjell (Revised 06/12/2008)

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

Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures

2. All the strings gets collected in a special memory are for Strings called " String constant pool".

Eng. Mohammed Abdualal

Creating Strings. String Length

CS Week 5. Jim Williams, PhD

Java Programming for Selenium

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

TUGCE KEMEROZ - ASLI OZKAN - AYSE TARTAN. Week 12/02/ /02/2007 Lecture Notes:

Array. Array Declaration:

Strings! Today! CSE String Methods!

Object-Oriented Concepts

In this chapter, you will:

Java Programming. String Processing. 1 Copyright 2013, Oracle and/or its affiliates. All rights reserved.

Getting started with Java

Introduction to Programming Using Java (98-388)

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

You ll be reading more about tools for branching in Sections 3.4, and We are skipping Sections 3.5, 3.11 and 3.13-end of chapter.

Strings and Arrays. Hendrik Speleers

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

In Java there are three types of data values:

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

Manual for Basic Java

BM214E Object Oriented Programming Lecture 8

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

Full file at

Building Java Programs

Basic Computation. Chapter 2

Chapter 7 User-Defined Methods. Chapter Objectives

Searching and Strings. IST 256 Application Programming for Information Systems

Introduction to Programming (Java) 4/12

Controls Structure for Repetition

Chapter 4 Arrays & Strings

Building Java Programs

Programming with Java

AP Computer Science Unit 1. Programs

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

Introduction to Java Unit 1. Using BlueJ to Write Programs

More on Strings. String methods and equality. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Compu<ng and Mathema<cs h=p://

COMP 110/L Lecture 7. Kyle Dewey

Final Exam COMP Fall 2004 Dec 16, 2004

Java Basic Programming Constructs

Join the course group CS230-S18! Post questions, answer them if you know the answer!

STRINGS AND STRINGBUILDERS. Spring 2019

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Announcements. Lab 03 inbase late submissions Midterm #1 Friday. Project 2 is posted. Covers everything through basics of OOP

1. Introduction to Java

CS171:Introduction to Computer Science II

appreciate the difference between a char and a string understand and use the String class methods

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

Interfaces, collections and comparisons

Advanced Systems Programming

CS 106 Introduction to Computer Science I

MODULE 02: BASIC COMPUTATION IN JAVA

Transcription:

Strings

- Strings in Java are objects - Thus there is a String class (a large class) - In a statement like this: System.out.println( Hello World ); the Java compiler creates a String object from the quoted text - Example string creation: String MyStr1 = new String( Hello World ); - String objects can be used anywhere a quoted string is allowed. for example: System.out.println( MyStr1 );

Example Declare Strings in Several Ways class StringDemo { public static void main(string args[]) { String mystr1 = new String("Java strings are objects."); String mystr2 = "They are constructed in various ways."; String mystr3 = new String(mystr2); System.out.println(mystr1); System.out.println(mystr2); System.out.println(mystr3); } }

Example Declare Strings in Several Ways class StringDemo { public static void main(string args[]) { String mystr1 = new String("Java strings are objects."); String mystr2 = "They are constructed in various ways."; String mystr3 = new String(mystr2); System.out.println(mystr1); System.out.println(mystr2); System.out.println(mystr3); } } Java strings are objects. They are constructed in various ways. They are constructed in various ways.

// String method operation: length( ) class StrOps { public static void main(string args[]) { String str1 ="When it comes to Web programming, Java is #1."; String str2 = new String(str1); String str3 = "Java strings are powerful."; int result, idx; char ch; System.out.println("Length of str1: " + str1.length( ) );

// String method operation: length( ) class StrOps { public static void main(string args[]) { String str1 ="When it comes to Web programming, Java is #1."; String str2 = new String(str1); String str3 = "Java strings are powerful."; int result, idx; char ch; System.out.println("Length of str1: " + str1.length( ) ); Length of str1: 45

// String method operation: equals( ) class StrOps { public static void main(string args[]) { String str1 ="When it comes to Web programming, Java is #1."; String str2 = new String(str1); String str3 = "Java strings are powerful."; int result, idx; char ch; if (str1.equals(str2) ) System.out.println("str1 equals str2"); else System.out.println("str1 does not equal str2");

// String method operation: equals( ) class StrOps { public static void main(string args[]) { String str1 ="When it comes to Web programming, Java is #1."; String str2 = new String(str1); String str3 = "Java strings are powerful."; int result, idx; char ch; if (str1.equals(str2) ) System.out.println("str1 equals str2"); else System.out.println("str1 does not equal str2"); str1 does not equal str2

// String method operation: equals( ) class StrOps { public static void main(string args[]) { String str1 ="When it comes to Web programming, Java is #1."; String str2 = new String(str1); String str3 = "Java strings are powerful."; int result, idx; char ch; if (str1.equals(str2) ) System.out.println("str1 equals str2"); else System.out.println("str1 does not equal str2"); Why use method equals( ) rather than if (str1 == str2)??

// String method operation: compareto( ) class StrOps { public static void main(string args[]) { String str1 ="When it comes to Web programming, Java is #1."; String str2 = new String(str1); String str3 = "Java strings are powerful."; int result, idx; char ch; result = str1.compareto(str3); if(result == 0) System.out.println("str1 and str3 are equal"); else if(result < 0) System.out.println("str1 is less than str3"); else System.out.println("str1 is greater than str3");

// String method operation: compareto( ) class StrOps { public static void main(string args[]) { String str1 ="When it comes to Web programming, Java is #1."; String str2 = new String(str1); String str3 = "Java strings are powerful."; int result, idx; char ch; result = str1.compareto(str3); if(result == 0) System.out.println("str1 and str3 are equal"); else if(result < 0) System.out.println("str1 is less than str3"); else System.out.println("str1 is greater than str3"); str1 is greater than str3

// String method operation: indexof( ) // Create a new string String str4 = "One Two Three One"; 0 14 idx = str4.indexof("one"); System.out.println("Index of first occurence of One: " + idx);

// String method operation: indexof( ) // Create a new string String str4 = "One Two Three One"; 0 14 idx = str4.indexof("one"); System.out.println("Index of first occurence of One: " + idx); Index of first occurence of One: 0

// String method operation: lastindexof( ) // Create a string String str4 = "One Two Three One"; 0 14 idx = str4.lastindexof("one"); System.out.println("Index of last occurence of One: " + idx);

// String method operation: lastindexof( ) // Create a string String str4 = "One Two Three One"; 0 14 idx = str4.lastindexof("one"); System.out.println("Index of last occurence of One: " + idx); Index of last occurence of One: 14

Strings are Immutable Once created, the character sequence that makes up the string cannot be changed. But that is not a problem. New strings can be created from old. Also, another class is StringBuffer (a topic for another day)

Strings can be concatenated together to form a new string String str1 = One ; String str2 = Two ; String str3 = Three ; String str4 = str1 + str2 + str3; System.out.println(str4);

Strings can be concatenated together to form a new string String str1 = One ; String str2 = Two ; String str3 = Three ; String str4 = str1 + str2 + str3; System.out.println(str4); OneTwoThree

The substring method substring(int startindex, int stopindex) class SubStr { public static void main(string args[]) { String origstr = "Java makes the Web move."; // construct a substring String substr = origstr.substring(5, 18); System.out.println("origstr: " + origstr); System.out.println("substr: " + substr); } }

The substring method substring(int startindex, int stopindex) class SubStr { public static void main(string args[]) { String origstr = "Java makes the Web move."; // construct a substring String substr = origstr.substring(5, 18); System.out.println("origstr: " + origstr); System.out.println("substr: " + substr); } } origstr: Java makes the Web move. substr: makes the Web

A string can be used to control a switch statement String command = cancel ; switch(command) { case connect : System.out.println( Connecting ); break; case cancel : System.out.println( Canceling ); break; default: System.out.println( Command Error ); break; }

For-Each Style Loops Useful when the elements of an array are sequentially accessed. Syntax definition: for(type iteration-var : collection) statement-block Example: // first declare variables int data[ ] = {11, 12, 13, 14, 15, 16, 17, 18}; int sum; // example loop using traditional method for (int i=0; I < 8; i++) sum += data[i];

For-Each Style Loops Cycles through a collection of items sequentially. Useful when the elements of an array are sequentially accessed. Syntax definition: for(type iteration-var : collection) statement-block

For-Each Style Loops Cycles through a collection of items sequentially. Useful when the elements of an array are sequentially accessed. Syntax definition: for(type iteration-var : collection) statement-block Examples: // first declare variables int data[ ] = {11, 12, 13, 14, 15, 16, 17, 18}; int sum; // example loop using traditional method for (int i=0; I < 8; i++) sum += data[i];

For-Each Style Loops Cycles through a collection of items sequentially. Useful when the elements of an array are sequentially accessed. Syntax definition: for(type iteration-var : collection) statement-block Examples: // first declare variables int data[ ] = {11, 12, 13, 14, 15, 16, 17, 18}; int sum; // example loop using traditional method for (int i=0; I < 8; i++) sum += data[i]; // example using For-Each style for (int x: data) sum += x;