Fundamental Programming Structures in Java: Strings.

Slides:



Advertisements
Similar presentations
1 StringBuffer & StringTokenizer Classes Chapter 5 - Other String Classes.
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 &
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.
©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 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.
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,
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.
©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 
CIS 234: Strings (click, scroll down)Strings Dr. Ralph D. Westfall April, 2010.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
The java.lang Package chapter 8 Java Certification Study Group February 2, 1998 Seth Ladd.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
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 Carol Yarbrough AP Computer Science Instructor Alabama School of Fine Arts.
Chapter 7: Characters, Strings, and the StringBuilder.
1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.
String Handling StringBuffer class character class StringTokenizer class.
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)
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,
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
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.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering products.
Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes.
1 Java Strings Dr. Randy M. Kaplan. 2 Strings – 1 Characters are a fundamental data type in Java It is common to assemble characters into units called.
Strings and ArrayLists April 23, 2012 ASFA AP CS.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
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,
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.
17-Feb-16 String and StringBuilder Part I: String.
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.
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,
Strings, Characters and Regular Expressions
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Strings.
The StringBuffer Class 
String and String Buffers
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.
Java – String Handling.
Lecture 07 String Jaeki Song.
String and StringBuilder
Object Oriented Programming
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

Fundamental Programming Structures in Java: Strings

In this class we will cover how to: Manipulate characters Declare a String object Compare String values Use other String methods Convert Strings to numbers Use the StringBuffer class Use of the StringTokenizer class

Manipulating Characters Character class- Contains standard methods for testing the values of characters, such as letters or digits –Primitive data type char is used to hold any single character –The Character class is defined in java.lang.Object –Automatically imported into every program you write –What is the difference between char and Character?

Example of Using Character Class Source code: –public class CharacterUse { public static void main(String[] args) { char c = 'c'; System.out.println("uppercase is " + Character.toUpperCase(c)); } } Result: –uppercase is C

Declaring a String Object String variable An object of the class String –The class String is defined in java.lang.String and is automatically imported into every program The string itself is distinct from the variable you use to refer to it Create a String object by using the keyword new and the String constructor method –String aGreeting = new String(“Hello”); What does it mean when someone says Strings are immutable?

Strings are Immutable Immutable- Strings and other objects that can’t be changed –String is a class; each created String is a class object –Strings are never actually changed; instead new Strings are created and String variables hold the new addresses. –A part of the Java system called the garbage collector will eventually discard the unused strings So, what does this mean for Java programmers?

Changing String Values Strings have no methods to let you change an existing character in a string. –To change a string you have to reassign the variable to a new string. –example: String greeting = “Hello”; greeting = greeting.substr(0,4) + “!”; Result: greeting is now “Hell!”

Comparing String Values Because String variables hold memory addresses, you cannot make a simple comparison The String class provides a number of methods –equals() method –equalsIgnoreCase() method –compareTo() method

Comparing String Values equals() method- Evaluates the contents of two String objects to determine if they are equivalent –This method returns true if the two String objects have identical contents –Can take either a variable String object or a literal string as its argument s1.equals(s2); s1.equals(“Hello”);

Comparing String Values equalsIgnoreCase() method- Similar to the equals() method –Ignores case when determining if two Strings are equivalent –Useful when users type responses to prompts in your program

Comparing String Values compareTo() method- Used to compare two Strings –It provides additional information to the user in the form of an integer value –Returns zero only if the two Strings hold the same value –If there is any difference between the Strings, a negative number is returned if the calling object is “less than” the argument –A positive number is returned if the calling object is “more than” the argument –Strings are considered “less than” or “more than” each other based on their Unicode values

Using Other String Methods There are additional String methods available in the String class –toUpperCase() and toLowerCase() convert any String to its uppercase or lowercase equivalent –substr() returns a portion of a String e.g. String s = “Hello”; String s2 = s.substring(1,4); What is the value of s2? –What are some other String methods? –See the API documentation for a complete list of String methods

Using Other String Methods indexOf() method determines whether a specific character occurs within a String If it does contain that character, the method returns the position of the character The first position of a String is zero, not one The return value is -1 if the character is not in the String

Using Other Methods charAt() method requires an integer argument which indicates the position of the character that the method returns endsWith() method and the startsWith() method each take a String argument and return true or false if a string object does or does not end or start with the specified argument replace() method allows you to replace all occurrences of some character within a String toString() method converts any primitive type to a String

Concatenation Concatenation- Joining strings Examples: –“45” + “36” = “4536” –String name = “Cartman”; System.out.println(“Name is “ + name);

Converting Strings to Numbers To convert a String to a number Use the Integer class –Part of java.lang parseInt() method is part of the Integer class and takes a String argument and returns its integer value –Use the integer value as any other integer –Useful for taking in integer parameters –e.g. String s = args[0]; int aNumber = Integer.parseInt(s); aNumber++; To convert a String object to a double value you must use the Double class

Learning about the StringBuffer Class A String class limitation is that the value of a String is fixed after the string is created To circumvent these limitations you can use the StringBuffer class

StringBuffer StringBuffer is an alternative to the String class Part of the java.lang package Must use the keyword new and provide an initializing value between parentheses A StringBuffer object contains a memory block called a buffer which might not contain a string

StringBuffer The length of the String may not be the same as the length of the buffer The length of the buffer is referred to as the capacity of the StringBuffer object You can change the length of a String in a StringBuffer object with the setLength() method When the StringBuffer object’s length is longer than the String it holds, the extra characters contain ‘\u0000’ If you use the setLength() method to specify a length shorter than its String, the string is truncated

StringBuffer Using StringBuffer objects provide more flexibility than String objects because you can insert or append new contents into a StringBuffer

StringBuffer Constructors The StringBuffer class provides 3 constructors –public StringBuffer() constructs a StringBuffer with no characters and a default size of 16 characters –public StringBuffer(int length) constructs a StringBuffer with no characters and a capacity specified by length –public StringBuffer(String s) contains the same characters as those stored in the String object s

StringBuffer Methods append() method- Lets you add characters to the end of a StringBuffer object insert() method- Lets you add characters at a specified location within a StringBuffer object setCharAt() method- Use to alter just one character in a StringBuffer

StringBuffer Methods charAt() method extracts characters from a StringBuffer object –Accepts an argument that is the offset of the character position from the beginning of a String

StringTokenizer Used to break up a string based on a certain “token” –e.g. take the string “Homer, Marge, Bart, Lisa, Maggie” You can use the StringTokenizer to break up the string based on the comma to get five separate strings “Homer” “Marge” “Bart” “Lisa” and “Maggie” Easier than using the “if” statement Example: –String testString = “one-two-three”; StringTokenizer st = new StringTokenizer(testString,”-"); while (st.hasMoreTokens()) { String nextName = st.nextToken(); System.out.println(nextName); } –Result: one two three