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.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
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.
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.
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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
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.
© 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.
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.
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.
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.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
CSC Programming I Lecture 9 September 11, 2002.
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.
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 Methods in the String class Manipulating text in Java.
The Methods and What You Need to Know for the AP Exam
Strings A String is a sequence of letters
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
University of Central Florida COP 3330 Object Oriented Programming
Programming in Java Text Books :
Strings Part 1 Taken from notes by Dr. Neil Moore
Primitive Types Vs. Reference Types, Strings, Enumerations
Advanced Programming Behnam Hatami Fall 2017.
String Objects & its Methods
Modern Programming Tools And Techniques-I Lecture 11: String Handling
Java Strings Slides provided by the University of Washington Computer Science & Engineering department.
String and StringBuilder
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
String and StringBuilder
Exposure Java 2015 Pre-AP®CS Edition Chapter 12 Slides String Methods
Tonga Institute of Higher Education
Logical Operations In Matlab.
16 Strings.
Goals Understand how to create and compare Strings.
String and StringBuilder
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Introduction to Computer Science
String methods 26-Apr-19.
String Methods.
Strings in Java.
Exam Prep.
STRINGS BY: DANIEL, JUSTIN, PANI.
Strings in Java Strings in Java are also a reference type.
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:

Methods in the String class Manipulating text in Java 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. Character M i c r o s f t Index 1 2 3 4 5 6 7 8

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. Character M i c r o s f t Index 1 2 3 4 5 6 7 8 “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 Character M i c r o s f t Index 1 2 3 4 5 6 7 8 “Microsoft”.indexOf( “Micro” ); returns 0 “Microsoft”.indexOf( “soft” ); returns 5 “Microsoft”.indexOf( “icro” ); returns 1

indexOf Character M i c r o s f t Index 1 2 3 4 5 6 7 8 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 Character M i c r o s f t Index 1 2 3 4 5 6 7 8 “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 Character M i c r o s f t Index 1 2 3 4 5 6 7 8 “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. We’ve discussed these for comparison with keys in Greenfoot: 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

Example

Example Write a method to check if a password meets the following criteria Contains a number Contains an uppercase letter Is at least 9 letters long

Example public boolean passwordOK( String pwd ) { boolean hasUppercase = false; for( int n = 0; n < pwd.length() - 1; n++ ) String ch = pwd.substring(n, n+1); if(ch.compareTo(“A”) >= 0 && ch.compareTo(“Z”) <= 0) hasUppercase = true; } boolean hasDigit = false; if(ch.compareTo(“0”) >= 0 && ch.compareTo(“9”) <= 0) hasDigit = true; return pwd.length() >= 9 && hasDigit && hasUppercase;

Example Write a method to count the number of times the word “Java” appears in a string

Example public int count(String words) { String target = “Java”; int foundAt = words.indexOf( target ); int count = 0; while( foundAt >= 0 ) count++; foundAt = words.indexOf( target, foundAt + target.length()); } return count;

Example Let’s say * is a wildcard and we want to know if a String contains a particular substring public int countPattern( String source, String target ) countPattern( “Hello World”, “orl” )  1 countPattern( “He**o World”, “Help”)  1 countPattern( “H**lo World”, “He” )  2 (once for He, and **)

Example public int countPattern(String source, String target) { int count = 0; for(int n = 0; n < source.length() - 1; n++) int matches = 0; for(int k = 0; k < target.length() – 1 && n + k < source.length() - 1; k++) String sourceChar = source.substring(n + k, n + k +1); String targetChar = target.substring(k, k+1); if(sourceChar.compareTo(“*”) == 0)//wildcard matches++; else if( sourceChar.compareTo( targetChar ) == 0 ) else k = target.length();//stop the loop  can also use break but not as elegant } if(matches == target.length()) count++; return count;