1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.

Slides:



Advertisements
Similar presentations
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Strings in Java 1. strings in java are handled by two classes String &
Strings Testing for equality with 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 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.
Programming 2 CS112- Lab 2 Java
CIS 234: Strings. What Is a String? series of characters enclosed in double quotes characters can include letters (lower case and upper case), digits,
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 ,
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
Fundamental Programming Structures in Java: Strings.
28-Jun-15 String and StringBuilder Part I: String.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--WuChapter Chapter 8 Characters and Strings.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
CIS 234: Strings (click, scroll down)Strings Dr. Ralph D. Westfall April, 2010.
Characters, String and Regular expressions. Characters char data type is used to represent a single character. Characters are stored in a computer memory.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Characters In Java, single characters are represented.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
The java.lang Package chapter 8 Java Certification Study Group February 2, 1998 Seth Ladd.
String Manipulation Chapter 15 This chapter explains the String facilities. You have already seen some of the main methods of the String class.
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.
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.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
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,
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
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.
Strings Mr. Smith AP Computer Science A. What are Strings? Name some of the characteristics of strings: A string is a sequence of characters, such as.
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.
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. For example,
Introduction to programming in java
String and StringBuffer classes
Strings, Characters and Regular Expressions
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
String class.
EKT 472: Object Oriented Programming
String Handling in JAVA
Primitive Types Vs. Reference Types, Strings, Enumerations
Modern Programming Tools And Techniques-I Lecture 11: String Handling
String and StringBuilder
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Part a: Fundamentals & Class String
String and StringBuilder
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. Every time.
CS2011 Introduction to Programming I Strings
JavaScript: Objects.
Strings in Java.
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries

2 Java’s String Class in simplest form, just quoted text "This is a string" "So is this" "hi" used as parameters to –Text constructor –System.out.println

3 Strings are Objects String is a class, not a primitive type Java provides many methods for manipulating them compare with equals method find length with length method

4 Manipulating Strings Java also provides String literals and + operator –special features because strings used in many programs

5 The Empty String smallest possible string made up of no characters at all (length is 0) "" typically used when we want to build something from nothing

6 Building a String "From Nothing" Ex. Morse code Allow user to display a series of dots and dashes Long mouse click signifies dash Short click signifies dot private String currentCode = ""; currentCode is empty until user begins to enter dots and dashes 16.1.rtf

7 Long Strings Strings can be arbitrarily long –String chapter in your Java text can be 1 big string Practical issue for long strings: Readability –Might want line breaks in a string –newline character \n Ex. Let's add instructions to the Morse Code program

8 Morse Code Instructions This program will allow you to enter a message in Morse Code. To enter your message: Click the mouse quickly to generate a dot; Depress the mouse longer to generate a dash.

9 Printing Instructions 1.Series of 5 System.out.printlin instructions, or 2.Define String constant INSTRUCTIONS; print INSTRUCTIONS private static final String INSTRUCTIONS = "This program will allow you to enter a message in Morse code.\n" + "\n" + "To enter your message:\n" + "Click the mouse quickly to generate a dot;\n" + "Depress the mouse longer to generate a dash."; Note "\n" just has length one!!

10 Readability and Legality Java does not allow us to write a String literal with actual line breaks in it! System.out.println( "The message that you have entered contains characters that cannot be translated." ); is illegal System.out.println( "The message that you have entered contains " + "characters that cannot be translated." ); is legal

11 Many String Methods someString.length() returns an int that is number of characters in someString someString.endsWith( otherString ) returns true if and only if otherString is a suffix of someString someString.startsWith( otherString ) returns true if and only if otherString is a prefix of someString

12 More Useful Methods Example. Web browsers offer automatic address completion I type " My browser suggests " Keep track of URLs typed in by users Use this to provide suggestions Start of a URL History class

13 Finding a Substring someString.indexOf( otherString ) –think of otherString as a pattern to be found –returns an int giving first index in someString where otherString found Example. if sentence is "Strings are objects in Java." and pattern is "in", then sentence.indexOf(pattern) returns 3.

14 If sentence is "Strings are objects in Java." and pattern is "primitive type", then sentence.indexOf(pattern) returns -1

15 Using indexOf to find URLs // Return true if and only if the history contains the given URL public boolean contains( String aURL ) { // Look for URL terminated by newline separator return urlString.indexOf( aURL + "\n" ) >= 0; } Why must we add newline to the URL to be found?

16 Another indexOf someString.indexOf( pattern, startIndex) –Searches for pattern in someString, beginning at index given by startIndex If someString is "Strings are objects in Java." and pattern is "ing", then someString.indexOf( pattern, 0) returns 3 someString.indexOf( pattern, 5) returns -1 someString.indexOf( "in", 5) returns 20

17 Case Sensitivity someString.indexOf( "IN" ) yields -1 if someString is "Strings are objects in Java."

18 Dealing with Lower and Upper Case sometimes useful and important to distinguish between lower and upper case sometimes not if " in our history surely we want to recognize " as the same Note: part of URL after domain name may be case sensitive. Will ignore that here.

