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,

Slides:



Advertisements
Similar presentations
Java
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.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering.
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.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text I/O.
©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.
Introduction to Computer Programming Stringing Along – Using Character and String Data.
Fundamental Programming Structures in Java: Strings.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text.
EXAM 1 REVIEW. days until the AP Computer Science test.
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.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering.
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.
Chapter 7: Characters, Strings, and the StringBuilder.
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,
1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.
String Handling StringBuffer class character class StringTokenizer class.
String and Scanner CS 21a: Introduction to Computing I First Semester,
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
1 The String Class F Constructing a String: F Obtaining String length and Retrieving Individual Characters in a string F String Concatenation (concat)
Strings JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Strings Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
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.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering products.
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.
Department of Computer Engineering Using Objects Computer Programming for International Engineers.
CSC Programming I Lecture 9 September 11, 2002.
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:
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering.
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. For example,
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings Chapter.
String and StringBuffer classes
Section 6.1 Introduction to String Methods. Section 6.1 Introduction to String Methods.
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Strings.
String and String Buffers
Exposure Java 2015 AP®CS Edition Chapter 8 Slides Manipulating Strings
Modern Programming Tools And Techniques-I Lecture 11: String Handling
F4105 JAVA PROGRAMMING F4105 Java Programming
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
Part a: Fundamentals & Class String
Chapter 9 Strings and Text I/O
Exposure Java 2015 Pre-AP®CS Edition Chapter 12 Slides String Methods
Java – String Handling.
Chapter 9 Strings.
Lecture 07 String Jaeki Song.
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 .
Pre-AP® Computer Science Quiz
Presentation transcript:

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, numerical characters and a large set of characters for a variety of purposes like: # $ % ^ & * ( ) _ +

String Literal Definition A string literal is a set of characters delimited with double quotations like: "Seymour Snodgrass" and "SSN: "

// Java1601.java // This program demonstrates how to declare five String objects. // Note that all five string objects store the same information. public class Java1601 { public static void main (String args[]) { String s1 = "Tango"; System.out.println("s1: " + s1); String s2; s2 = "Tango"; System.out.println("s2: " + s2); String s3 = new String("Tango"); System.out.println("s3: " + s3); String s4 = new String(); s4 = "Tango"; System.out.println("s4: " + s4); char Dance[] = {'T','a','n','g','o'}; String s5 = new String(Dance); System.out.println("s5: " + s5); System.out.println(); }

Mathematical Addition = 300 int x = 100; x += 200;

String Concatenation “100” + “200” = “100200” String x = “100”; x += “200”;

// Java1602.java // This program shows how to concatenate strings using the // + operator and the method. public class Java1602 { public static void main (String args[]) { String s1 = "Argentine"; String s2 = "Tango"; String s3 = s1 + " " + s2; System.out.println("s3: " + s3); String s4 = "Argentine"; s4 = s4.concat(" Tango"); System.out.println("s4: " + s4); System.out.println(); }

String Method concat s1 = s2.concat("hiss"); concat concatenates the method argument to the object String. If s2 == "boo" then s1 will become "boohiss"

// Java1603.java // This program demonstrates the use of the method. public class Java1603 { public static void main (String args[]) { String s1 = "Argentine"; String s2 = "Tango"; String s3 = s1 + " " + s2; System.out.println(s1 + " has " + s1.length() + " characters."); System.out.println(s2 + " has " + s2.length() + " characters."); System.out.println(s3 + " has " + s3.length() + " characters."); System.out.println(); }

// Java1604.java // This program demonstrates how to access individual characters of // a String object with the method. public class Java1604 { public static void main (String args[]) { String s1 = "Madam I'm Adam"; String s2 = ""; int N = s1.length() - 1; for (int K = N; K >= 0; K--) s2 += s1.charAt(K); System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println(); }

String Method charAt letter = s.charAt(k); Method charAt returns the character stored at the k th index location of the String object s. The first character is at index 0. If s == "Aardvark" and k == 4 then letter becomes v

// Java1605.java // This program demonstrates how to access specified characters of // a string with the method, where SI is the // StartIndex and EI is one less than the EndIndex. public class Java1605 { public static void main (String args[]) { String s = "Racecar"; int n = s.length(); for (int k = 1; k <= n; k++) System.out.println(s.substring(0,k)); System.out.println(); for (int k = 0; k <= n-3; k++) System.out.println(s.substring(k,k+3)); System.out.println(); }

String Method substring s1 = “Aardvark”; s2 = s1.substring(0,k); Method substring returns a set of consecutive characters from String s1, starting at index 0, and ending at index k-1. s3 = s1.substring(4,8); s2 becomes "Aard" and s3 becomes "vark" s Aardvark

