F4105 JAVA PROGRAMMING F4105 Java Programming

Slides:



Advertisements
Similar presentations
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
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.
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.
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.
Strings and Arrays The objectives of this chapter are:  To discuss the String class and some of its methods  To discuss the creation and use of Arrays.
Programming 2 CS112- Lab 2 Java
String class  Construct a string  String str = new String(“welcome”);  Char[] charr = {‘G’, ‘o’, ‘o’, ‘d’};  String mes = new String(charr);  A full.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Characters, String and Regular expressions. Characters char data type is used to represent a single character. Characters are stored in a computer memory.
String Class. Objectives and Goals Identify 2 types of Strings: Literal & Symbolic. Learn about String constructors and commonly used methods Learn several.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Chapter 7: Characters, Strings, and the StringBuilder.
String Handling StringBuffer class character class StringTokenizer class.
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.
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.
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:
17-Feb-16 String and StringBuilder Part I: String.
1 Unit-2 Arrays, Strings and Collections. 2 Arrays - Introduction An array is a group of contiguous or related data items that share a common name. Used.
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.
ICS3U – StringBuffer Teacher: Mr. Ho Course URL:
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,
Chapter 8 String Manipulation
The String class is defined in the java.lang package
String and StringBuffer classes
Strings, Characters and Regular Expressions
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
EKT 472: Object Oriented Programming
Programming in Java Text Books :
String and String Buffers
Modern Programming Tools And Techniques-I Lecture 11: String Handling
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.
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
What We Want To Do User enters: Mary Smith
Presentation transcript:

F4105 JAVA PROGRAMMING F4105 Java Programming Introduction To Java Programming 1.0 Basic Concepts of Java Programming 2.0 Classes, Polymorphism and Inheritance 3.0 Exception handling 4.0

3.0 CLASS, POLYMORPHISM AND INHERITANCE 3.4 String

LEARNING OUTCOMES TOPIC 3.4 F4105 JAVA PROGRAMMING By the end of this chapter students shall be able to : Declare and manipulate data of char type. 1 Explain String references as parameters 2 Differentiate between the String and StringBuffer class. 3 Construct programme using method in class String 4

Introduction A string is a sequence of characters. F4105 JAVAPROGRAMMING Introduction A string is a sequence of characters.

String Declaration Example 1 char str [ ] = {'a','e','i','o','u'}; F4105 JAVAPROGRAMMING String Declaration Example 1 char str [ ] = {'a','e','i','o','u'};

String Declaration Example 2 F4105 JAVAPROGRAMMING String Declaration Example 2 char text[ ] = {'I','','C','A','N','','W','R','I','T', 'E','A','','G','O','O','D','','P','R','O','G','R','A','M','I','N','','J','A','V','A'}; Example 3 String text = "I CAN WRITE A GOOD PROGRAM IN JAVA";

class String class StringBuffer F4105 JAVAPROGRAMMING java.lang Package class String class StringBuffer method method

String Class String class is present in java.lang package. F4105 JAVAPROGRAMMING String Class String class is present in java.lang package. The methods in this class are used to manipulate strings. The string objects created in a String class cannot be changed. Example: String obj = new String("Welcome"); obj.append("_World"); System.out.println(obj); wrong

Creating String Object F4105 JAVAPROGRAMMING Creating String Object Example 1: Syntax String stringobject = new String( ); Example: String s=new String(“Java”);

Creating String Object F4105 JAVAPROGRAMMING Creating String Object Example 2 Syntax String stringobject = <string value>; Example: String s = "I have to study for my exams";

Creating String Object F4105 JAVAPROGRAMMING Creating String Object Example 3 Syntax String stringobject = new String(array_name); Example: char arr[ ] = {'H','E','L','L','O'}; String s = new String(arr);

Creating String Object F4105 JAVAPROGRAMMING Creating String Object Example 4 Syntax String stringobject = new String(stringobject); Example: char arr[ ] = {'H','E','L','L','O'}; String s = new String(arr); String s1 = new String(s);

F4105 JAVAPROGRAMMING String Methods The String class has several methods that can be used to manipulate the string values. Each method has its own characteristic.

length() method Is used to find the number of characters in a string. F4105 JAVAPROGRAMMING length() method Is used to find the number of characters in a string.

