Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes.

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.
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.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Introduction to Objects and Input/Output
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 ,
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.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Strings Edward J. Biebel. Strings Strings are fundamental part of all computing languages. At the basic level, they are just a data structure that can.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
String Class. Objectives and Goals Identify 2 types of Strings: Literal & Symbolic. Learn about String constructors and commonly used methods Learn several.
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.
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.
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.
Objectives ► Become familiar with the class String ► ► Learn how to use input and output dialog boxes in a program ► ► Learn how to tokenize the input.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;
Operations on Strings. 8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation.
Chapter 7: Characters, Strings, and the StringBuilder.
Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,
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.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
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:
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
The String class is defined in the java.lang package
String and StringBuffer classes
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Strings.
University of Central Florida COP 3330 Object Oriented Programming
Programming in Java Text Books :
String and String Buffers
String Handling in JAVA
EE422C - Software Design and Implementation II
Modern Programming Tools And Techniques-I Lecture 11: String Handling
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Java – String Handling.
Lecture 07 String Jaeki Song.
Strings in Java.
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
JAVA – String Function PROF. S. LAKSHMANAN,
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

Strings And other things

Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes Printf() method The StringBuffer class

What is a string A sequence of characters Representing alphanumeric data. Defined in the java.lang.String class String is a class and objects of String type are reference variables.

Making a string Declared as a typical object would be. String s= new String(“Hello”) Or using a short hand that java provides String s = “Hello”

The string class provides many methods. charAt(int idx) compareTo(string s) concat(string s) equals(string s) indexOf(int ch,int fromhere) lastIndexOf(int ch,int fromhere) length() replace(char old, char new) substring(int beginidx, int endidx) trim() toUpper(String s)

Joining strings Concatenation is the operation of joining one string on the end of another. S3 = S.concat(S2) Or S3 = S + S2 Java will implicitly type cast a numeric value as a string

String literals “Hello” “Good bye” “10,100” “576DSW” “ ” “Bill”

String equality String s1=“Hello” String s2=“Hello” If (s1==s2) System.out.print(“Equal”)  Not equal If (s1.equals(s2)) System.out.print(“Equal”)  Equal String s3=s1 If (s3==s1) System.out.print(“Equal”)  Equal

String comparisons If S1 < S2 may not work correctly If (S1.compareTo(s2)) < 0 System.out.print(“Less then”) If (S1.compareTo(s2)) == 0 System.out.print(“Equals”) If (S1.compareTo(s2)) > 0 System.out.print(“Greater then”)

Finding strings in strings indexOf(int ch,int fromhere) lastIndexOf(int ch,int fromhere) indexOf(String S, int fromhere) S1=“Hello” S1.indexof(‘l’)  Will return 2 S1.lastindexof(‘l’)  Will return 3

String Conversions String.valueof(number)  Returns a string And Integer.parseint(String)  Return a number

Converting Strings ToLowerCase() ToUppercase()

The stringbuffer class Like the string class Why 2 string classes? Other methods:.delete(int loc, int toloc).reverse().capacity() the size of the stringbuffer

Char data type A char is a single unicode value Unicode is a code used to specify character data. char c; char[] carray = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ }; A character array can be used to build a string String s= new String(carray)

Command line arguments Command line arguments are passed to the args array in the main method. We can find the length of this array and process these arguments in our program.

That Character class A wrapper class  These are likes like Double and Integer which can be used in place of the primitive data types.  There is one for each primitive data type Provides methods like:  compareTo( char)  equals(char)  Character.isDigit(char) // a static method  Also: isLetter(char) and isLowerCase()…

StringBuilder An object that can be created to manipulate strings values mutably then store the result back into a string. Methods: append( String) insert(index, String) deleteCharAT( index) replace( startindex, endindex, string) toString()

StringTokenizer Used to break strings apart. This process is also known as parsing a string. A token is a special character used to delimit the parts of a string.  nextToken()  countTokens() The default token is set to “ \t\n\r\f” the white space characters. We can change this using the optional constructor..  StringTokenizer( string, tokenString)

Formatting data The printf() method of PrintStream class Format specifiers:  %.2f - a double value, 2 decimals  %d - an integer  %x- hexvalue System.out.printf(“data = %.2f”, data); Formatting dates:  %tA, %tB, %td, %tY  For day, month, dayof month, year

Argument index value System.out.printf(“%1$d, %2$.2f”, int, double) The 1$ and 2$ are used to specify the index of the parameter to use. By default the value of 1 is used. Field width: %3d- specifies the integer with room for 3 digits

Justification %-15s- left justify a string of 15 characters %15s- default right justify the string The format method  Formatter f = new Formatter()  F.format( “formatstring”, args…) Or  outstring =String.format( formatstring, args…)

Summary Strings Characters Char Format and sprint methods Importance of API documentation