String Handling Java implements strings as objects of
String Handling • Java implements strings as objects of type String. • String Constructors – To create an empty String, you call the default constructor. – For example, String s = new String(); – will create an instance of String with no characters in it. • String(chars[ ]) – chars[] = { 'a', 'b', 'c' }; – String s = new String(chars);
• String(chars[ ], int start. Index, int num. Chars) – chars[] = { 'a', 'b', 'c', 'd', 'e', 'f' }; – String s = new String(chars, 2, 3); • String(String str. Obj) • String(byte ascii. Chars[ ]) • String(byte ascii. Chars[ ], int start. Index, int num. Chars) • String(String. Buffer str. Buf. Obj)
String Length int length( ) chars[] = { 'a', 'b', 'c' }; String s = new String(chars); System. out. println(s. length());
Special String Operations • String Literals chars[] = { 'a', 'b', 'c' }; String s 1 = new String(chars); String s 2 = "abc"; // use string literal System. out. println("abc". length()); • String Concatenation String age = "9"; String s = "He is " + age + " years old. "; System. out. println(s); int age = 9; String s = "He is " + age + " years old. "; System. out. println(s); String s = "four: " + 2; System. out. println(s);
Character Extraction • char. At(int where) char ch; ch = "abc". char. At(1); • void get. Chars(int source. Start, int source. End, char target[ ], int target. Start) class get. Chars. Demo { public static void main(String args[]) { String s = "This is a demo of the get. Chars method. "; int start = 10; int end = 14; char buf[] = new char[end - start]; s. get. Chars(start, end, buf, 0); System. out. println(buf); } }
Character Extraction • get. Bytes( ) • to. Char. Array( ) – char[ ] to. Char. Array( )
String Comparison • boolean equals(Object str) • boolean equals. Ignore. Case(String str)
String Comparison • boolean region. Matches(int start. Index, String str 2, int str 2 Start. Index, int num. Chars) • boolean region. Matches(boolean ignore. Case, int start. Index, String str 2, int str 2 Start. Index, int num. Chars) • boolean starts. With(String str) • boolean ends. With(String str) "Football". ends. With("ball") and "Football". starts. With("Foot") are both true.
String Comparison • boolean starts. With(String str, int start. Index) – "Football". starts. With("ball", 4) • equals( ) Versus ==
String Comparison • int compare. To(String str)
// A bubble sort for Strings. class Sort. String { static String arr[] = {"Now", "is", "the", "time", "for", "all", "good", "men“, "to", "come", "to", "the", "aid", "of", "their", "country" }; public static void main(String args[]) { for(int j = 0; j < arr. length; j++) { for(int i = j + 1; i < arr. length; i++) { if(arr[i]. compare. To(arr[j]) < 0) { String t = arr[j]; arr[j] = arr[i]; arr[i] = t; } } System. out. println(arr[j]); } }
Searching Strings • index. Of( ) Searches for the first occurrence of a character or substring. • last. Index. Of( ) Searches for the last occurrence of a character or substring. • int index. Of(int ch) • int last. Index. Of(int ch)
Searching Strings • To search for the first or last occurrence of a substring, use – int index. Of(String str) – int last. Index. Of(String str) – Here, str specifies the substring. • You can specify a starting point for the search using these forms: • int index. Of(int ch, int start. Index) • int last. Index. Of(int ch, int start. Index) • int index. Of(String str, int start. Index) • int last. Index. Of(String str, int start. Index)
Searching Strings
Modifying a String • substring( ) – String substring(int start. Index, int end. Index)
Modifying a String • concat( ) – String concat(String str) – String s 1 = "one"; – String s 2 = s 1. concat("two"); • replace( ) – String replace(char original, char replacement) – String s = "Hello". replace('l', 'w'); • trim( ) – String s = " Hello World ". trim(); – This puts the string “Hello World” into s.
Data Conversion Using value. Of( ) • • static String value. Of(double num) static String value. Of(long num) static String value. Of(Object ob) static String value. Of(chars[ ])
Changing the Case of Characters Within a String • String to. Lower. Case( ) • String to. Upper. Case( ) // Demonstrate to. Upper. Case() and to. Lower. Case(). class Change. Case { public static void main(String args[]) { String s = "This is a test. "; System. out. println("Original: " + s); String upper = s. to. Upper. Case(); String lower = s. to. Lower. Case(); System. out. println("Uppercase: " + upper); System. out. println("Lowercase: " + lower); } }
String. Buffer • String. Buffer represents growable and writeable character sequences. • String. Buffer may have characters and substrings inserted in the middle or appended to the end. • String. Buffer( ) 16 chars • String. Buffer(int size) • String. Buffer(String str) • String. Buffer(Char. Sequence chars)
String. Buffer: length( ) and capacity( ) • int length( ) • int capacity( ) // String. Buffer length vs. capacity. class String. Buffer. Demo { public static void main(String args[]) { String. Buffer sb = new String. Buffer("Hello"); System. out. println("buffer = " + sb); System. out. println("length = " + sb. length()); System. out. println("capacity = " + sb. capacity()); } }
• ensure. Capacity( ): to set the size of the buffer. – void ensure. Capacity(int capacity) • set. Length( ): set the length of the buffer within a String. Buffer object. – void set. Length(int len) • char. At( ) and set. Char. At( ): – char. At(int where) – void set. Char. At(int where, char ch) • get. Chars( ) – void get. Chars(int source. Start, int source. End, char target[ ], int target. Start)
append( ) • String. Buffer append(String str) • String. Buffer append(int num) • String. Buffer append(Object obj) // Demonstrate append(). class append. Demo { public static void main(String args[]) { String s; int a = 42; String. Buffer sb = new String. Buffer(40); s = sb. append("a = "). append(a). append("!"). to. String(); System. out. println(s); } }
insert( ) • The insert( ) method inserts one string into another • String. Buffer insert(int index, String str) • String. Buffer insert(int index, char ch) • String. Buffer insert(int index, Object obj) // Demonstrate insert(). class insert. Demo { public static void main(String args[]) { String. Buffer sb = new String. Buffer("I Java!"); sb. insert(2, "like "); System. out. println(sb); } }
reverse( ) • String. Buffer reverse( ) // Using reverse() to reverse a String. Buffer. class Reverse. Demo { public static void main(String args[]) { String. Buffer s = new String. Buffer("abcdef"); System. out. println(s); s. reverse(); System. out. println(s); } }
delete( ) and delete. Char. At( ) • String. Buffer delete(int start. Index, int end. Index) • String. Buffer delete. Char. At(int loc) // Demonstrate delete() and delete. Char. At() class delete. Demo { public static void main(String args[]) { String. Buffer sb = new String. Buffer("This is a test. "); sb. delete(4, 7); System. out. println("After delete: " + sb); sb. delete. Char. At(0); System. out. println("After delete. Char. At: " + sb); } }
replace( ) • String. Buffer replace(int start. Index, int end. Index, String str) // Demonstrate replace() class replace. Demo { public static void main(String args[]) { String. Buffer sb = new String. Buffer("This is a test. "); sb. replace(5, 7, "was"); System. out. println("After replace: " + sb); } }
- Slides: 26