length() method Example 1 String s = "I am going to school"; F4105 JAVAPROGRAMMING length() method Example 1 String s = "I am going to school"; int numchar = s.length( ); System.out.println(numchar); Output 20

length() method Example 2 String s = " "; int numchar = s.length( ); F4105 JAVAPROGRAMMING length() method Example 2 String s = " "; int numchar = s.length( ); System.out.println(numchar); Output 2

length() method Syntax int variable_name = stringobject.length( ); F4105 JAVAPROGRAMMING length() method Syntax int variable_name = stringobject.length( );

concat() method Example 1 String firststring = "The Twin Tower"; F4105 JAVAPROGRAMMING concat() method Example 1 String firststring = "The Twin Tower"; String secondstring = " looks beautiful"; String thirdstring = firststring.concat(secondstring); @ firststring+secondstring Output The Twin Tower looks beautiful

concat() method Syntax F4105 JAVAPROGRAMMING concat() method Syntax String Stringvariable3 = stringvariable1.concat (stringvariable2);

concat() method Example 2 String s1 = "Result: "+5+1; F4105 JAVAPROGRAMMING concat() method Example 2 String s1 = "Result: "+5+1; System.out.println(s1); Output Result: 51

concat() method Example 3 String s2 = "Result: "+(5+1); F4105 JAVAPROGRAMMING concat() method Example 3 String s2 = "Result: "+(5+1); System.out.println(s2); Output Result: 6

charAt() method Extracts a single character from a string F4105 JAVAPROGRAMMING charAt() method Extracts a single character from a string

charAt() method Example String s = "Malaysia" char ch = s.charAt(4); F4105 JAVAPROGRAMMING charAt() method Example String s = "Malaysia" char ch = s.charAt(4); System.out.println(ch); Output y

charAt() method Syntax F4105 JAVAPROGRAMMING charAt() method Syntax char variablename = stringobject.charAt(int where);

equals() method Compares two strings for equality. F4105 JAVAPROGRAMMING equals() method Compares two strings for equality.

equals() method Example String s1="Both are equal"; F4105 JAVAPROGRAMMING equals() method Example String s1="Both are equal"; String s2="Both are equal"; String s3="Both are not equal"; System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3)); Output true false

equals() method Syntax F4105 JAVAPROGRAMMING equals() method Syntax boolean variablename = stringobject.equals(stringobject);

F4105 JAVAPROGRAMMING indexOf() method Will search the first occurrence of a character or set of characters inside a string.

indexOf() method Example 1 int val1 = s.indexOf('s'); F4105 JAVAPROGRAMMING indexOf() method Example 1 int val1 = s.indexOf('s'); System.out.println(val1); Output 3

indexOf() method Example 2 int val2 = s.indexOf("is"); F4105 JAVAPROGRAMMING indexOf() method Example 2 int val2 = s.indexOf("is"); System.out.println(val2); Output 2

indexOf() method Syntax F4105 JAVAPROGRAMMING indexOf() method Syntax int variablename = stringobject.indexOf(character); int variablename = stringobject.indexOf(string);

F4105 JAVAPROGRAMMING lastIndexOf() method Will search the last occurrence of a character or a set of characters inside a string.

lastIndexOf() method Example 1 int val1 = s.lastIndexOf('s'); F4105 JAVAPROGRAMMING lastIndexOf() method Example 1 int val1 = s.lastIndexOf('s'); System.out.println(val1); Output 12

lastIndexOf() method Example 2 int val2 = s.lastIndexOf("is"); F4105 JAVAPROGRAMMING lastIndexOf() method Example 2 int val2 = s.lastIndexOf("is"); System.out.println(val1); Output 5

lastIndexOf() method Syntax F4105 JAVAPROGRAMMING lastIndexOf() method Syntax int variablename = stringobject.lastIndexOf(character); int variablename = stringobject.lastIndexOf(string);

F4105 JAVAPROGRAMMING replace() method Will replace all occurrences of a character in a string with another character.

replace() method Example String s="Hewwo"; F4105 JAVAPROGRAMMING replace() method Example String s="Hewwo"; String s1 = s.replace('w','l'); System.out.println(“Old string= “ + s); System.out.println(“New string= “ + s1); Output Hewwo Hello

replace() method Syntax F4105 JAVAPROGRAMMING replace() method Syntax String stringvariable = stringobject.replace(original,replacement);

