Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.

Slides:



Advertisements
Similar presentations
Java
Advertisements

1 StringBuffer & StringTokenizer Classes Chapter 5 - Other String Classes.
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.
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.
String class  Construct a string  String str = new String(“welcome”);  Char[] charr = {‘G’, ‘o’, ‘o’, ‘d’};  String mes = new String(charr);  A full.
Chapter 7: The String class We’ll go through some of this quickly!
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,
StringBuffer class  Alternative to String class  Can be used wherever a string is used  More flexible than String  Has three constructors and more.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
 2003 Prentice Hall, Inc. All rights reserved. Chapter 11 – Strings and Characters Outline 11.1 Introduction 11.2 Fundamentals of Characters and Strings.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
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.
Geoff Holmes Date Math Weighted Distr Strings String methods Tokenizers System Examples Utility Classes (Chapter 17) import java.util.*;
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,
CHAPTER 8 File Input Output Part 1: String. The String Class  Constructing a String: String message = "Welcome to Java“; String message = new String("Welcome.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
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 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,
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings Chapter.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
String and StringBuffer classes
Strings, Characters and Regular Expressions
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Strings.
EKT 472: Object Oriented Programming
String and String Buffers
Primitive Types Vs. Reference Types, Strings, Enumerations
String class in java Visit for more Learning Resources string.
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
Chapter 9 Strings and Text I/O
Java – String Handling.
Lecture 07 String Jaeki Song.
String and StringBuilder
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:

Jaeki Song JAVA Lecture 07 String

Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class

Jaeki Song JAVA String A string is a sequence of characters –Includes letters, digits, and various special characters, such as +, -, *, /, $ e.g.) “Jaeki Song” “ISQS 6337”

Jaeki Song JAVA Classes In Java, a string is an object Java provides three string classes –Java.lang package string class –Storing and processing strings but strings created using the string class cannot be modified stringBuffer class –Create flexible strings that can be modified –Java.util package stringTokenizer class –Can be used to extract tokens from a string

Jaeki Song JAVA String Class String class provide nine constructors and more than 40 methods for examining in individual characters in a sequence

Jaeki Song JAVA String Constructor You can create a string from a string value or from an array of characters String newString = new String(StringValue);  The argument StringValue is a sequence of characters enclosed inside double quotes e.g.) String message = new String (“Welcome”); String message = “Welcome”;

Jaeki Song JAVA Example: StringConstructor char charArray[]={'b', 'i', 'r', 't', 'h', ' ', 'd', 'a', 'y'}; byte byteArray[]={(byte) 'n', (byte) 'e', (byte) 'w', (byte) ' ', (byte) 'y', (byte) 'e', (byte) 'a', (byte) 'r'}; String s, s1, s2, s3, s4, s5, s6, s7, output; String s = new String ("hello"); StringBuffer buffer = new StringBuffer ("Welcome"); String s1 = new String( ); String s2 = new String(s); String s3 = new String(charArray); String s4 = new String(charArray, 6, 3); String s5 = new String(byteArray, 4, 4); String s6 = new String(byteArray); String s7 = new String(buffer); output = "s1 = " + s1 + "\ns2 = " + s2 + "\ns3 = " + s3 + "\ns4 = " + s4 + "\ns5 = " + s5 + "\ns6 = " + s6 + "\ns7 = " + s7; JOptionPane.showMessageDialog(null, output); System.exit (0);

Jaeki Song JAVA length, charAt, and getChars Methods Determine the length of string s1.length( ) Get the character at a specific location in a string s1.charAt (1) Get the entire set of characters in a string s1.getChars (0, 5, charArray, 0)

Jaeki Song JAVA Example String output; String s1 = new String( "hello there" ); char charArray[ ] = new char[ 5 ]; output = "s1: " + s1; output += "\nLength of s1: " + s1.length(); output += "\nThe string reversed is: "; for ( int count = s1.length() - 1; count >= 0; count-- ) output += s1.charAt( count ) + " "; s1.getChars( 0, 5, charArray, 0 ); output += "\nThe character array is: "; for ( int count = 0; count < charArray.length; count++ ) output += charArray[ count ]; JOptionPane.showMessageDialog( null, output); System.exit( 0 );

