Lecture Notes K.Yellaswamy Assistant Professor K L University

Similar documents
Building Strings and Exploring String Class:

Lab 14 & 15: String Handling

2. All the strings gets collected in a special memory are for Strings called " String constant pool".

Creating Strings. String Length

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Mathematics for Computer Graphics - Lecture 12

09 Objects III: String and StringBuffer, Singly (Doubly) Linked Lists

Java String Java String provides a lot of concepts that can be performed on a string such as compare, concat, equals, split, length, replace,

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters

"Hello" " This " + "is String " + "concatenation"

Object-Oriented Programming

Strings, StringBuffer, StringBuilder

String. Other languages that implement strings as character arrays

JAVA NUMBERS, CHARS AND STRINGS

CS112 Lecture: Characters and Strings

CHAPTER 6 MOST COMMONLY USED LIBRARIES

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

OOP-Lecture Java Loop Controls: 1 Lecturer: Hawraa Sh. You can use one of the following three loops: while Loop do...while Loop for Loop

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination

J.43 The length field of an array object makes the length of the array available. J.44 ARRAYS

More non-primitive types Lesson 06

Chapter 10: Text Processing and More about Wrapper Classes

Oracle FLEXCUBE Direct Banking

SOFTWARE DEVELOPMENT 1. Strings and Enumerations 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

- Thus there is a String class (a large class)

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

appreciate the difference between a char and a string understand and use the String class methods

String related classes

Chapter 8 Strings. Chapter 8 Strings

Class Library java.lang Package. Bok, Jong Soon

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) - All Sections Final Examination

Eng. Mohammed Abdualal


String is one of mostly used Object in Java. And this is the reason why String has unique handling in Java(String Pool). The String class represents

TEXT-BASED APPLICATIONS

Lesson:9 Working with Array and String

We now start exploring some key elements of the Java programming language and ways of performing I/O

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

STUDENT LESSON A10 The String Class

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Java Strings Java, winter semester

More on variables and methods

Duhok Polytechnic University Amedi Technical Institute/ IT Dept. Halkawt Rajab Hussain

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2009) - All Sections Final Examination

A variable is a name for a location in memory A variable must be declared

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Chapter 12 Strings and Characters. Dr. Hikmat Jaber

TechSparxJavaTuitionsTechSparxJava

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

ECS-503 Object Oriented Techniques

TEST (MODULE:- 1 and 2)

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 1, Name: KEY A

Programming with Java

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 2, Name:

Chettinad College of Engineering & Technology Department of Information Technology Internal Examination II (Answer Key)

IS502052: Enterprise Systems Development Concepts Lab 1: Java Review

Computer Science 252 Problem Solving with Java The College of Saint Rose Spring Topic Notes: Strings

COMPUTER APPLICATIONS (86)

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 11, Name: KEY

Intro to Computer Science II

Prepared By: Shiba R. Tamrakar

CSE1710. Big Picture. Today is last day covering Ch 6. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture.

CS 1301 Ch 8, Part A

Bellwork Strings 11/25/13

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 6, Name:

CS1150 Principles of Computer Science Math Functions, Characters and Strings (Part II)

COMP-202 Unit 5: Basics of Using Objects

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2010) - All Sections Final Examination

Class. Chapter 6: Data Abstraction. Example. Class

Index COPYRIGHTED MATERIAL

Chapter 29: String and Object References Bradley Kjell (Revised 06/15/2008)

Java Programming. String Processing. 1 Copyright 2013, Oracle and/or its affiliates. All rights reserved.

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Ohad Barzilay and Oranit Dror

Java s String Class. in simplest form, just quoted text. used as parameters to. "This is a string" "So is this" "hi"

Netbeans tutorial:

STRINGS AND STRINGBUILDERS. Spring 2019

CLASS X. THEORY 100 Marks

Characters, Strings and Text Processing in Java

CSC 222: Object-Oriented Programming. Spring 2017

Selected Questions from by Nageshwara Rao

CISC 370: Introduction to Java

Midterm Exam 2 Thursday, November 15th, points (15% of final grade) Instructors: Jim Williams and Marc Renault

Using Java Classes Fall 2018 Margaret Reid-Miller

Midterms Save the Dates!

C08c: A Few Classes in the Java Library (II)

SSE3052: Embedded Systems Practice

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

COE318 Lecture Notes Week 4 (Sept 26, 2011)

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

AP Computer Science. Strings. Credit: Slides are modified with permission from Barry Wittman at Elizabethtown College

Java characters Lecture 8

Utilities (Part 2) Implementing static features

CSC 222: Object-Oriented Programming. Spring 2013

Java By Abstraction - Test-B (Chapters 1-6)

