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.

Slides:



Advertisements
Similar presentations
Strings in Java 1. strings in java are handled by two classes String &
Advertisements

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.
Java Programming Strings Chapter 7.
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.
Introduction to Objects and Input/Output
Programming 2 CS112- Lab 2 Java
Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
COMP 14 Introduction to Programming Miguel A. Otaduy May 17, 2004.
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 29, 2005.
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,
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.
String StringBuffer. class StringExample { public static void main (String[] args) { String str1 = "Seize the day"; String str2 = new String(); String.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
28-Jun-15 String and StringBuilder Part I: String.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
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.
Strings.
From C++ to Java A whirlwind tour of Java for C++ programmers.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Working with string In java Four classes are provided to work with String:1) String 2)String Buffer 3)String Tokenizer 4)String Builder Note: An object.
Objectives ► Become familiar with the class String ► ► Learn how to use input and output dialog boxes in a program ► ► Learn how to tokenize the input.
1 CSC 221: Computer Programming I Spring 2010 Design & text processing  design principles  cohesion & coupling  objects vs. primitives  String methods:
Using Java Class Library
Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,
CHAPTER 9 Text Processing and More about Wrapper Classes Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
String Handling StringBuffer class character class StringTokenizer class.
String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
Strings Mr. Smith AP Computer Science A. What are Strings? Name some of the characteristics of strings: A string is a sequence of characters, such as.
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.
CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings.
CSCI 51 Introduction to Computer Science Joshua Stough February 3, 2009.
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 handling. when you create a String object, you are creating a string that cannot be changed. That is, once a String object has been created, you.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
17-Feb-16 String and StringBuilder Part I: String.
Declaring variables The type could be: int double char String name is anything you want like lowerCaseWord.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
Chapter 8: Loops, Arrays, Strings Loop statements –do –while –for Arrays –declaration, allocation, initialization, access –multi-dimensional –heterogeneous.
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
(Dreaded) Quiz 2 Next Monday.
Chapter 8 String Manipulation
The String class is defined in the java.lang package
String and StringBuffer classes
C# - Strings.
Programming in Java Text Books :
String and String Buffers
String Handling in JAVA
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
Chapter 7: Strings and Characters
Object Oriented Programming
תרגול מס' 3 עבודה עם מחרוזות (Strings) מתודות (Methods) העברת פרמטרים
String and StringBuilder
String and StringBuilder
Java – String Handling.
16 Strings.
String and StringBuilder
String methods 26-Apr-19.
String Methods.
Strings in Java.
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

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 package containing class (java.lang) 3.Name of method (pow), its parameters (int a, int b), and function (a^b) 4.Remember to import the appropriate package.

Program Example import java.lang.*; public class TestPowerFunction { // main(): application entry point static public void main(String args[]) { double answer; answer = Math.pow(2,3); System.out.println("ANSWER 2^3) = " + answer); } }

The class String String variables are reference variables Given String name; –Equivalent Statements: name = new String("Lisa Johnson"); name = “Lisa Johnson”;

The class String The String object is an instance of class String The value “Lisa Johnson” is instantiated The address of the value is stored in name String methods are called using the dot operator

Commonly Used String Methods LENGTH: –int length(String str) Length of string. GETTING PARTS: –char charAt(int index) Character at position index. –String substring(int startIndex, int stopIndex) String (substring) starting at startIndex until stopIndex-1. –String substring(int startIndex) String (substring) starting at startIndex until end of the string.

Commonly Used String Methods SEARCHING: All ‘ indexOf ’ methods return int values. All ‘ indexOf ’ methods return -1 if the string/char is not found. –int indexOf(char ch) Index of 1 st occurrence of the character. –int indexOf(char ch, int pos) Index of 1 st occurrence of the character. Parameter pos specifies from where to begin the search. –int indexOf(String str) Index of 1 st occurrence of the string. –int indexOf(String str, int pos) Index of 1 st occurrence of the string. Parameter pos specifies from where to begin the search.

Commonly Used String Methods CREATING A NEW STRING FROM THE ORIGINAL: Returns a String. –String toLowerCase() New String with all chars lowercase –String toUpperCase() New String with all chars uppercase –String replace(char c1, char c2) New String in which every occurrence of c1 is replaced with c2.

Commonly Used String Methods COMPARISON: Use these instead of == and != –int compareTo(String str) Compares 2 strings character by character. Returns 0 / negative / positive –boolean equals(String str) Returns true if 2 strings have equal values. Otherwise false. –boolean equalsIgnoreCase(String str) Same as above, but upper & lower case are the same.

CLASS EXERCISE public class VariousStringMethods { public static void main (String[] args) { String sentence,str1,str2,str3; int index; sentence = "Now is the time for the birthday party"; //Line 1 System.out.println("Line 2: sentence = \"" + sentence + "\""); //Line 2 System.out.println("Line 3:The length of sentence = " + sentence.length()); //Line 3 System.out.println("Line 4:The character at index 16 in " + "sentence = " + sentence.charAt(16)); //Line 4

System.out.println("Line 5: The index of first t in " + "sentence = " + sentence.indexOf('t')); //Line 5 System.out.println("Line 6: The index of for in sentence " + "= " + sentence.indexOf("for")); //Line 6 System.out.println("Line 7: sentence in uppercase = \"" + sentence.toUpperCase() + "\""); //Line 7 index = sentence.indexOf("birthday"); //Line 8 str1 = sentence.substring(index, index + 14); //Line 9 System.out.println("Line 10: str1 = \"" + str1 + "\""); //Line 10 str2 = "Super " + sentence.substring(index, index + 14); //Line 11 System.out.println("Line 12: str2 = \"" + str2 + "\""); //Line 12 str3 = sentence.replace('t', 'T'); //Line 13 System.out.println("Line 14: str3 = \"" + str3 + "\""); //Line 14 }