Strings string: An object storing a sequence of text characters.

Slides:



Advertisements
Similar presentations
Java Programming Strings Chapter 7.
Advertisements

Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 4 Lecture 4-2: Strings reading: 3.3, self-check: Ch. 4 #12, 15 exercises:
The Class String. String Constants and Variables  There is no primitive type for strings in Java.  There is a class called String that can be used to.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 4 Lecture 4-3: Strings and objects; printf reading: 3.3, self-check: Ch.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 4 Lecture 4-2: Strings reading: 3.3, self-check: Ch. 4 #12, 15 exercises:
1 Chapter 2 JAVA FUNDAMENTALS CONT’D. 2 PRIMITIVE DATA TYPES Computer programs operate on data values. Values in Java are classified according to their.
Objects and Classes Plan for Today - we will learn about: the difference between objects and primitive values how to create (or construct) objects some.
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.
1 Text Processing. 2 Type char char : A primitive type representing single characters. –A String is stored internally as an array of char String s = "Ali.
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.
1 Text processing. 2 text processing: Examining, editing, formatting text.  Text processing often involves for loops that examine the characters of a.
From C++ to Java A whirlwind tour of Java for C++ programmers.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 4 Lecture 4-3: Strings, char reading: 3.3, self-check: Ch. 4 #12, 15 exercises:
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 5/27/20161.
Interactive Programs with Scanner. 2 Input and System.in interactive program: Reads input from the console. –While the program runs, it asks the user.
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
1 Fencepost loops suggested reading: The fencepost problem Problem: Write a static method named printNumbers that prints each number from 1 to a.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE STRING CLASS.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE STRING CLASS.
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.
Cryptography.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
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.
1 reading: 3.3 Using objects. Objects So far, we have seen: methods, which represent behavior variables, which represent data (categorized by types) It.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 4 Lecture 4-3: Strings, char reading: 3.3, self-check: Ch. 4 #12, 15 exercises:
Building Java Programs
Building Java Programs
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 15: Strings and Fencepost Loops
Building Java Programs
Chapter 2 Basic Computation
Lecture 8: The String class
Building Java Programs
CSCI 161 – Introduction to Programming I William Killian
Building Java Programs
Chapter 6 GC 101 Strings.
Adapted from slides by Marty Stepp and Stuart Reges
Conditional Execution
Building Java Programs Chapter 4.3
String Handling in JAVA
Topic 13 procedural design and Strings
CS 106A, Lecture 9 Problem-Solving with Strings
Chapter 7: Strings and Characters
Building Java Programs
תרגול מס' 3 עבודה עם מחרוזות (Strings) מתודות (Methods) העברת פרמטרים
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2018 Lecture 14: Booleans and Strings
Building Java Programs
CSC 221: Introduction to Programming Fall 2018
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Lecture 8: The String Class and Boolean Zen
Building Java Programs
Building Java Programs
Lecture 14: Strings and Recursion AP Computer Science Principles
Building Java Programs
Using Objects (continued)
Building Java Programs
Boolean logic suggested reading: 5.2.
Building Java Programs
Strings string: An object storing a sequence of text characters.
Building Java Programs Chapter 4
STRINGS BY: DANIEL, JUSTIN, PANI.
Optional Topic: User Input with Scanner
Presentation transcript:

Strings string: An object storing a sequence of text characters. Unlike most other objects, a String is not created with new. String name = "text"; String name = expression; Examples: String name = "Kanye West"; int x = 3; int y = 5; String point = "(" + x + ", " + y + ")";

Objects (usage) object: An entity that contains data and behavior. data: variables inside the object behavior: methods inside the object You interact with the methods; the data is hidden in the object. A class is a type of objects. Constructing (creating) an object: Type objectName = new Type(parameters); Calling an object's method: objectName.methodName(parameters);

String methods These methods are called using the dot notation: Method name Description indexOf(str) index where the start of the given string appears in this string (-1 if not found) length() number of characters in this string substring(index1, index2) or substring(index1) the characters in this string from index1 (inclusive) to index2 (exclusive); if index2 is omitted, grabs till end of string toLowerCase() a new string with all lowercase letters toUpperCase() a new string with all uppercase letters These methods are called using the dot notation: String sesameStreet = ”Bert & Ernie"; System.out.println(sesameStreet.length()); // 12

Strings as user input Scanner's next method reads a word of input as a String. Scanner console = new Scanner(System.in); System.out.print("What is your name? "); String name = console.next(); name = name.toUpperCase(); System.out.println(name + " has " + name.length() + " letters and starts with " + name.substring(0, 1)); Output: What is your name? Nas NAS has 3 letters and starts with N The nextLine method reads a line of input as a String. System.out.print("What is your address? "); String address = console.nextLine();

String test methods Method Description equals(str) whether two strings contain the same characters equalsIgnoreCase(str) whether two strings contain the same characters, ignoring upper vs. lower case startsWith(str) whether one contains other's characters at start endsWith(str) whether one contains other's characters at end contains(str) whether the given string is found within this one String name = console.next(); if(name.endsWith("Kweli")) { System.out.println("Pay attention, you gotta listen to hear."); } else if(name.equalsIgnoreCase("NaS")) { System.out.println("I never sleep 'cause sleep is the cousin of death."); }

Strings question Write a program that reads two people's names and generates a new hybrid name. Example Output: Person 1 name? John Person 2 name? Danielle Name Gender? f Suggested name: JODANIA Name Gender? Masculine Suggested name: JODANIO Person 1 name? John Person 2 name? Danielle Name Gender? nope Suggested name: JODANI

Type char char : A primitive type representing single characters. Each character inside a String is stored as a char value. Literal char values are surrounded with apostrophe (single-quote) marks, such as 'a' or '4' or '\n' or '\'' It is legal to have variables, parameters, returns of type char char letter = 'S'; System.out.println(letter); // S char values can be concatenated with strings. char initial = 'P'; System.out.println(initial + " Diddy"); // P Diddy

char vs. int All char values are assigned numbers internally by the computer, called ASCII values. Examples: 'A' is 65, 'B' is 66, ' ' is 32 'a' is 97, 'b' is 98, '*' is 42 Mixing char and int causes automatic conversion to int. 'a' + 10 is 107, 'A' + 'A' is 130 To convert an int into the equivalent char, type-cast it. (char) ('a' + 2) is 'c'

String/char question A Caesar cipher is a simple encryption where a message is encoded by shifting each letter by a given amount. e.g. with a shift of 3, A → D, H → K, X → A, and Z → C Write a program that reads a message from the user and performs a Caesar cipher on its letters: Your secret message: I love Computer Science Your secret key: 3 The encoded message: l oryh frpsxwhu vflhqfh