Transcription:

Lecture Notes K.Yellaswamy Assistant Professor K L University Building Strings and Exploring String Class: -------------------------------------------- The String class ------------------- String: A String is a sequence of characters. ex:- name,address,creditcard no,etc.., In java,any string is object of String class. ->String is not a character array. The String class has 11 constructors and more than 40 methods. create String object:- -------------------- 1.We can declare a String type variable and initialize it directly with a group of characters. ex:- String st = "yellaswamy"; 2.We can create a String object using new operator and pass a group of characters to the object. ex:- String s1 = new String("Ashok"); 3.We can create a character array into a string by passing it to the String object. ex:- char arr[] = 'K','s','w','a','m','y'; String s2 = new String(arr); ex:- String s3 = new String(arr,1,4); o/p: swamy Ex1: class Test public static void main(string... args) //create a String object or constructing a String //1.We can declare a String type variable and initialize it directly with a group of characters. String str1="yellaswamy"; System.out.println(str1); //2.We can create a String object using new operator and pass a group of characters to the object.

String str2=new String("Ashok"); System.out.println(str1); //3.We can create a character array into a string by passing it to the String object. char arr[] = 'K','y','e','l','l','a','s','w','a','m','y'; String s1 = new String(arr); System.out.println(s1); String s2 = new String(arr,1,10); System.out.println(s2); D:\Yellaswamy_ClassNotes\Strings>javac Test.java D:\Yellaswamy_ClassNotes\Strings>java Test Yellaswamy Yellaswamy Kyellaswamy yellaswamy D:\Yellaswamy_ClassNotes\Strings> -------------------------------------------- Types of Obejcts:- There are 2 types. 1.Mutable 2.Immutable 1.Mutable:- A mutable object is an object where content can be modified. 2.Immutable:- An immutable object is an object whose content can not be modified. =>String objects are immutable.its contents cannot be changed. String class methods:- -------------------- String class belongs to java.lang package. 1.String concat(string str):- concatenates the calling string with str.

note:- '+' will also do the same. ex:- String s1="hydera"; String s2 = "bad"; String s3 = s1.concat(s2); o/p: hyderabad 2.int length():- Returns the length of the string. String s1 = "Vijayawada"; int n = s1.length(); 3.char charat(int i):- It extracts only one character from given String.That character at the same ith place. 4.int compareto(string str):- (case sensitive) (or) int comparetoignorecase(string str):- (case insensitive) They are used to compare two strings. 5.boolean equals(string str):- (case sensitive) (or) boolean equalsignorecase(string str):- (case insensitive) It returns true if the calling string equals to str. 6.boolean startswith(string prefix):- It returns true if the calling string starts with prefix(begins) prefix - sub string (or) not. 7.boolean endswith(string suffix):- It returns true if the invoking string ends with suffix. Note:- The above two methods use case sensitive comparison. 8.int indexof(string str):- It returns the position number of substring in the main String.So we have to pass substring. It returns the first occurance of str in the string. EX:- "This is a book" int n = str.indexof("is"); o/p :- n = 2 9.int lastindexof(string str):- It returns last Occurance in the string. 10.String replace(char oldchar,char newchar):- It returns a new String that is obtained by replacing all characters of 'oldchar' in string with 'newchar'. 11.String substring(int beginindex):- It ruturns a new String consisting of all characters from beginindex until the end of the String. 12.String substring(int beginindex,int endindex):- It returns a new String consisting all characters from beginindex until endindex(exclusive).

13.String tolowercase():- It converts all characters into lowercase and returns. 14.String touppercase():- IT converts all characters into uppercase and returns. 15.String trim():- It eleminates all leading and trailing spaces. ------------------------------------------------------ StringBuffer Objects are mutable. Creating StringBuffer objects:- ----------------------------- 1.StringBuffer sb = new StringBuffer("hello"); 2.StringBuffer sb = new StringBuffer(50); 3.StringBuffer sb = new StringBuffer(); java.lang.stringbuffer methods:- ------------------------------- 1)StringBuffer append(x):- x may be int,float,double,char,string (or) StringBuffer.It StringBuffer. 2)StringBuffer insert(int offset,x):- x may be int,float,double,char,string (or) StringBuffer.It at offset. 3)StringBuffer delete(int start,int end):- Removes the characters from start to end position. 4)StringBuffer reverse():- It reverses the all characters in the StringBuffer. 5)String tostring():- Converts the StringBuffer into the String. purpose:- converts StringBuffer to string class. 6)int length():- returns the length of the StringBuffer. 7)int indexof(string str):- It returns the first position of substring 'str' in the 8)int lastindexof(string str):- It returns the last Occurance of substring 'str' in the will be appended to the calling will be inserted into the StringBuffer StringBuffer object. StringBuffer object. StringBuilder:-

