Strings Methods in the String class Manipulating text in Java.

Slides:



Advertisements
Similar presentations
Java
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
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.
Chapter 10 Review. Write a method that returns true is s1 and s2 end with the same character; otherwise return false. Sample Answer: public boolean lastChar(String.
Java Strings in 10 minutes
©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.
Strings In Java, strings are contained in an object that is an instance of the String class. String in java is the name of a class. That’s why it starts.
Strings Reading for this Lecture, L&L, 3.2. Strings String is basically just a collection of characters. Thus, the string “Martyn” could be thought of.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
© 2007 Lawrenceville Press Slide 1 Chapter 6 The while Statement  Loop structure that executes a set of statements as long as a condition is true  The.
Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text.
Objects and Classes; Strings. 2 Classes and objects class: A program entity that represents either 1.A program / module, or 2.A type of objects* –A class.
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.
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.
ROUND 1 Name a method associated with class String 1.) 15 compareTo() 26 indexOf() 34 length() 2.) 3.) 4.) 3 toUpper() 7 substring() 11 charAt() 5.)
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
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.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering products.
CSC Programming I Lecture 9 September 11, 2002.
CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming strings.
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, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
CHAPTER 6 GC Strings. THE CLASS STRING  Contains operations to manipulate strings.  String:  Sequence of zero or more characters.  Enclosed.
Chapter 8 String Manipulation
The Methods and What You Need to Know for the AP Exam
Introduction to programming in java
Computer Programming ||
Strings, Characters and Regular Expressions
David Meredith Aalborg University
Programming in Java Text Books :
String Handling in JAVA
Java Strings Slides provided by the University of Washington Computer Science & Engineering department.
Working with Text and Numbers in Java
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Part a: Fundamentals & Class String
Exposure Java 2015 Pre-AP®CS Edition Chapter 12 Slides String Methods
Tonga Institute of Higher Education
16 Strings.
Goals Understand how to create and compare Strings.
String Processing 1 MIS 3406 Department of MIS Fox School of Business
String Processing 1 MIS 3406 Department of MIS Fox School of Business
String Methods.
Strings in Java.
Exam Prep.
Strings in Java Strings in Java are also a reference type.
Methods in the String class Manipulating text in Java
Pre-AP® Computer Science Quiz
Unit-2 Objects and Classes
String Methods Strings have actions known as method. We will review a few of the methods associated with strings that are a part of the built in Java.
What We Want To Do User enters: Mary Smith
Presentation transcript:

Strings Methods in the String class Manipulating text in Java

The String Class ► Like all standard java classes, there is a javadoc for the String class that can be helpful to you. ► The methods our class is responsible for are:  length()  indexOf( String )  indexOf( String, int )  substring( int, int )  substring( int )

Strings ► A string is a series of characters, also known as an array. ► Arrays are 0-based meaning we start counting at 0 rather than one ► Ex. CharacterMicrosoft Index

Length ► Strings can tell you how many characters they have by using the length method:  Ex. ► String name1 = new String( “Sally” ); ► String name2 = new String( “John” ); ► String name3 = new String( “Bob” ); ► String name4 = new String( “Mary Sue” ); ► System.out.println( name1.length() );//prints 5 ► System.out.println( name2.length() );//prints 4 ► System.out.println( name3.length() );//prints 3 ► System.out.println( name4.length() );//prints 8  White space counts when asking for length!

Substrings ► Strings have a method called substring that can give you pieces of the string ► substring takes the starting index and one past the stopping index ► Ex. CharacterMicrosoft Index  “Microsoft”.substring( 0, 5 );  returns “Micro”  “Microsoft”.substring( 5, 9 );  returns “Soft”  “Microsoft”.substring( 3, 7 );  returns “roso”

Substring ► substring has another form: ► if you give substring the starting index only, it goes to the end of the String  “Microsoft”.substring( 3 ); //returns “rosoft”  “Microsoft”.substring( 5 ); //returns “soft”  “Microsoft”.substring( 7 ); //returns “ft”

indexOf ► The indexOf method returns the index where a substring is found CharacterMicrosoft Index  “Microsoft”.indexOf( “Micro” );  returns 0  “Microsoft”.indexOf( “soft” );  returns 5  “Microsoft”.indexOf( “icro” );  returns 1

indexOf ► When a character is not found indexOf returns -1, an invalid index, to let the caller know ► When a character or substring can be found more than once, indexOf returns the first occurrence CharacterMicrosoft Index  “Microsoft”.indexOf( “m” );  returns -1  “Microsoft”.indexOf( “apple” );  Returns -1  “Microsoft”.indexOf( “o” );  returns 4

indexOf ► indexOf has another parameter ► You can tell it where to start looking CharacterMicrosoft Index  “Microsoft”.indexOf( “o”, 5 );  returns 6;//skips past the first ‘o’ by starting at index 5  “Microsoft”.indexOf( “s”, 3 );  returns 5;// still finds the first s  “Microsoft”.indexOf( “M”, 5 );  returns -1;// no ‘M’ after index 5

The Last Index ► Finding the last index in a String means you’ll need to use the length() method ► Remember, Strings are 0-based! ► The last index is length() – 1 ► Ex.  String name = new String( “Billy” ); String lastChar = new String(); int lastIndex = name.length() – 1; lastChar = name.substring( lastIndex ); System.out.println( lastChar );//prints ‘y’

Other String Methods ► I encourage you to refer to the JavaDoc online for Strings, there are many methods that you may find helpful as we create new projects. ► Later on I will review with you the methods:  equals  compareTo ► Some suggestions that can be helpful:  replace  trim  valueOf  toUpperCase  toLowerCase  matches ► You’ll also need to learn about a very useful programming concept called regular expressions in order to use this method