1 Working with String Duo Wei CS110A_004. 2 Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized.

Slides:



Advertisements
Similar presentations
9 Copyright © 2005, Oracle. All rights reserved. Using Strings, String Buffer, Wrapper, and Text-Formatting Classes.
Advertisements

Java
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
String Pemrograman Berbasis Obyek Oleh Tita Karlita.
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 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.
CIS 234: Strings. What Is a String? series of characters enclosed in double quotes characters can include letters (lower case and upper case), digits,
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
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.
The String Class. Objectives: Learn about literal strings Learn about String constructors Learn about commonly used methods Understand immutability of.
Class T{ public static int id; public static int num = 5; public int n; public boolean c; public static void setNumberOfBicycles(int val) { num=val; //
String Class in Java java.lang Class String java.lang.Object java.lang.String java.lang.Object We do not have to import the String class since it comes.
String Class. Objectives and Goals Identify 2 types of Strings: Literal & Symbolic. Learn about String constructors and commonly used methods Learn several.
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.
The java.lang Package chapter 8 Java Certification Study Group February 2, 1998 Seth Ladd.
String Manipulation Chapter 15 This chapter explains the String facilities. You have already seen some of the main methods of the String class.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Strings Carol Yarbrough AP Computer Science Instructor Alabama School of Fine Arts.
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.
1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.
Geoff Holmes Date Math Weighted Distr Strings String methods Tokenizers System Examples Utility Classes (Chapter 17) import java.util.*;
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
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.
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,
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.
Strings and ArrayLists April 23, 2012 ASFA AP CS.
CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate 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.
"Chapter 9" Strings Java Methods Maria Litvin Gary Litvin
The String Class.
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
EKT 472: Object Oriented Programming
String and String Buffers
String Class.
Strings Chapter 6.
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
Java – String Handling.
Lecture 07 String Jaeki Song.
CS2011 Introduction to Programming I Strings
Strings in Java.
Exam Prep.
Switch, Strings, and ArrayLists in Java
In Java, strings are objects that belong to class java.lang.String .
Strings in Java Strings in Java are also a reference type.
Pre-AP® Computer Science Quiz
Week 3 - Friday COMP 1600.
Unit-2 Objects and Classes
What We Want To Do User enters: Mary Smith
Presentation transcript:

1 Working with String Duo Wei CS110A_004

2 Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized string: String s1 = ""; String s2 = new String(); Empty strings private String errorMsg; errorMsg is null

3 Constructors String’s no-args and copy constructors are not used much. Other constructors convert arrays into strings (used in a lab in Chapter 10). String s1 = new String (); String s2 = new String (s1); String s1 = ""; String s2 = s1;

4 Methods — length, charAt int length (); char charAt (k); Returns the number of characters in the string Returns the k-th char 6 ’n' ”Flower".length(); ”Wind".charAt (2); Returns: Character positions in strings are numbered starting from 0

5 Methods — substring String s2 = s.substring (i, k);  returns the substring of chars in positions from i to k-1 String s2 = s.substring (i);  returns the substring from the i-th char to the end ”raw" "happy" "" (empty string) ”strawberry".substring (2,5); "unhappy".substring (2); "emptiness".substring (9); Returns: strawberry i k

6 Methods — Concatenation String result = s1 + s2;  concatenates s1 and s2 String result = s1.concat (s2);  the same as s1 + s2 result += s3;  concatenates s3 to result result += num;  converts num to String and concatenates it to result

7 Methods — Find (indexOf) String date = "July 5, :28:19 PM" ; date.indexOf ('J'); 0 date.indexOf ('2'); 8 date.indexOf ("2012"); 8 date.indexOf ('2', 9); 11 date.indexOf ("2020"); -1 date.lastIndexOf ('2'); Returns: (not found) (starts searching at position 9)

8 Methods — Comparisons boolean b = s1.equals(s2);  returns true if the string s1 is equal to s2 boolean b = s1.equalsIgnoreCase(s2);  returns true if the string s1 matches s2, case-blind int diff = s1.compareTo(s2);  returns the “difference” s1 - s2 int diff = s1.compareToIgnoreCase(s2);  returns the “difference” s1 - s2, case-blind

9 Methods — Replacements String s2 = s1.trim ();  returns a new string formed from s1 by removing white space at both ends String s2 = s1.replace(oldCh, newCh);  returns a new string formed from s1 by replacing all occurrences of oldCh with newCh String s2 = s1.toUpperCase(); String s2 = s1.toLowerCase();  returns a new string formed from s1 by converting its characters to upper (lower) case

10 Replacements (cont’d) Example: how to convert s1 to upper case A common bug: s1 = s1.toUpperCase(); s1.toUpperCase(); s1 remains unchanged

11 Methods — toString It is customary to provide a toString method for your class. toString converts an object into a String (for printing it out, for debugging, etc.). is the same as System.out.print (obj); System.out.print (obj.toString());

12 Numbers to Strings and Strings to Numbers Integer and Double are “wrapper” classes from java.lang that represent numbers as objects. Integer and Double provide useful static methods for conversions: String s1 = Integer.toString (i); String s2 = Double.toString (d); int n = Integer.parseInt (s1); double x = Double.parseDouble (s2); int i; double d;

13 Numbers to Strings Three ways to convert a number into a string: 1. String s = "" + num; 2. String s = Integer.toString (i); String s = Double.toString (d); 3. String s = String.valueOf (num); int i; double d;