-------------- This class has been added in jdk1.5.0 which has same features like StringBuffer class.these objects are also mutable as are the StringBuffer objects. IIQ)What is the difference between the StringBuffer and StringBuilder classes? r) StringBuffer class is synchronized and StringBuilder is not.when the programmer wants to use several threads,he should use StringBuffer as it gives reliable results.if only one thread is used,stringbuilder is prefered,as it improves execution time. H.W: 1)sort a given group of strings into alphabetical order. 2)find the position of substring in a given main string. 3)Test whether a given string is palindrome or not. Ex2: D:\Yellaswamy_ClassNotes>javap java.lang.string Compiled from "String.java" public final class java.lang.string extends java.lang.object implements java.io. Serializable,java.lang.Comparable,java.lang.CharSequence public static final java.util.comparator CASE_INSENSITIVE_ORDER; public java.lang.string(); public java.lang.string(java.lang.string); public java.lang.string(char[]); public java.lang.string(char[], int, int); public java.lang.string(int[], int, int); public java.lang.string(byte[], int, int, int); public java.lang.string(byte[], int); public java.lang.string(byte[], int, int, java.lang.string) throws jav a.io.unsupportedencodingexception; public java.lang.string(byte[], int, int, java.nio.charset.charset); public java.lang.string(byte[], java.lang.string) throws java.io.unsup portedencodingexception; public java.lang.string(byte[], java.nio.charset.charset); public java.lang.string(byte[], int, int); public java.lang.string(byte[]); public java.lang.string(java.lang.stringbuffer);

public java.lang.string(java.lang.stringbuilder); java.lang.string(int, int, char[]); public int length(); public boolean isempty(); public char charat(int); public int codepointat(int); public int codepointbefore(int); public int codepointcount(int, int); public int offsetbycodepoints(int, int); void getchars(char[], int); public void getchars(int, int, char[], int); public void getbytes(int, int, byte[], int); public byte[] getbytes(java.lang.string) throws java.io.unsupportedenc odingexception; public byte[] getbytes(java.nio.charset.charset); public byte[] getbytes(); public boolean equals(java.lang.object); public boolean contentequals(java.lang.stringbuffer); public boolean contentequals(java.lang.charsequence); public boolean equalsignorecase(java.lang.string); public int compareto(java.lang.string); public int comparetoignorecase(java.lang.string); public boolean regionmatches(int, java.lang.string, int, int); public boolean regionmatches(boolean, int, java.lang.string, int, int); public boolean startswith(java.lang.string, int); public boolean startswith(java.lang.string); public boolean endswith(java.lang.string); public int hashcode(); public int indexof(int); public int indexof(int, int); public int lastindexof(int); public int lastindexof(int, int); public int indexof(java.lang.string); public int indexof(java.lang.string, int); static int indexof(char[], int, int, char[], int, int, int); public int lastindexof(java.lang.string); public int lastindexof(java.lang.string, int); static int lastindexof(char[], int, int, char[], int, int, int); public java.lang.string substring(int); public java.lang.string substring(int, int); public java.lang.charsequence subsequence(int, int); public java.lang.string concat(java.lang.string);

public java.lang.string replace(char, char); public boolean matches(java.lang.string); public boolean contains(java.lang.charsequence); public java.lang.string replacefirst(java.lang.string, java.lang.string); public java.lang.string replaceall(java.lang.string, java.lang.string); public java.lang.string replace(java.lang.charsequence, java.lang.charsequen ce); public java.lang.string[] split(java.lang.string, int); public java.lang.string[] split(java.lang.string); public java.lang.string tolowercase(java.util.locale); public java.lang.string tolowercase(); public java.lang.string touppercase(java.util.locale); public java.lang.string touppercase(); public java.lang.string trim(); public java.lang.string tostring(); public char[] tochararray(); public static java.lang.string format(java.lang.string, java.lang.object[]); public static java.lang.string format(java.util.locale, java.lang.string, ja va.lang.object[]); public static java.lang.string valueof(java.lang.object); public static java.lang.string valueof(char[]); public static java.lang.string valueof(char[], int, int); public static java.lang.string copyvalueof(char[], int, int); public static java.lang.string copyvalueof(char[]); public static java.lang.string valueof(boolean); public static java.lang.string valueof(char); public static java.lang.string valueof(int); public static java.lang.string valueof(long); public static java.lang.string valueof(float); public static java.lang.string valueof(double); public native java.lang.string intern(); public int compareto(java.lang.object); static ; ----------------------------------------- Programs: 1. public class str1