// Java1606.java // This program shows the method, which returns the index of the first // occurrence of the string argument, and the method, which returns the // index of the last occurrence of the string argument. public class Java1606 { public static void main (String args[]) { String s1 = "racecar"; String s2 = "racecar in the carport"; String s3 = "car"; int index1 = s1.indexOf(s3); int index2 = s1.lastIndexOf(s3); int index3 = s2.indexOf(s3); int index4 = s2.lastIndexOf(s3); int index5 = s1.indexOf("qwerty"); System.out.println("With \"" + s1 + "\" car starts at " + index1 + " and last shows up at " + index2); System.out.println("With \"" + s2 + "\" car starts at " + index3 + " and last shows up at " + index4); System.out.println("With \"" + s3 + "\" Qwerty shows up at " + index5); System.out.println(); }

String Methods indexOf & lastIndexOf indexOf returns the first occurrence of a substring s1.indexOf(“hum”); returns 0 s1.indexOf(“ku”); returns 10 s1.indexOf(“qwerty”); returns -1 lastIndexOf returns the last occurrence of a substring s1.lastIndexOf(“hum”); returns 4 s1.lastIndexOf(“ku”); returns 14 s1.lastIndexOf(“qwerty”); returns -1 Note: The i in i ndexOf is lowercase, but in last I ndexOf it’s capital! s humuhumunukunukuapua’a By the way, its the State Fish of Hawaii.

// Java1607.java // This program demonstrates the String // method, which replaces all occurrences of the Old character // with the New character. // This method creates a new String object with the replaced // characters. public class Java1607 { public static void main (String args[]) { String s1 = "racecar"; String s2 = s1.replace('r','l'); System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println(); }

String Method replace String s2 = s1.replace('m','b'); Method replace returns a string such that every occurrence of the old character ( 'm' ) is replaced by the new character ( 'b' ). If s1 == "madam" then s becomes "badab"

// Java1608.java // This program demonstrates the String and // methods. public class Java1608 { public static void main (String args[]) { String s1 = "racecar"; String s2 = "RaCeCaR"; String s3 = "RACECAR100"; String s4 = s1.toUpperCase(); String s5 = s2.toUpperCase(); String s6 = s3.toUpperCase(); System.out.println("s1 --> s4: " + s4); System.out.println("s2 --> s5: " + s5); System.out.println("s3 --> s6: " + s6); System.out.println(); s1 = s4.toLowerCase(); s2 = s5.toLowerCase(); s3 = s6.toLowerCase(); System.out.println("s4 --> s1: " + s1); System.out.println("s5 --> s2: " + s2); System.out.println("s6 --> s3: " + s3); System.out.println(); }

String Methods toUpperCase and toLowerCase s1 = s2.toLowerCase; s3 = s1.toUpperCase; Method toLowerCase returns a string with lower-case letters. Method toUpperCase returns a string with upper-case letters. Any characters that are not letters will be ignored by both methods and returned in their same relative string position.

// Java1609.java // This program demonstrates the method of the String class, // which is shown to convert four data types to a string. // Note that is a static method and must be called using //. public class Java1609 { public static void main (String args[]) { String s1 = String.valueOf(1000); String s2 = String.valueOf( ); String s3 = String.valueOf(true); String s4 = String.valueOf('A'); System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println("s3: " + s3); System.out.println("s4: " + s4); System.out.println(); }

String static Method valueOf String s1 = String.valueOf(1000); String s2 = String.valueOf( ); String s3 = String.valueOf(true); String s4 = String.valueOf('A'); Method valueOf converts the provided parameter and returns a string. Four overloaded valueOf methods are displayed. Note that the valueOf method is a static method (or class method) that is called with the String class identifier.

// Java1610.java // This program converts string values to integer and double values // using the and methods of the // and classes. public class Java1610 { public static void main (String args[]) { String s1 = "12345"; String s2 = " "; int n1 = Integer.parseInt(s1); double n2 = Double.parseDouble(s2); System.out.println(n1 + " + " + n1 + " = " + (n1 + n1)); System.out.println(n2 + " + " + n2 + " = " + (n2 + n2)); System.out.println(); }

Integer static method parseInt and Double static method parseDouble int n1 = Integer.parseInt(s1); double n2 = Double.parseDouble(s2); Method parseInt converts a String into an int. Method parseDouble converts a String into a double. Parameters that include non-numerical characters will compile, but will cause a run-time error.