F4105 JAVAPROGRAMMING trim() method Will trim the leading and trailing white spaces in a string.

trim() method Example String s=" Hello Malaysia "; F4105 JAVAPROGRAMMING trim() method Example String s=" Hello Malaysia "; String s1 = s.trim( ); System.out.println(s1); Output Hello Malaysia

trim() method Syntax String stringvariable = stringobject.trim( ); F4105 JAVAPROGRAMMING trim() method Syntax String stringvariable = stringobject.trim( );

F4105 JAVAPROGRAMMING toLowerCase() method Converts all the characters in a string from uppercase to lowercase.

toLowerCase() method Example String s1="LOWER"; F4105 JAVAPROGRAMMING toLowerCase() method Example String s1="LOWER"; String s2 = s1.toLowerCase( ); System.out.println(s2); Output lower

toLowerCase() method Syntax F4105 JAVAPROGRAMMING toLowerCase() method Syntax String stringvariable = stringobject.toLowerCase( );

F4105 JAVAPROGRAMMING toUpperCase() method Converts all the characters in a string from lowercase to uppercase.

toUpperCase() method Example String s1="upper"; F4105 JAVAPROGRAMMING toUpperCase() method Example String s1="upper"; String s2 = s1.toUpperCase( ); System.out.println(s2); Output UPPER

toUpperCase() method Syntax F4105 JAVAPROGRAMMING toUpperCase() method Syntax String stringvariable = stringobject.toUpperCase( );

substring() method Extracts a part of the string. F4105 JAVAPROGRAMMING substring() method Extracts a part of the string.

substring() method Example String s1="I do shopping in Mega Mall"; F4105 JAVAPROGRAMMING substring() method Example String s1="I do shopping in Mega Mall"; String s2 = s1.substring(5,12); System.out.println(s2); Akrasa ke (5) sehingga aksara ke(12-1) Output shoppin

StringBuffer Class Is a special class supported by Java to handle strings. StringBuffer creates strings of flexible length whereas String class creates strings of fixed length.

Creating StringBuffer Objects Example 1 StringBuffer sb1 = new StringBuffer(); Syntax StringBuffer StringBufferobject = new StringBuffer();

Creating StringBuffer Objects Example 2 StringBuffer s = new StringBuffer(6); Syntax StringBuffer StringBufferobject = new StringBuffer(integervariable);

Creating StringBuffer Objects Example 3 StringBuffer sb3 = new StringBuffer ("Java"); Syntax StringBuffer StringBufferobject = new StringBuffer(stringvariable);

length() method Finds the current length of a StringBuffer object.

length() method Example StringBuffer sb1 = new StringBuffer(“Java”); System.out.println (sb1.length()); Output 4

capacity() method Returns the amount of memory allocated for the StringBuffer object.

capacity() method Example Output 30 StringBuffer sb = new StringBuffer("Capacity of sb"); System.out.println (sb.capacity()); * “Capacity of sb” = 14 + 16(free storage) = 30 (amount of storage) Output 30

capacity() method Example Output 16 StringBuffer sb1 = new StringBuffer(""); System.out.println (sb1.capacity()); * “Capacity of sb” = 16(free storage) = 16 (amount of storage) Output 16

append() method Allows data to be added to the end of a StringBuffer object.

append() method Example StringBuffer sb1 = new StringBuffer("Welcome to beautiful"); sb1.append("_World"); System.out.println(sb1); Output Welcome to beautiful_World

setCharAt() method Sets the character at any position within a StringBuffer.

setCharAt() method Example StringBuffer sb1 = new StringBuffer("Hello World"); sb1.setCharAt(5,'_'); System.out.println(sb1); Output Hello_World

insert() method Allows data to be added at a specified position in the StringBuffer object.

insert() method Example StringBuffer sb1 = new StringBuffer("Hello World"); sb1.insert(6,"Java "); System.out.println(sb1); Output Hello Java World

reverse() method Example StringBuffer sb=new StringBuffer("roweT"); sb.reverse( ); System.out.println(sb); Output Tower

delete() method Example StringBuffer sb=new StringBuffer(“Program"); sb.delete(2, 5 ); //(2,(5-1)) System.out.print(sb); Output Pram

deleteCharAt() method Example StringBuffer sb=new StringBuffer(“Java"); sb.deleteCharAt(1 ); System.out.print(sb); Output Jva