Lecture 6 (Strings in Java)
Lecture 6 (Strings in Java)
StringBuffer
4. String(String x) << Creates a String frorm another String >> 5. String(byte bytes[]) << Creates a String frorm byte Array>> 6. String(byte bytes[],int start, int numChars)
<< Creates a String frorm byte Array from start to start+numChars -1 >>
String Examples
1. String s1 = Object OR String s1 = new String(Object); 2. String s1 = new String(); << Empty String Created>> 3. char[ ] names ={ O,B, J,E, C,T, ,O,R, I,E, N,T, E,D}; String s1 = new String(names); << s1 will be OBJECT ORIENTED>> String s2 = new String(names,2,4); << s2 will be JECT>> String s3 = new String(names,6,10); << StringIndexOutOfBoundsException>> String s4 = new String(names,6,-4); 4. byte[ ] values = { 10,45,67,34,68,66 }; << s1 will be -C"DB>> String s1 = new String(values); String s2 = new String(values, 2,3); << s2 will be C"D>> String s3 = new String(values,2,6); << StringIndexOutOfBoundsException>> 5. String s1 = Object; String s2 = new String(s1); << Creating String from another String>>
String Examples cont.. 6. String s1 =OOP; String s2 =OOP; String s3 =OOP; String s1 = new String(OOP);
String: s1 s2 s3 OOP
String Length
int length() << Returns the length of String>> Usage : <string>.length(); Examples : S.O.P(Object.length()); 6 s1.length(); name.length();
String Concatenation
Adding Strings together <<Concatenation>> + operator can be used to concatenate two strings
// s1 will be HelloHowareYou2020
3. System.out.println(xyz.concat(oop));
4. String s1 = 20+20+Hello+How are You; // s1 will be 40HelloHowareYou
Start index in End index in char Array where Invoking String Invoking String characters from string are to be Start index in char stored Array from where the characters are 1. characters will be extracted from sourceStart to be stored to sourceEnd-1 2. Extracted Characters will be stored in target[] char array from index targetStart 3. Every index either in invoking or target string should be within specified limits
class StringExamples { public static void main(String args[]) { String s1="object oriented";
char[] name = new char[10];
getChars() Example 1
s1.getChars(2,6,name,0);
for(int i=0;i<name.length;i++) System.out.print(name[i]+" "); System.out.println(" "); char[] name1 = new char[10];
s1.getChars(5,8,name1,3);
for(int i=0;i<name.length;i++) System.out.print(name1[i]+" "); } }
getChars() Example 2
class StringExamples { public static void main(String args[]) { String s1="object oriented";
char[] name = new char[10];
s1.getChars(6,2,name,0);
for(int i=0;i<name.length;i++) System.out.print(name[i]+" "); System.out.println(" ");
char[] name1 = new char[10];
s1.getChars(8,5,name1,3);
for(int i=0;i<name.length;i++) System.out.print(name1[i]+" "); } }
D:\java\bin>java StringExamples Exception in thread "main" java.lang.StringIndexOutOfBoundsExce ption: String in ex out of range: -4 at java.lang.String.getChars(String.java:72 4) at StringExamples.main(StringExamples.ja va:8)
getChars() Example 3
class StringExamples { public static void main(String args[]) { String s1="object oriented";
char[] name = new char[10];
s1.getChars(0,13,name,0);
for(int i=0;i<name.length;i++) System.out.print(name[i]+" "); System.out.println(" ");
char[] name1 = new char[10];
s1.getChars(0,5,name1,3);
for(int i=0;i<name.length;i++) System.out.print(name1[i]+" "); } }
D:\java\bin>java StringExamples Exception in thread "main" java.lang.ArrayIndexOutOfBoundsE xception at java.lang.System.arraycopy(Native Method) at java.lang.String.getChars(String.jav a:726) at StringExamples.main(StringExampl es.java:8)
byte[] getBytes[]
String Comparisons
boolean equals(Object str) boolean equalsIgnoreCase(String str) Examples :
(i) xyz.equals(abc); << false>> (ii) xyz.equalsIgnoreCase(XYZ) << true> (iii) s1.equals(s2) << returns if s1 and s2 are equal>> (iv) s1.equalsIgnoreCase(s2) << returns if s1 and s2 are equal by ignoring case>>
equals Vs ==
String s1 = new String(OOP); String s2 = new String(OOP); if (s1 == s2) S.O.P(Hello); else S.O.P(Hi); How Many Objects are created? TWO What will be output? Hi
= = checks whether two string references are pointing to same string object or not. if (s1.equals(s2)) What will be output? S.O.P(Hello); Hello else S.O.P(Hi); equals( ) checks whether contents of two strings are pointing two same object or not.
equlas Vs == cont..
String s1 = new String(OOP); String s2 = s1 if (s1 == s2) S.O.P(Hello); else S.O.P(Hi); if (s1.equals(s2)) S.O.P(Hello); else S.O.P(Hi); How Many Objects are created? ONE What will be output? Hello
Used for String comparisons. Returns one of the three possible values: <0 invoking string is less than str >0 invoking string is greater than str =0 if both strings are equal Used for ordering/sorting strings
String s1 = "OOP"; String s2 = "OOP"; String s3 = "java"; if( s1 == s2) System.out.println("Hello"); else System.out.println("Hi");
System.out.println(s1.compareTo(s3)); System.out.println(s3.compareTo(s1));
Hello -27 27
Searching Strings
indexOf() lastIndexOf()
Used searching first/last occurences of a character / substring Return the index of character or substring if found otherwise -1 These two methods are overloaded in several different ways 1. int indexOf(int ch) / int lastIndexOf(int ch)
Example
String s1 = "Now is the time for all good men to come forward to aid their country";
7 66 33 49 49 33
Substring Example
String s1 = "Now is the time for all good men to come forward to aid their country";
System.out.println(s1.substring(20));
Index 20 to end
Index 20 to 39
oriented
System.out.println("object oriented".substring(5,10));
Index 6 to end
t ori
Index 5 to 9
class Course { private String courseNo; private int compCode; private String courseName; Course(String courseString) { // courseString has first 10 places for courseNo // Next 4 places for comp code // Next 30 places for courseName
courseNo = courseString.substring(0,10); compcode = Integer.parseInt(courseString.substring(10,14)); courseName = courseString.substring(14); } }
1. 2.
3.
String trim() Removes any leading and trailing whitespaces Hello World .trim(); << returns Hello World>> Useful for processing user commands
Changing Case of Characters Within a String 1. 2. String toLowerCase() String toUpperCase() Examples : S.O.P(object.toUpperCase()); S.O.P(OBJECT.toLowerCase());
String toString()
Converts any object reference to String form This method is by default supplied by Object class. toString() method in Object class returns the hascode value of Object in String form Any class can override this method with following syntax: public String toString() { .. return <anyString> }
This class A does not supply any toString() method. So it will be called from Object class
class test100 { public static void main(String args[]) { A a1 = new A(10,8); System.out.println(a1); } } HashCode of Object OUTPUT A@10b62c9 Name of class
class A { int a,b; A(int a,int b) { this.a = a; this.b = b; } public String toString() { return "a="+a+"b="+b; } }
This class A supplies a toString() method. So it will be called from class A itself class test100 { public static void main(String args[]) { A a1 = new A(10,8); System.out.println(a1); } }
a=10b=8
Change this statement to return Hello Java; Recompile and ReExcecute the Program
Home Exercise
1. 2. 3. void sort(String[] arr, boolean isAscending) void sort(String[] arr, int fromIndex, int toIndex, boolean isAscending) boolean isRefelexiveMirror(String other) RAM and MAR are reflexive mirrors XYZ and ZYX are reflexive mirrors object and tebojc are reflexive mirrors static boolean isRefelexiveMirror(String first, String second) boolean contains(String other) << whether a string contains other or not. static boolean contains(String f1, String f2)