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.

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.
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.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
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.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
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.
28-Jun-15 String and StringBuilder Part I: String.
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.
Strings.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 6, page 1 Sun Certified Java 1.4 Programmer Chapter 6 Notes Gary Lance
From C++ to Java A whirlwind tour of Java for C++ programmers.
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.
Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;
Chapter 7: Characters, Strings, and the StringBuilder.
CHAPTER 9 Text Processing and More about Wrapper Classes Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
10-2 Chapter 10 discusses the following main topics:  Introduction to Wrapper Classes  Character Testing and Conversion with the Character Class  More.
String Handling StringBuffer class character class StringTokenizer class.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
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.
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.
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.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
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.
17-Feb-16 String and StringBuilder Part I: String.
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".
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
The String class is defined in the java.lang package
String and StringBuffer classes
Strings, StringBuilder, and Character
EKT 472: Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
Programming in Java Text Books :
String and String Buffers
String Handling in JAVA
Modern Programming Tools And Techniques-I Lecture 11: String Handling
String and StringBuilder
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
String and StringBuilder
String and StringBuilder
Java – String Handling.
String and StringBuilder
String methods 26-Apr-19.
Strings in Java.
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

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 of String class represents Immutable sequence of characters. By immutable we menace that once a String object is created, it can’t be modifying.whenever an operation is performed on a string object that results in some changes a new string object is constructed.

Constructor of string:- Public String(); Public string(char[]); Public String(char[], int offset, int no of characters); Public String (byte[]); Public string(byte[],int offset, int no of bytes); Public String (String str); Ex: char a[]={‘P’,’Q’,’R’,’S’,’T’}; Byte b[]={65,66,67,68,}; String s1=new String(a);

String s2=new String(b); String s3=new String(a,1,3); String s4=new String(b,2,2); String s5=new String(s1); >print all string object s1,s2,s3,s4,s5. Note: System.out.println(s1==s2); False Note: system.out.println(s1==s5); False Description:- equals to operators when use with object comparers value of their reference variable not the contents of the objects.

Equals method is used to content wise compare to objects of a class. This method is define in object class. Note: default implementation of equals methods compares the value of reference var. This method must be overridden in a class to facilitate content wise comparison. Note: String class Overrides equals methods. Public boolean equals(Object o); System.out.println(s1.equals(s2)); False System.out.println(s1.equals(s5)); True

Advantages of immutable String Advantage of strings being immutable is that they can be shared. In java String literals and compilation time literals expressions having same character sequence are represented by single string object such a string object that has multiple reference in different scope is called interned String. Compiler create a separate tools of such strings.

program Class Other { Public static string a=“ABCD”; } Class Test { Static String b=“ABCD”; Public static void main(String arr[]) { String c=“ABCD”; String d=“AB”; String e=d+”CD”;//String e=“AB”+”CD”; String f=d+new String(“CD”);//create at runtime (In Heap) String g=new String(c);// create at runtime (In Heap) System.out.println(Other.a==b); True System.out.println(b==c); True System.out.println(c==e); True System.out.println(e==f); False System.out.println((Other.a.equals(b))); True System.out.println(Other.a==g); False }

Pool of Interned String ABCD 100 AB 200 a d e b c CD ABCD g 400 f

Method of string class length():- returns the number of character in a strings. Public int length(); charAt():-returns the character at the specified index from the string ; Public char charAt(int index); getChars():-used to extracts characters from a string. Public getChar (char[],int index,int no characters); Ex:String s=LovingIndia System.out.println(charAt(6)); Char a[]= new char[5]; S.getChars(a,6,5); System.out.println(new String (a)); O/p I O/p India

toCharArray():- returns contents of the invoking string in the form of a character arrays. Public char[] toCharArray(); Char a[]=s.toCharArray(); compare To():- is used to find out the Sorting order of two string, content wise compare of two String and returns zero if both string are same,positive integer if invoking String comes after argument String in sorted order other wise represents negative integer. Public int compareTo(String s); E.g. String s1=“CA”; String s2=“DOG”; S.o.p(s1.compareTo(s2)); C-D=-1; S.o.p(s2.compareTo(s1)); D-C=1;

substring():- is used to obtain a part of string. 1)Public subString(int start index); 2) public subString (int start index,int end index); Ex: String s1=“ABCDEF”; S.o.p(s1.subString(2));//CDEF S.O.P(s1.subString(2,3));//C index Of():- returns the index of first operands of a character or String within String. public int indexOf(char ch); Public int indexOf(String s); Public int lastIndexOf(Char ch); Public int lastindexOf(String s); Ex: String s=“ababcababc”; s.O.P(s.indexOf(b));// o/p 1 S.O.P(s.indexOf(“abca”)); o/p 2 s.o.p(s.lastIndexOf(‘c’)); o/p 9 s.o.p(s.lastindexOf(“cab”)); o/p 4

toUpperCase():-convert lower case to Upper case. Public string toupperCase(); toLowerCase():- convert to lowercase of upper case string. Public string toLowerCase(); Ex: string s=“abcd”; //s.toLowerCase(); S.o.p(s); o/p abcd; // String t=s.toUpperCase(); s.o.p(t); // o/p ABCD;

valueOf():- is used to convert a primitive type into a string. Public static String valueOf(char ch); Public static String valueOf(byte b); Public static String valueOf(short s); Public static String valueOf(int i); Public static String valueOf(long l); Public static String valueOf(float f); Public static String valueOf(double d); a program for depiction

Class A { Public static void display(String a) { S.o.p(a); } Class Test { Public static void main(string arr[]) { Int a=45; Char c=‘A’; Double d=45.67; A.display(a);//A.display(String.valueOf(a)); A.display(c);//A.display(String.valueOf(c)); A.display(d);//A.display(String.valueOf(d)); } O/P type mismatch Error or can not be applied to int,cahr,double.

Alternative method to convert primitive type into a String object. All the wrapper classes have static toString method is defined convert the primitive type into a string. Public static String toString(type value); Actual methods: Public static String toString(char ch); Public static String toString(int i); Public static String toString(float f);