Operations on Strings. 8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms The C Programming Language.
Advertisements

CS 100: Roadmap to Computing Fall 2014 Lecture 0.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Java Programming Strings Chapter 7.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Fundamental Programming Structures in Java: Strings.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 2: Introduction to C++.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
String Escape Sequences
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
05/09/2015SJF L31 F21SF Software Engineering Foundations Formatting Converting numbers to Strings and vice versa Monica Farrow EM G30
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
 Pearson Education, Inc. All rights reserved Formatted Output.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Input, Output, and Processing
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Types CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (Chapter 4, Horstmann text)
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.
Introduction to Java Java Translation Program Structure
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Chapter 7: Characters, Strings, and the StringBuilder.
String and Scanner CS 21a: Introduction to Computing I First Semester,
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Java Basics Variables, Expressions, Statements, etc. CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Simple Console Output. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
Simple Console Output CS 21a. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
28 Formatted Output.
Lecture 5 array declaration and instantiation array reference
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Documentation Need to have documentation in all programs
Programming Languages and Paradigms
Programming Paradigms
Programming Languages and Paradigms
OUTPUT STATEMENTS GC 201.
System.out.println for console output
Chapter 7: Strings and Characters
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
CS2011 Introduction to Programming I Strings
Classes CS 21a: Introduction to Computing I
Programming Languages and Paradigms
CS 1054 Introduction to Programming in Java
Arrays.
Presentation transcript:

Operations on Strings

8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation Slide 2 Strings In Java, strings are objects and String is a “built- in” class with special treatment String s = new String( “Hello” ); is the same as String s = “Hello”; “Hello” is an example of a String literal representing a String object The + operator can be used for String concatenation The String class has certain methods length() substring(…) equals(…) equalsIgnoreCase(…) others

8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation Slide 3 String Assignment Since String is a class (just like BankAccount), a String variable stores a reference to a String object String object: sequence of characters delimited by quotes; e.g., “Hello” Can be viewed as equivalent to new String( “Hello” ) Assignment Points the String reference (left-hand side) to the String instance (right-hand side) Similar to object declaration and assignment Uses the “=” operator String s = “Hello”;

8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation Slide 4 String Concatenation Strings have special treatment in Java in that the + operator works on string objects Concatenation Appends a variable or object with a String Returns the linked String as the result Uses the “+” operator String a = s + “ World!”; // a = “Hello World!” String b = s + 5; // b = “Hello5” “+=” can also be used on Strings b += “ World!”; // b = “Hello5 World!”

8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation Slide 5 String Methods The length() method returns an int representing the number of characters in the string String s = “Hello there”; System.out.println( s.length() ); // prints 11 The substring() method requires two ints as parameters and returns a part of the string bounded by those integers String s = “Hello there”; String t = s.substring(2,7); // assigns “llo t” Other string methods for equality and comparison

8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation Slide 6 String Equality Equality Requires a String parameter and returns a boolean Value equality: equals(), equalsIgnoreCase() method Checks if the value of two Strings are equal String a = “myString”; String b = “this is myString again”.substring(8,16); boolean ansA = a.equals( b ); // true boolean ansB = a == b; // false Pointer equality: “==” Checks if the two Strings are pointing to the same object Not used to check if Strings contain the same value

8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation Slide 7 String Comparison Natural comparison of Strings A more flexible comparison (not just checking for equality) Used by calling the compareTo() method Returns a number Example String a = “application”; int result = a.compareTo( “applet” ); Returns a negative number when a is less than b Returns 0 when a is equal to b Returns a positive number when a is greater than b What if we change the first line to String a = “Application” ?

8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation Slide 8 String Formatting There are string methods that can help with Text alignment Formatting of numeric values Java provides the a String formatting method String.format() The desired effect is also available through System.out.printf()

8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation Slide 9 String Formatting Methods which produce formatted outputs requires a format string and an argument list Example double pi = ; String s = String.format( “Pi: %.2f”, pi ); // s = “Pi: 3.14” Format string Contains fixed text and format specifiers that process the arguments

8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation Slide 10 Format Specifiers Indicates how the arguments should be processed and where they should be inserted Syntax % - start of the specifier arg_index + '$' (optional) position of argument in the argument list flags (optional) provides special formatting (e.g. left align, padding with zeroes)

8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation Slide 11 Format Specifiers Syntax (continued) width (optional) minimum number of spaces to print argument puts blanks in unused spaces '.' + precision (optional) number of digits after decimal ConversionFormatted into: dintegers ffloating-point number sString ccharacter conversion – how the argument should be formatted

8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation Slide 12 Example Print a price list String header = String.format("%s branch sales for %s“, "Katipunan“, “July” ); int num = 1; double price1 = 3.75; double price2 = 100;... double price10 = 12.5; System.out.println( header ); System.out.printf( "%s %s" "Item #", "Price" ); System.out.printf( "%-6d P%5.2f \n", num++, price1 ); System.out.printf( "%-6d P%5.2f \n", num++, price2 );... System.out.printf( "%-6d P%5.2f \n", num++, price10 ); Output: Katipunan branch sales for July Item # Price 1 P P P 12.50

8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation Slide 13 Summary String is a class; there are String objects with which String methods() can be carried out Strings also have a special treatment in Java String literals; e.g., “hello” The concatenation operators ( +, += )