



























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This lecture is part of lecture series delivered by Narayan Singh for Java Programming course at Cochin University of Science and Technology. It includes: Character, Extension, Default, Character-to-byte, Array, Conversions, Platform, Unicode, Internet, Protocols
Typology: Slides
1 / 35
This page cannot be seen from the preview
Don't miss anything!




























Using-
getChars()
141
adrish.b@ardentcollaboratio docsity.com
getBytes( )
There is an alternative to getChars( ) that stores the characters in an array of bytes. This method is called
getBytes( ), and it uses the default character-to-byte conversions provided by the platform. Here is its
simplest form:
byte[ ] getBytes( )
Other forms of getBytes( ) are also available. getBytes( ) is most useful when you are exporting a String
value into an environment that does not support 16-bit Unicode characters. For example, most Internet
protocols and text file formats use 8-bit ASCII for all text interchange.
toCharArray( )
If you want to convert all the characters in a String object into a character array, the easiest way is to call toCharArray( ). It returns an array of characters for the entire string. It has this general form: char[ ] toCharArray( )
This function is provided as a convenience, since it is possible to use getChars( ) to achieve the same result.
142
adrish.b@ardentcollaboratio docsity.com
To perform a comparison that ignores case differences, call
equalsIgnoreCase( ). When it compares two strings, it
considers A-Z to be the same as a-z. It has this general
form:
boolean equalsIgnoreCase(String str)
str is the String object being compared with the invoking String
object. It too, returns true if the strings contain the same
characters in the same order, and false otherwise.
144
adrish.b@ardentcollaboratio docsity.com
Sample program illustrating equals() and equalsIgnoreCase()
class equalsDemo { public static void main(String args[]) { String s1 = "Hello"; String s2 = "Hello"; String s3 = "Good-bye"; String s4 = "HELLO"; System.out.println(s1 + " equals " + s2 + " -> " + s1.equals(s2)); System.out.println(s1 + " equals " + s3 + " -> " + s1.equals(s3)); System.out.println(s1 + " equals " + s4 + " -> " + s1.equals(s4)); System.out.println(s1 + " equalsIgnoreCase " + s4 + " -> " + s1.equalsIgnoreCase(s4)); } }
145
adrish.b@ardentcollaboratio docsity.com
This method compares a specific region inside a string with another specific region in another string. There
is an overloaded form that allows you to ignore case in such comparisons. Here are the general forms for these two methods: boolean regionMatches(int s tartIndex, String str2, int str2StartIndex, int numChars) boolean regionMatches(boolean ignoreCase, int startIndex, String str2, int str2StartIndex, int numChars)
For both versions, startIndex specifies the index at which the region begins within the invoking String object. The String being compared is specified by str2. The index at which the comparison will start
within str2 is specified by str2StartIndex. The length of the substring being compared is passed in
numChars. In the second version, if ignoreCase is true, the case of the characters is ignored.
Otherwise, case is significant.
147
adrish.b@ardentcollaboratio docsity.com
startsWith( ) and endsWith( )
String defines two routines that are, more or less, specialized forms of regionMatches( ). The startsWith( ) method determines whether a given String begins with a specified string. Conversely, endsWith( ) determines whether the String in question ends with a specified string. They have the following general forms: boolean startsWith(String str) boolean endsWith(String str) Here, str is the String being tested. If the string matches, true is returned. Otherwise, false is returned. For example, "Foobar".endsWith("bar―) and "Foobar".startsWith("Foo―) are both true. A second form of startsWith( ), shown here, lets you specify a starting point: boolean startsWith(String s tr, int startIndex) Here, startIndex specifies the index into the invoking string at which point the search will begin. For example, "Foobar".startsWith("bar", 3) returns true.
adrish.b@ardentcollaboratio
148
docsity.com
equals( ) Versus ==
adrish.b@ardentcollaboratio
150
docsity.com
equals( ) Versus ==
adrish.b@ardentcollaboratio
151
Hello equals Hello -> true Hello == Hello -> false
docsity.com
indexOf()
& lastIndexOf()
adrish.b@ardentcollaboratio
153
docsity.com
indexOf()
& lastIndexOf()
System.out.println("lastIndexOf(the) = " +s.la stIndexOf("the")); System.out.println("indexOf(t, 10) = " +s.indexOf('t', 10)); System.out.println("lastIndexOf(t, 60) = " +s.lastIndexOf('t', 60)); System.out.println("indexOf(the, 10) = " +s.indexOf("the", 10)); System.out.println("lastIndexOf(the, 60) = " +s.lastIndexOf("the", 60)); } }
adrish.b@ardentcollaboratio
154
Now is the time for all good men to come to the aid of their country. indexOf(t) = 7 lastIndexOf(t) = 65 indexOf(the) = 7 lastIndexOf(the) = 55 indexOf(t, 10) = 11 lastIndexOf(t, 60) = 55 indexOf(the, 10) = 44 lastIndexOf(the, 60) = 55
docsity.com
adrish.b@ardentcollaboratio
156
docsity.com
Program using subString()
class StringReplace {
public static void main(String args[]) {
String org = "This is a test. This is, too.";
String search = "is";
String sub = "was";
String result = "";
int i;
do { // replace all matching substrings
System.out.println(org);
adrish.b@ardentcollaboratio
157
docsity.com
adrish.b@ardentcollaboratio
159
docsity.com
The replace( ) method replaces all occurrences of one character in the invoking string with another character. It has the following general form:
String replace(char original, char replacement)
Here, original specifies the character to be replaced by the
character specified by replacement. The resulting string is returned. For example,
String s = "Hello".replace('l', 'w');
puts the string ―Hewwo‖ into s.
adrish.b@ardentcollaboratio
160
docsity.com