String st1=new String(); st1="java"; byte b[]=65,66,67,68,69,70; String st2=new String(b); String st3=new String(b,1,3); System.out.println(st1); System.out.println(st2); System.out.println(st3); D:\Yellaswamy_ClassNotes\Strings>java str1 Java ABCDEF BCD ----------------- Ex2: public class str2 char ch[]='a','b','c','d','e','f'; String st1=new String("Java"); StringBuffer sb=new StringBuffer("cmrcet"); String st2=new String(ch); String st3=new String(ch,2,4); String st4=new String(sb); System.out.println(st1); System.out.println(st2); System.out.println(st3); System.out.println(st4); D:\Yellaswamy_ClassNotes\Strings>java str2

Java abcdef cdef cmrcet ------------------------------ Ex3: public class str3 String s=new String("welcome"); int i; char ch; i=s.length(); ch=s.charat(5); System.out.println("Length of given string : "+i); System.out.println("The character at given index : " +ch); D:\Yellaswamy_ClassNotes\Strings>java str3 Length of given string : 7 The character at given index : m ------------------------------------------------------- Ex4: public class str4 String s1=new String("java"); String s2=new String("JAVA"); String s3=new String("welcome"); int n; n=s1.compareto(s3); if(n==0)

System.out.println("s1 and s3 are equal"); else if(n>0) System.out.println("s1 is greater than s3"); else System.out.println("s1 is less than s3"); n=s1.compareto(s2); if(n==0) System.out.println("s1 and s2 are equal"); else if(n>0) System.out.println("s1 is greater than s2"); else System.out.println("s1 is less than s2"); n=s1.comparetoignorecase(s2); if(n==0) System.out.println("s1 and s2 are equal"); else if(n>0) System.out.println("s1 is greater than s2"); else System.out.println("s1 is less than s2"); D:\Yellaswamy_ClassNotes\Strings>java str4 s1 is less than s3 s1 is greater than s2 s1 and s2 are equal ------------------------------------ Ex:5 class str5 boolean b; StringBuffer sb=new StringBuffer("welcome"); String s1=new String("welcome"); String s2=new String(" to cmrcet"); String s3=new String();

s3=s1.concat(s2); System.out.println("Concatenated String : "+s3); b=s1.contentequals(sb); if(b==true) System.out.println("Given 2 strings are equal"); else System.out.println("Given 2 strings are not equal"); D:\Yellaswamy_ClassNotes\Strings>java str5 Concatenated String : welcome to cmrcet Given 2 strings are equal ------------------------------------ Ex:6 class str6 char ch[]='a','b','c','d','e','f'; String s1=string.copyvalueof(ch); String s2=string.copyvalueof(ch,1,4); System.out.println(s1); System.out.println(s2); D:\Yellaswamy_ClassNotes\Strings>java str6 abcdef bcde ----------------------------------------- Ex:7 public class str7 String s1=new String("WELCOME");

String s2=new String("welcome"); boolean b; b=s1.endswith("come"); if(b==true) System.out.println("true"); else System.out.println("false"); b=s1.equals(s2); if(b==true) System.out.println("Given 2 strings are equal"); else System.out.println("Given 2 strings are not equal"); b=s1.equalsignorecase(s2); if(b==true) System.out.println("Given 2 strings are equal"); else System.out.println("Given 2 strings are not equal"); D:\Yellaswamy_ClassNotes\Strings>java str7 false Given 2 strings are not equal Given 2 strings are equal -------------------------------------------------- Ex:8 public class str8 int i; String st=new String("WELCOME"); byte bt[]=new byte[10]; char ch[]=new char[10]; bt=st.getbytes(); System.out.println("Byte Array");

for(i=0;i<bt.length;i++) System.out.println(bt[i]+" "); st.getchars(3,7,ch,2); System.out.println("Character Array"); for(i=0;i<ch.length;i++) System.out.println(ch[i]+" "); D:\Yellaswamy_ClassNotes\Strings>java str8 Byte Array 87 69 76 67 79 77 69 Character Array C O M E --------------------------------------- Ex:9 public class str9 String s1=new String("welcome"); String s2=new String("abcdabcdabc"); int id,hc; hc=s1.hashcode();

System.out.println("Hash Code of given String : "+hc); id=s1.indexof('e'); System.out.println("Index : " +id); id=s1.indexof('e',3); System.out.println("Index : " +id); id=s1.indexof("come"); System.out.println("Index : " +id); id=s2.indexof("ab",5); System.out.println("Index : " +id); D:\Yellaswamy_ClassNotes\Strings>java str9 Hash Code of given String : 1233099618 Index : 1 Index : 6 Index : 3 Index : 8 ------------------------------------------------