Computer Programming 2 Lab (1) I.Fatimah Alzahrani.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Getting Started with Java.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
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.
Java Programming Strings Chapter 7.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Ch2: Getting Started with Java - Objectives After.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
1 Strings and String Operations Overview l Creating String Objects l Substring methods l The Concatenation Operator l Strings are Immutable l Other Methods.
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.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Applications of Regular Expressions BY— NIKHIL KUMAR KATTE 1.
2.2 Information on Program Appearance and Printing.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
Getting Started with Java Recitation – 1/23/2009 CS 180 Department of Computer Science, Purdue University.
SE-1010 Dr. Mark L. Hornick 1 Some Java Classes using & calling methodsS.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text.
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
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.
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
Strings JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
Chapter 2 Getting Started with Java. Objectives After you have read and studied this chapter, you should be able to Identify the basic components of Java.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
CSI 3125, Preliminaries, page 1 Compiling the Program.
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.
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 Can view as array of chartacters –Implemented as a class with its own methods Simplest operation on strings is catenating two strings String x.
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.
Strings and I/O. Lotsa String Stuff… There are close to 50 methods defined in the String class. We will introduce several of them here: charAt, substring,
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
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.
String & Exception. Lesson plan Class & Object String Exercise for midterm.
1 reading: 3.3 Using objects. Objects So far, we have seen: methods, which represent behavior variables, which represent data (categorized by types) It.
Chapter 8 String Manipulation
Strings, Characters and Regular Expressions
String class.
Programming in Java Text Books :
String Handling in JAVA
Primitive Types Vs. Reference Types, Strings, Enumerations
Modern Programming Tools And Techniques-I Lecture 11: String Handling
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
An Introduction to Java – Part I, language basics
Chapter 9 Strings and Text I/O
Microsoft Visual Basic 2005: Reloaded Second Edition
Bryan Burlingame 13 March 2019
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Strings CSE 1310 – Introduction to Computers and Programming
Strings in Java.
Getting Started with Java
Exam Prep.
More on iterations using
Unit-2 Objects and Classes
What We Want To Do User enters: Mary Smith
Presentation transcript:

Computer Programming 2 Lab (1) I.Fatimah Alzahrani

String A sequence of characters separated by double quotes is String constants. As String is a class, we can create an instance and give it a name. For example,

Unlike in other classes, the explicit use of new to create an instance is optional for the String class. We can create a new String object, for example, in this way

There are close to 50 methods defined in the String class. We will introduce four of them here: substring, length, indexOf, and charAt

substring We can extract a substring from a given string by specifying the beginning and ending positions. For example,

Note Individual characters in a String object are indexed from 0, The first argument of the substring method specifies the position of the first character, and the second argument specifies the value that is 1 more than the position of the last character.

Error An error will result if you pass invalid arguments, such as negative values, the second argument larger than the number of characters in a string, or the first argument larger than the second argument

Length We can find out the number of characters in a String object by using the length method. For example, if the name text refers to a string Espresso, then will return the value 8, because there are eight characters in the string. Here are some more examples:

indexOf To locate the index position of a substring within another string, we use the indexOf method. For example, if the name text refers to a string I Love Java, then

will return the value 2, the index position of the first character of the designated string Love. If the searched substring is not located in the string, then -1 is returned. Notice that the search is done in a case- sensitive manner. Thus,

will return -1. If there is more than one occurrence of the same substring, the index position of the first character of the first matching substring is returned. Here are some more examples:

charAt This method returns the character located at the String's specified index. For example Code Output : public class Test { public static void main(String args[]) { String s = "Strings are immutable"; char result = s.charAt(8); System.out.println(result); } a

String Concatenation String concatenation. We can create a new string from two strings by concatenating the two strings. We use the plus symbol (+) for string concatenation. Here are the examples:

Reference (BOOK) A Comprehensive Introduction to Object- Oriented Programming with Java, C.Thomas