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.

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.
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 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.
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.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
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 ,
1 String Buffer & String Tokenizer Overview l Overview of String Buffer class and Methods l Overview of String Tokenizer class and methods l Preview: Notions.
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.
 Pearson Education, Inc. All rights reserved Strings, Characters and Regular Expressions.
Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text.
Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
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.
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.
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.
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)
Strings Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and.
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.
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 class. Method concat Create string object –> String st1, st2; Input character to string object –> st1=br.readLine(); st2= br.readLine(); Use method.
17-Feb-16 String and StringBuilder Part I: String.
1 Unit-2 Arrays, Strings and Collections. 2 Arrays - Introduction An array is a group of contiguous or related data items that share a common name. Used.
Chapter 8 String Manipulation
String and StringBuffer classes
Strings, Characters and Regular Expressions
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Strings.
EKT 472: Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
Programming in Java Text Books :
String and String Buffers
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
String and StringBuilder
String and StringBuilder
Java – String Handling.
Representation and Manipulation
Lecture 07 String Jaeki Song.
String and StringBuilder
String.
JavaScript: Objects.
String methods 26-Apr-19.
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:

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 hold a series of characters However, strings are not implemented as a character array in Java as in other languages.

Strings in Java Strings are implemented as two classes in Java java.lang.String provides an unchangeable String object java.lang.StringBuffer provides a String object that can be amended

Basic String Methods length() returns the length of the string toLowerCase() converts the string to lower case toUpperCase() converts the string to upper case replace(char, char) replaces occurrences of one character with another character

Basic Strings continued Basic strings are not meant to change frequently so there are no add or append methods However the concat(String) method does allow two strings to be concatenated together

Basic Strings continued Substrings of a String object can also be accessed A portion of String object can be copied to a character array using the getChars() method The substring() method can return substring beginning at a specified offset

Searching a string Methods for searching strings –indexOf(x) searches for the first occurrence of x –indexOf(x, y) searches for the first occurrence of x after the offset of y –lastIndexOf(x) searches backwards for the first occurrence of x –lastIndexOf(x, y) searches backwards for the first occurrence of x after the offset of y

Example of string search indexOf(x) and indexOf(x, y) can find all occurrences of a character(s) in a string public void paint(Graphics g) { String str = new String("Wish You Were Here"); int count = 0; int fromIndex = 0; while(fromIndex != -1) { fromIndex = str.indexOf("er", fromIndex); if (fromIndex != -1) { count++; fromIndex++; } g.drawString(String.valueOf(count), 10, 10); }

Parsing Strings Strings can be parsed with the StringTokenizer class The default delimiters (space, tab, newline and carriage return) can be used for parsing sentences By specifying different delimiters, a wide variety of strings may be parsed

Parsing Strings continued Different default constructors are provided –Tokenize the string based on the default delimiters –Tokenize the string based on a specified set of delimiters –Tokenize the string based on a specified set of delimiters with a boolean flag to specify whether the delimiters should also be returned as tokens

StringBuffer class The StringBuffer class is provided for strings that need may need to be changed The StringBuffer class contains methods for both inserting and appending text An object created as a StringBuffer can easily be converted to an object of the String class if needed

More on StringBuffer Class Conversion may be needed because many Java library methods expect a string The toString() method is used for converting a StringBuffer object to a String object Example of converting a StringBuffer to a String: public void paint(Graphics g) { StringBuffer buf = new StringBuffer("Hello, World"); g.drawString(buf.toString(), 10, 10); }

More on StringBuffer Class StringBuffer objects are mutable and capacity & length affect performance If the StringBuffer object needs to be expanded during an append or insert, a new array is created and the old data copied to it Use capacity() and ensureCapacity(int) methods to minimize expansions

Length v. Capacity The length() method returns the length of the string in the StringBuffer The capacity() method returns the total “space” in a StringBuffer The ensureCapacity(int) method insures the StringBuffer has at least the specified amount of capacity remaining

Length v. Capacity con’t Examples of length() and capacity() methods StringBuffer buf = new StringBuffer(25);// creates StringBuffer with length 25 buf.append("13 Characters"); // appends 13 characters int len = buf.length(); // length() returns 13 int cap = buf.capacity(); // capacity returns 25

Bibliography ct/4/string.htm ml m /jw-1004-java101guide.html