// Java1611.java // This program checks the equality of two strings with the == operator. // The program executes as you might expect. public class Java1611 { public static void main (String args[]) { String s1 = "Foxtrot"; String s2 = "Waltz"; String s3 = "Foxtrot"; if (s1 == s2) System.out.println(s1 + " == " + s2); else System.out.println(s1 + " != " + s2); if (s1 == s3) System.out.println(s1 + " == " + s3); else System.out.println(s1 + " != " + s3); System.out.println(); }

// Java1612.java // This program checks equality of strings, but this time a string entered at the keyboard // is used for comparison. This program has unexpected results. import java.util.Scanner; public class Java1612 { public static void main (String args[]) { Scanner input = new Scanner(System.in); String s1 = "Foxtrot"; String s2 = "Waltz"; System.out.print("Enter a string ===>> "); String s3 = input.nextLine(); if (s1 == s2) System.out.println(s1 + " == " + s2); else System.out.println(s1 + " != " + s2); if (s1 == s3) System.out.println(s1 + " == " + s3); else System.out.println(s1 + " != " + s3); System.out.println(); }

// Java1613.java // This program uses the method, which removes "white space" from both ends of // the string argument. This method is used to try and solve the problem of the previous // program. import java.util.Scanner; public class Java1613 { public static void main (String args[]) { Scanner input = new Scanner(System.in); String s1 = "Foxtrot"; String s2 = "Waltz"; System.out.print("Enter a string ===>> "); String s3 = input.nextLine(); if (s1 == s2) System.out.println(s1 + " == " + s2); else System.out.println(s1 + " != " + s2); if (s1 == s3. trim ()) System.out.println(s1 + " == " + s3); else System.out.println(s1 + " != " + s3); System.out.println(); }

String Method trim s1 = s2.trim(); String method trim returns a string with "white space" removed from both ends of the String object.

// Java1614.java // This program demonstrates the method, which is capable of // testing equality of string objects correctly. import java.util.Scanner; public class Java1614 { public static void main (String args[]) { Scanner input = new Scanner(System.in); String s1 = "Foxtrot"; String s2 = "Waltz"; System.out.print("Enter a string ===>> "); String s3 = input.readLine(); if ( s1.equals(s2) ) System.out.println(s1 + " == " + s2); else System.out.println(s1 + " != " + s2); if ( s1.equals(s3) ) System.out.println(s1 + " == " + s3); else System.out.println(s1 + " != " + s3); System.out.println(); }

What Is Going On? Part 1 dff6ccd Foxtrot 3b0eb0 Waltz 18d107f Foxtrot

What Is Going On? Part 2 dff6ccd Foxtrot 3b0eb0 Waltz 18d107f Foxtrot

// Java1615.java // This program demonstrates the method, which returns an integer value. // The value is 0 when the strings are equal, otherwise a value is returned that // indicates the relative distance between the strings. public class Java1615 { public static void main (String args[]) { String s1 = "AARDVARK"; String s2 = "ZEBRA"; String s3 = "AARDVARK"; String s4 = "BART"; int value1 = s1.compareTo(s2); int value2 = s1.compareTo(s3); int value3 = s2.compareTo(s1); int value4 = s1.compareTo(s4); System.out.println("value1: " + value1); System.out.println("value2: " + value2); System.out.println("value3: " + value3); System.out.println("value4: " + value4); System.out.println(); }

String methods equals and compareTo if (s1.equals(s2)) int distance = s3.compareTo(s4); Method equals returns true is s1 == s2, and false otherwise. Method compareTo returns 0 if s3 == s4, otherwise an integer is returned based on the difference between s3 and s4. If s3 < s4, the returned value is negative. If s3 > s4, the returned value is positive.

String Objects are Immutable A mutator is a method that mutates or alters object values. The character contents of a String object cannot be altered, which means that String objects are immutable. You need to construct a StringBuffer object if it is necessary to change the character content after the object is constructed.

What do you mean "A String cannot be altered?!" If you look at these statements: it sure looks like name is being altered. In reality what happens is the old String object is destroyed and a new object (with a new memory address) is created. String name = "Bob"; name = "Joe";

// Java1616.java // This program introduces the class. // Three different approaches are shown to construct a StringBuffer object. // You cannot assign a String to a StringBuffer object. class Java1616 { public static void main (String args[]) { // constructs a StringBuffer object with capacity 16 and no characters StringBuffer s1 = new StringBuffer(); // s1 = "This will not compile"; System.out.println("s1: " + s1); // constructs a StringBuffer object with capacity 50 and no characters StringBuffer s2 = new StringBuffer(50); System.out.println("s2: " + s2); // constructs a StringBuffer object with required capacity and stores the string argument StringBuffer s3 = new StringBuffer("The quick brown fox jumps over the lazy dog"); System.out.println("s3: " + s3); System.out.println(); }

