BPJ444: Business Programming Using Java – String Handling Tim McKenna

Slides:



Advertisements
Similar presentations
1 StringBuffer & StringTokenizer Classes Chapter 5 - Other String Classes.
Advertisements

Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
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 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.
Java Programming Strings Chapter 7.
Chapter 10 Review. Write a method that returns true is s1 and s2 end with the same character; otherwise return false. Sample Answer: public boolean lastChar(String.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text I/O.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
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 ,
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
Fundamental Programming Structures in Java: Strings.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
28-Jun-15 String and StringBuilder Part I: String.
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 ,
 Pearson Education, Inc. All rights reserved Strings, Characters and Regular Expressions.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Chapter 9: Text Processing and More about Wrapper Classes Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.
Characters, Strings and the String Buffer Jim Burns.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
Lecture 14 March 23, Exam Results Class Average was 69.4 –Median was 72.5 (which means there were some very low grades) Questions 31, 32, and 33.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 8 Strings 1.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;
Chapter 7: Characters, Strings, and the StringBuilder.
CHAPTER 9 Text Processing and More about Wrapper Classes Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
10-2 Chapter 10 discusses the following main topics:  Introduction to Wrapper Classes  Character Testing and Conversion with the Character Class  More.
String Handling StringBuffer class character class StringTokenizer class.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
1 The String Class F Constructing a String: F Obtaining String length and Retrieving Individual Characters in a string F String Concatenation (concat)
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 10: Text Processing and More about Wrapper Classes Starting.
Strings Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and.
String Definition A String is a set of characters that behaves as a single unit. The characters in a String include upper-case and lower-case letters,
String String Builder. System.String string is the alias for System.String A string is an object of class string in the System namespace representing.
Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
3 - 1 Text Processing In Java: Characters and Strings Reading:Downey: Chapter 7 Problem Set:Assignment #1 due Tuesday, Feburary 13 Wellesley College CS230.
String Definition A string is a set of characters that behaves as a single unit. The characters in a string include upper-case and lower- case letters,
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
17-Feb-16 String and StringBuilder Part I: String.
CHAPTER 6 GC Strings. THE CLASS STRING  Contains operations to manipulate strings.  String:  Sequence of zero or more characters.  Enclosed.
String and StringBuffer classes
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Strings.
Chapter 6 GC 101 Strings.
String String Builder.
String Handling in JAVA
Advanced String handling
String and StringBuilder
16 Strings.
Lecture 07 String Jaeki Song.
CS2011 Introduction to Programming I Strings
Chapter 10 Thinking in Objects Part 2
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
Presentation transcript:

BPJ444: Business Programming Using Java – String Handling Tim McKenna

The String Class u Strings are "immutable" objects: once instantiated, a String object is constant and not changeable. u because String objects are immutable, they can be shared fearlessly: no one can change your object. u Java optimizes memory by maintaining a pool of shared Strings: the constants "A", "A", "A" will have three references to the same String object. u Note: an empty string object is not null u literal: "" is a String object with a length() of 0 u Example: StringDemo.java

Strings are special in Java  Strings are used so often in programming, Java makes special allowances for coding them  construct a String without new String() String s1 = new String("some text"); String s2 = "more text";  the only overloaded operators in Java are for Strings  s1 += s2; // s1 = s1.concat(s2);  s1 = s2 + "etc"; // s1 = s2.concat("etc");

anything can be a String, just ask  static method String.valueOf() returns a String can take almost anything as a parameter: all primitives, char array, any object.  all objects inherit or override the Object class toString() method  System.out.println() automatically calls toString() on any object in the parameter list System.out.println(myObject); // is same as System.out.println(myObject.toString() );

The String Class u comparison of two String objects: u thisString.equals(thatString)compares contents u this is what most of us mean most of the time u thisString == thatStringcompares obj.ref. u this may seem like it works but is unreliable u when is thisString really thatString when is thisString really thatString u an array of characters vs a String object u Example: StringDemo2.java

String Class Methods u length()returns int of character count u trim() returns String exclusive of lead/trail blanks u toUpperCase(), to LowerCase() returns consistent case u valueOf()returns String of any primitive u indexOf()returns int locating a char or substring u charAt()allows processing of string like char[] u substring()returns a substring from this string u replace()changes characters u replaceAll() changes strings with regular expressions u split()splits a string into an array of strings using reg.exp. u Examples:StringDemo3.java, StringDemoGUI.java

The StringBuffer/Builder Class u mutable string objects u much better performance for building up a String u useful methods for string manipulation u append() – add a string or char u insert() setCharAt() – change a string u setLength() – truncates or pads u Example: StringDemo4.java

StringTokenizer Class u parsing: extract words (i.e. tokens) from a string u StringTokenizer st; st = new StringTokenizer("this is a test"); // default delimiter is whitespace while (st.hasMoreTokens()) { System.out.println( st.nextToken()); } this is a test

StringTokenizer alternatives u API recommends using String.split("\\s+") u split uses a regular expression to identify delimiters u can be powerful but reg. exp. are tricky and require extensive, thorough, and exhaustive testing u split takes 3-4 times longer than StringTokenizer u Scanner class is a new alternative in J2SE 5.0 u it takes times longer than StringTokenizer u has other functions to parse primitives and BigDecimals u Example: ParseString.java

StringTokenizer and StringBuilder  use these two classes to remove embedded blanks from a string.  String s; StringTokenizer st; StringBuilder sb; s=" My Spacebar Is Sticky " st=new StringTokenizer(s); sb=new StringBuilder( s.length()); while ( st.hasMoreTokens() ) { sb.append( st.nextToken() );} s=sb.toString();

The Character Class u some useful methods: u isDigit () u isLetter ()  isLetterOrDigit( ) u isLowerCase () toLowerCase () u isUpperCase () toUpperCase () u isWhitespace ()