19 Methods for Handling Case someString.equalsIgnoreCase( otherString ) returns true if someString and otherString are composed of the same sequence of characters ignoring diffs in case someString.toLowerCase() returns a copy of someString with upper case chars replaced by lower case someString.toUpperCase()

20 Improving our contains method // Return true if and only if the history contains the given URL public boolean contains( String aURL ) { String lowerUrlString = urlString.toLowerCase(); // Look for URL terminated by newline separator return lowerUrlString.indexOf( aURL.toLowerCase() + "\n" ) >=0; } Alternative: Maintain URL History in lower case Fig16.6.rtf

21 Cutting and Pasting can paste strings together with concatenation operator (+) can also extract substrings somestring.substring( startIndex, endIndex ) returns substring of someString beginning at startIndex and up to, but not including, endIndex Ex. If urlString is “ urlString.substring( 7, 10 ) returns "www" and urlString.substring( 0, 7 ) returns “ and urlString.substring( 7, urlString.length() ) returns “

22 Rules for substring startIndex must be a valid index in the string endIndex may not be greater than the length of someString

23 Will use substring to help us find URL completions Let prefix be URL entered so far. Use indexOf to find prefix in urlString Extract full URL from urlString (up to newline) Add full URL to list of all possible completions. fig 16.7.rtf

24 Trimming Strings often want to ignore leading and trailing blanks in a string “ vs. " " someString.trim() returns a copy of someString with white space removed from both ends Fig 16.8.rtf

25 Comparing Strings equals and equalsIgnoreCase someString.compareTo( anotherString ) returns –0, if someString and anotherString are equal –positive int, if someString appears after anotherString in lexicographic ordering –negative int, if someString appears before anotherString in lexicographic ordering

26 Lexicographic Ordering if 2 strings are made up of alphabetic characters and both all lower case or upper case then lexicographic ordering = alphabetical ordering

27 StringBuffer Java Strings are immutable. StringBuffer is essentially a mutable String Various ways to construct them // empty with initial capacity 1000 StringBuffer urlStringBuffer = new StringBuffer(1000); // create StringBuffer from existing String StringBuffer urlStringBuffer = new StringBuffer (urlString); Many useful methods (append, replace, delete) Some String methods missing (toLowerCase, toUpperCase)

28 Characters Strings are sequences of characters Java data type char represents characters a primitive data type char literal written by putting character in single quotes 'a', 'A', '?', '7', '\n' Note: these are not the same as "a", "A", "?", "7", "\n"

29 Declaration and Use To declare variable letter of type char char letter; chars in Java represented internally as integers can perform arithmetic operations on them can compare them with operators like

30 1.Determine whether a char represents a digit in the range 0-9. if ( mysteryChar >= '0' && mysteryChar <= '9') works because integers representing '0' to '9' are consecutive numbers 1.e 2.Determine whether mysteryChar is a lower-case alphabetical character if ( mysteryChar >= 'a' && mysteryChar <= 'z') works because ints representing 'a' to 'z' are consecutive

31 Constructing Strings from chars can build a String from char components new String (characterArray) If example is the array of char then String aString = new String(example); creates the String "an example" 'a''n'' 'e''x''a''m''p''l''e'

32 Extracting chars from Strings aString.charAt( index ) returns the char at the specified index in aString If aString is "Coffee", then aString.charAt(1) returns '0' common use for charAt: check whether the characters in a string have some property

33 Using charAt Consider a medical record management program Want to treat weight as an int If weightField is the weight text field: String weight = weightField.getText(); int weightValue = Integer.parseInt(weight); But this only works if weight entered looks like an int

34 Checking for Integer Conversation Valid: "154", "016" Not valid: "154lbs", " 12" // Returns true if and only if number is a string of // digits in the range 0-9 public boolean validInt( String number ) { for (int i = 0; i < number.length(); i++) { char digit = number.charAt( i ); if (digit '9') { return false; } return true; }

35 Operations on chars ability to perform arithmetic on chars can be extremely useful. Example. A program that will translate a message into Morse code. –Make it simple: alphabetic messages only –Assume all characters upper case.

36 Translating to Morse Code I LOVE JAVA

37 High-level Translation // Converts an alphabetic string into Morse Code public String toMorseCode( String message ) { String morseMessage = ""; for (int i = 0; i < message.length(); i++) { char letter = message.charAt( i ); if (letter == ' ') { morseMessage = morseMessage + WORD_SPACE: } else { morseMessage = morseMessage + morseCode( letter ) + " "; } return morseMessage; }

38 How Does morseCode work? look up code in array would be convenient if int value of 'A' was 0, but it isn't –can calculate appropriate index! [letter - 'A'] –if letter is 'A', gives 0 –if letter is 'B', gives 1 etc.

39 Translating a Character to Morse Code // Returns the sequence of dots and dashes corresponding to // a letter of the alphabet public String morseCode( char letter ) { return letterCode[letter - 'A']; }

40 Chapter Review Java provides String literals and + operator But Strings are objects! Many useful methods –equals, equalsIgnoreCase –compareTo –toUpperCase, toLowerCase –indexOf –substring –trim –startsWith, endsWith and many others

41 char allows us to manipulate characters written as individual characters between single quotes represented internally as integers - can perform arithmetic on them