// Java1617.java // This program demonstrates the method, which inserts a new string at a // specified location. // The method automatically allocates the required StringBuffer space. public class Java1617 { public static void main (String args[]) { StringBuffer s1 = new StringBuffer(); s1.insert(0,"The quick brown fox jumps over the lazy dog"); System.out.println("s1: " + s1); StringBuffer s2 = new StringBuffer(50); s2.insert(0,"The quick brown fox jumps over the lazy dog"); System.out.println("s2: " + s2); StringBuffer s3 = new StringBuffer("The quick brown fox jumps over the lazy dog"); s3.insert(4,"ever so "); System.out.println("s3: " + s3); System.out.println(); }

StringBuffer Method insert s.insert(index,newString); Method insert adds newString to the existing StringBuffer object s, at the index character location.

// Java1618.java This program demonstrates the, and methods. // length returns the size of the stored String. capacity returns the size of the StringBuffer, // and setLength alters the size of the StringBuffer. // Note how 16 extra spaces are constructed with a string argument construction. public class Java1618 { public static void main (String args[]) { StringBuffer s1 = new StringBuffer(); System.out.println("Length: " + s1.length()); System.out.println("Capacity: " + s1.capacity()); s1.insert(0,"The quick brown fox jumps over the lazy dog"); System.out.println("Length: " + s1.length()); System.out.println("Capacity: " + s1.capacity()); System.out.println(); StringBuffer s2 = new StringBuffer(50); System.out.println("Length: " + s2.length()); System.out.println("Capacity: " + s2.capacity()); s2.insert(0,"The quick brown fox jumps over the lazy dog"); System.out.println("Length: " + s2.length()); System.out.println("Capacity: " + s2.capacity()); System.out.println(); StringBuffer s3 = new StringBuffer("The quick brown fox jumps over the lazy dog"); System.out.println("Length: " + s3.length()); System.out.println("Capacity: " + s3.capacity()); s3.insert(4,"ever so "); System.out.println("Length: " + s3.length()); System.out.println("Capacity: " + s3.capacity()); System.out.println(); s1.setLength(1000); System.out.println("Length: " + s1.length()); System.out.println("Capacity: " + s1.capacity()); System.out.println(s1); }

StringBuffer Methods length, capacity and setLength int len = s.length(); int cap = s.capacity(); s.setLength(1000); Method length returns the size of the StringBuffer object. Method capacity returns buffer capacity for total characters. Method setLength sets the size of the StringBuffer object and appends the string with spaces.

// Java1619.java // This program demonstrates how to use the method to // access individual characters in a StringBuffer object. public class Java1619 { public static void main (String args[]) { StringBuffer s1 = new StringBuffer( "Madam I'm Adam"); StringBuffer s2 = new StringBuffer(100); int n = s1.length(); for (int k = 0; k < n; k++) s2.insert(k, s1.charAt(n-k-1)); System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println(); }

// Java1620.java // This program demonstrates how the method // replaces characters in a StringBuffer object. public class Java1620 { public static void main (String args[]) { StringBuffer s = new StringBuffer( "Aardvark"); System.out.println(s); s.setCharAt(0,'E'); s.setCharAt(1,'e'); s.setCharAt(5,'e'); System.out.println(s); System.out.println(); }

StringBuffer Method setCharAt s.setCharAt(5,'A'); Method setCharAt replaces the character at index 5 of the s object with character 'A'.

// Java1621.java // This program demonstrates the method, // which deletes a substring from a StringBuffer object // from StartingIndex to EndIndex-1. public class Java1621 { public static void main (String args[]) { StringBuffer s = new StringBuffer( "The quick brown fox jumps over the lazy dog"); System.out.println(s); s.delete(9,15); System.out.println(s); s.delete(3,9); System.out.println(s); System.out.println(); }

StringBuffer Method delete s.delete(4,7); Method delete removes a substring from s starting at index 4 and ending at index 6. If s == "Discombobulated" then after the s.delete(4,7); call, s becomes "Discobulated".

// Java1622.java // This program demonstrates the method, // which replaces a substring from a StringBuffer object // from StartingIndex to EndIndex-1 with string S. public class Java1622 { public static void main (String args[]) { StringBuffer s = new StringBuffer( "The quick brown fox jumps over the lazy dog"); System.out.println(s); s.replace(10,15,"red"); System.out.println(s); s.replace(4,9,"slow"); System.out.println(s); System.out.println(); }

StringBuffer Method replace s.replace(0,3,"lear"); Method replace deletes a substring of s from start index 0 to last index 2, and replaces this space with "lear". If s == "burning" then after replace, s == "learning".