CHAPTER 6 GC 101 1 Strings. THE CLASS STRING  Contains operations to manipulate strings.  String:  Sequence of zero or more characters.  Enclosed.

Slides:



Advertisements
Similar presentations
Java Programming Strings Chapter 7.
Advertisements

©2004 Brooks/Cole Chapter 7 Strings and Characters.
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:
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.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Strings.
INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in The.
1 Sierpinski Valentine. CS 112 Introduction to Programming Switch; Text Processing Yang (Richard) Yang Computer Science Department.
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.
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.
Chapter 2 – Continued Basic Elements of Java. Chapter Objectives Type Conversion String Class Commonly Used String Methods Parsing Numeric Strings Commonly.
Chapter 2: Java Fundamentals Type conversion,String.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Chapter 7: Characters, Strings, and the StringBuilder.
Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,
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.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering products.
Department of Computer Engineering Using Objects Computer Programming for International Engineers.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE STRING CLASS.
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.
Declaring variables The type could be: int double char String name is anything you want like lowerCaseWord.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
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:
String and Lists Dr. José M. Reyes Álamo.
Building Java Programs
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
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
Building Java Programs Chapter 4.3
Strings string: An object storing a sequence of text characters.
Topic 13 procedural design and Strings
String Objects & its Methods
Java Strings Slides provided by the University of Washington Computer Science & Engineering department.
CS 106A, Lecture 9 Problem-Solving with Strings
Chapter 7: Strings and Characters
Building Java Programs
Java Fundamentals 3 Type conversion ,String.
Chapter 2: Basic Elements of Java
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Lecture 10 Strings CSE /26/2018.
Lecture 8: The String Class and Boolean Zen
Building Java Programs
Lecture 14: Strings and Recursion AP Computer Science Principles
Building Java Programs
Using Objects (continued)
Building Java Programs
Building Java Programs
Strings string: An object storing a sequence of text characters.
Java Fundamentals 3 Type conversion ,String.
Strings in Java Strings in Java are also a reference type.
Presentation transcript:

CHAPTER 6 GC Strings

THE CLASS STRING  Contains operations to manipulate strings.  String:  Sequence of zero or more characters.  Enclosed in double quotation marks.  Is processed as a single unit.  Null or empty strings have no characters. “ “  Every character has a relative position, the first character is in position 0. 2

Java Programming: From Problem Analysis to Program Design, Second Edition3 THE CLASS STRING  Java system automatically makes the class String available (i.e no need to import this class )  Example : String name = " text "; String name = expression ;  Examples: String name = "Marla Singer"; int x = 3; int y = 5; String point = "(" + x + ", " + y + ")"; 3

INDEXES  Characters of a string are numbered with 0-based indexes: String name = "P. Diddy";  The first character's index is always 0  The last character's index is 1 less than the string's length index char P. Diddy 4

THE CLASS STRING  Length of the string is the number of characters in it.  When determining the length of a string, blanks count.  Example :  “ “  has length = 0  “abc”  has length = 3, position of a = 0,b= 1, c= 2  “a boy”  has length = 5 5

CSM-Java Programming-I Lesson-1 STRING OPERATIONS  The length() method returns the length of the string. Eg: System.out.println(“Hello”.length()); // prints 5  The + operator is used to concatenate two or more strings. Eg: String myname = “Harry” String str = “My name is” + myname+ “.”; 6

STRING METHODS  These methods are called using the dot notation: String gangsta = "Dr. Dre"; System.out.println(gangsta.length()); // 7 Method nameDescription indexOf( str ) index where the start of the given string appears in this string (-1 if it is not there) 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 omitted, grabs till end of string toLowerCase() a new string with all lowercase letters toUpperCase() a new string with all uppercase letters 7

STRING METHOD EXAMPLES String s1 = "Stuart Reges"; String s2 = "Marty Stepp"; System.out.println(s1.length()); // 12 System.out.println(s1.indexOf("e")); // 8 System.out.println(s1.substring(7, 10)) // "Reg" String s3 = s2.substring(2, 8); System.out.println(s3.toLowerCase()); // "rty st"  Given the following string: // index String book = "Building Java Programs";  How would you extract the word "Java" ?  How would you extract the first word from any string? 8

Java Programming: From Problem Analysis to Program Design, Second Edition9 SOME COMMONLY USED STRING METHODS 9 programming with Java is fun g 17 String mystr=new String("programming with Java is fun"); System.out.println(mystr); System.out.println(mystr.charAt(3)); System.out.println(mystr.indexOf('J')); System.out.println(mystr.indexOf(‘j'));

Java Programming: From Problem Analysis to Program Design, Second Edition10 SOME COMMONLY USED STRING METHODS 10

EXAMPLE 11 programming with Java is fun String mystr=new String("programming with Java is fun"); System.out.println(mystr); System.out.println(mystr.indexOf('a',10)); System.out.println(mystr.indexOf("with")); System.out.println(mystr.indexOf("ing"));

MODIFYING STRINGS  Methods like substring, toLowerCase, etc. create/return a new string, rather than modifying the current string. String s = "lil bow wow"; s.toUpperCase(); System.out.println(s); // lil bow wow  To modify a variable, you must reassign it: String s = "lil bow wow"; s = s.toUpperCase(); System.out.println(s); // LIL BOW WOW 12

Java Programming: From Problem Analysis to Program Design, Second Edition13 SOME COMMONLY USED STRING METHODS 13

EXAMPLES ON STRING METHODS String s1, s2, s3 ; s1 = “abcdefeg” ; System.out.println( s1.length() ); // 8 System.out.println(s1.charAt(3)); //d System.out.println(s1.indexOf(‘e’)); //4 System.out.println(s1.indexOf(“cd”)); //2 System.out.println(s1.toUpperCase()); //ABCDEFEG 14

15 EXAMPLES ON STRING METHODS System.out.println(s1.substring(1, 4)); //bcd System.out.println(s1 + “xyz”); // abcdefegxyz System.out.println( s1.replace(‘d’,’D’)); // abcDefeg System.out.println(s1.charAt(4) ); // e System.out.println(s1.indexOf(‘b’)); // 1 System.out.println(s1.indexOf(‘e’,5)); // 6 15

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

CHAR VS. STRING  "h" is a String 'h' is a char (the two behave differently)  String is an object; it contains methods String s = "h"; s = s.toUpperCase(); // 'H' int len = s.length(); // 1 char first = s.charAt(0); // 'H'  char is primitive; you can't call methods on it char c = 'h'; c = c.toUpperCase(); // ERROR: "cannot be dereferenced"