String Manipulation Chapter 15 This chapter explains the String facilities. You have already seen some of the main methods of the String class.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Advertisements

Strings in Java 1. strings in java are handled by two classes String &
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Working with String Duo Wei CS110A_ Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Strings and Arrays The objectives of this chapter are:  To discuss the String class and some of its methods  To discuss the creation and use of Arrays.
Chapter 10.
String class  Construct a string  String str = new String(“welcome”);  Char[] charr = {‘G’, ‘o’, ‘o’, ‘d’};  String mes = new String(charr);  A full.
CIS 234: Strings. What Is a String? series of characters enclosed in double quotes characters can include letters (lower case and upper case), digits,
Introduction to Computers and Programming Lecture 7:
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Fundamental Programming Structures in Java: Strings.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic.
CIS 234: Strings (click, scroll down)Strings Dr. Ralph D. Westfall April, 2010.
Recitation 2 Main Method, API & Packages, Java Basics.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Java 2 More Java Then Starbucks ;-). Important Terms Primitive Data – Basic, built-in values (characters & numbers) Data Type – Used to talk about values.
From C++ to Java A whirlwind tour of Java for C++ programmers.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
String Manipulation. Java String class  The String class represents character strings “Tammy Bailey”  All strings (arrays of characters) in Java programs.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Introduction to Java Java Translation Program Structure
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
Chapter 7: Characters, Strings, and the StringBuilder.
Java Programming Java Basics. Data Types Java has two main categories of data types: –Primitive data types Built in data types Many very similar to C++
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
Strings JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Strings Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering products.
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming strings.
Road map char data type Reading –Liang 5: Chapter 2: 2.7.4; 2.9; –Liang 6: Chapter 2: 2.7.4; 2.9 –Liang 7: Chapter 2: 2.7.4; 2.9.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Chapter 8: Loops, Arrays, Strings Loop statements –do –while –for Arrays –declaration, allocation, initialization, access –multi-dimensional –heterogeneous.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
String class.
Primitive Types Vs. Reference Types, Strings, Enumerations
Modern Programming Tools And Techniques-I Lecture 11: String Handling
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Tonga Institute of Higher Education
CS2011 Introduction to Programming I Strings
Introduction to Computer Science
Switch, Strings, and ArrayLists in Java
Strings in Java Strings in Java are also a reference type.
Unit-2 Objects and Classes
What We Want To Do User enters: Mary Smith
Presentation transcript:

String Manipulation Chapter 15 This chapter explains the String facilities. You have already seen some of the main methods of the String class

Introduction: Strings are very important. All programming languages have primitive character manipulation Java has a useful collection of methods associated with Strings. This chapter reviews the features covered earlier in the text and extends the study of String processing methods

Some situations where Strings are used: Display messages on screen Input text from user Store data on files (see Chapter 17) Searching web pages Hold text for word processing and editors

Using Strings: a recap Declare variables and provide initial values –String x, y; –String myName = “Parr” –String myCountry = new String (“Japan”); ( new is performed implicitly and explicitly – it does not have to be used) Strings may be assigned to other Strings –String x = “England”; String y = “France”; y = x; y = “ “; // zero length String

The concatenation operator int number = 123; textField.setText (“ Value is “ + number ); Java rule: when one item is a string the others are converted to String –int value = 2333 – textField.setText ( “ value is “ + ( )); –// value is 55 - what is displayed in Text field Appending x + “ something” // adds to end of x

Compare Strings if ( x.equals (y)) …// the.equals operator is used with String (not = = ) Arrays of Strings – String [ ] cities = new String [10]; – cities[ 1 ] = “Los Angles”;

The characters within Strings Double quote surround String x = “abc”; To display a double quote use \ character textField.setText ( “ A \“tricky “ \“ problem!”); \n is the newline character \t is the tab charcater

A note about the class type char Holds one item as a 16 bit unicode character. Can be compared as swiftly as int – char initial = ‘M’; –// note single quotes – char marker = ‘\n’; // \n is a single character Character type rarely used but is efficient

The String class String class methods An exception StringIndexOutOfBounds –Index too large or negative –See page 228 screenshot of StringTemplate program User keys in the Strings, result field tells if they are equal …if ( string1.equals(string2)) result = “They are equal.”; – else result = “They are not equal.”; …

Comparing Strings Methods: – equals, equalsIgnoreCase, compareTo equals: if (string1.equals (string2)) result = “They are equal”; –equalsIgnoreCase: “This string” vs. “this string” –compareTo returns a result of: 0 = equal negative = object precedes positive object that follows positive = object follows – n = ant.compareTo (“bee”); // returns a negative number (ANSI value for b greater that a)

Amending Strings replace – string1 = “Massassappi”.replace ( ‘i’, ‘a’); Produces Mississippi toLowerCase – string1 = “Version 1.1” ; – result = string1.toLowerCase ( ); –Produces version toUpperCase – string1 = “Java”; – result = string1.toUpperCase; –Produces JAVA

trim – string1 = “ Center “; Result = string1.trim ( ); Produces “Center” without leading/following spaces

Examining Strings length returns current number of characters – string1.length returns characters in string1 –.length often used in loops with arrays Substrings: Extracts a specified part of a string – string1 = “position”; – result = string1.substring (2,5) ; –Result = sit // remember start count at zero

charAt – char c1, c2; –Returns the character at a specified position string1 = “position”; c1 = string1.charAt(1); // c1 = o // remember start at zero!

indexOf Is a substring contained within a String – int n = “mississippi”.index(“is”,4); Sets n to 4 // start count at zero if we tried (“is”, 5) N set to -1 // not found lastIndexOf –Return rightmost occurrence of substring – int place = “//a.b.c/directory/file.lastIndexOf(“/”); –Value 17 returned – again start your count at zero

endsWith Does String end with a particular substring? boolean r = “ (“/”); //sets r to true

StringTokenizer class If data has repeated substrings seperated by special characters splitting with stringTokenizer class more easily done January , 6,7,10,10,12,13, 15, 21, 20, 19,8 Separators are spaces after January and commas after each number

nextToken ( ) String example1 = “January 21 5”; String month, day, hours; stingTokenizer senddata = new StingTokenizer (example1, “ “); month = sumData.nextToken( ); day = sumData.nextToken( ); hours = sumData.nextToken( );

hasMoreTokens( ) Returns true if there are more data for nextToken to fetch import.util.*; // whenever StringTokenizer class to be used

String conversions Primitive types: int, double, boolean, character may be used int n = 123; String s = Integer.toString (n); // s becomes “123” double d = 12.34; String s = Double.toString (d); // s becomes “12.34”

parseInt n = Integer(SomeTextField.getText( )); –Converts a String into an int – d = Double.parseDouble (s);

String parameters String any = “Hello”; … return any + any –Method returns concatenated String HelloHello Example of String processing: String class has replace method – only handels a single character at a time. See method replace page 288. Replaces substring

private String replace (String original, String from, String to) { …

String case study 1970 Joseph Weizenbaum’s program ELIZA simulates psychiatrist –See page 289 User keys in a statement Program responds – echoes users statement – asks why? –Program Psychiatrist pages –Program AskFraiser pages –Screenshot page 291 –See Summary Strings page 294