Jaeki Song JAVA String Comparisons Java provides a various comparison methods –equals Compare any two string objects for equality s1.equals(“hello”); Use lexicographical comparison –equalsIgnoreCase S1.equalsIgnoreCase (s2)

Jaeki Song JAVA String Comparisons compareTo s1.compareTo(s2) s1 > s2  positive number s1 < s2  negative number s1 = s2  zero e.g 1) s1 “h” and s2 “g”  s1.compareTo(s2)  1 e.g 2) s1 “abc” and s2 “abe”  s1.compareTo(s2)  -2

Jaeki Song JAVA String Comparisons regionMatches compares portions of two String objects for equality s1.regionMatches (0, s2, 0, 5); s1.regionMatches (true, 0, s2, 0, 5); If the first argument is true, the method ignores The case of the characters being compared

Jaeki Song JAVA Example CompareString

Jaeki Song JAVA Extracting Substrings substring method enable a new String object to be created by copying part of an existing String object substring (int startIndex)  Copies the characters form the starting index to the end of the String substring(int beginIndex, int endIndex)  Copies the characters from the starting index to one beyond the endIndex

Jaeki Song JAVA Example String letters = “Happy Birthday” letters.substring(7)  “irthday” letters.substring (0,5)  “Happy”

Jaeki Song JAVA String Concatenation Java provide the concat method to concatenate two strings String s1 = new String (“Happy ”); String s2 = new String (“Birthday”); s1.concat(s2);  “Happy Birthday”

Jaeki Song JAVA String Conversions Generally, the contents of a string cannot be changed once the string is created Java provides conversion methods –toUpperCase and toLowerCase Return a new string by converting all the characters in the string to lowercase or uppercase –Trim Returns a new string by eliminating blank characters from both ends of the string –Replace(oldChar, newChar) Can be used to replace a character in the string with a new character

Jaeki Song JAVA Example Example: CovergeString

Jaeki Song JAVA Converting Characters and Numeric Values to Strings The String class provides valueOf methods for converting a character, an array of characters and numeric values to strings –valueOf method take different argument types

Jaeki Song JAVA The StringBuffer Class Alternative class Can be used wherever a string is used –More flexible than String Can add, insert, or append new contents into a string buffer However, the value of string is fixed once the string is created The String class has three constructors and more than 30 methods for managing the buffer and for modifying strings in the buffer –Every StringBuffer is capable of storing a number of characters specified by its capacity

Jaeki Song JAVA StringBuffer Constructors public StringBuffer( ) –No characters in it and an initial capacity of 16 characters public StringBuffer (int length) –No characters in it and an inial capacity specified by the length argument public StringBuffer (String string) –Contains String argument and an initial capacity of the buffer is 16 plus the length of the argument

Jaeki Song JAVA StringBuffer Methods capacity method –Returns the current capacity of the string buffer length method –Returns the number of characters in the string buffer setLength method –Sets the length of the string buffer If the newLength argument is less than the current length of the string buffer, then the string buffer is truncated to contain exactly the number of characters given by the newLength argument charAt method –Returns the character at a specific index in the string buffer The first character of a string buffer is at index 0

Jaeki Song JAVA StringBuffer Methods You can append new contents at the end of a string buffer, insert new contents at a specified position in a string buffer, and delete of replace characters in a string buffer –Provides overload methods to append and insert boolean, char, char array, double, float, int, lng and String into string buffer

Jaeki Song JAVA Example Append and Insert String

Jaeki Song JAVA The StringTokenizer Class Break a string into pieces (tokens) so that information contained in it can be retrieved and processed –How does the StringTokenizer class recognize individual words? Specify a set of characters as delimiters when constructing a StringTokenizer object

Jaeki Song JAVA The StringTokenizer Class Methods hasMoreToken( ) Returns true if there is a token left in the string nextToken( ) Returns the next token in the string NextToken(String delim) Returns the next token in the string after reseting the delimiter to delim countToken( ) Returns the number of tokens remaining in the string tokenizer

Jaeki Song JAVA Example import java.util.*; public class TestToken { public static void main(String[] args) { String s = new String ("1000 Lucas Lubbock TX 79413"); StringTokenizer tokens = new StringTokenizer(s); System.out.println(tokens.countTokens()); while (tokens.hasMoreTokens()) { System.out.println(tokens.nextToken() + "\n"); } }