Programming Java Part5-Java Programming-Lecture Slides, Slides of Java Programming

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

2011/2012

Uploaded on 07/07/2012

proo
proo 🇮🇳

4.4

(26)

96 documents

1 / 35

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Character Extraction
Using-
getChars()
class getCharsDemo {
public static void main(String args[]) {
String s = "This is a demo of the getChars method.";
int start = 10;
int end = 14;
char buf[] = new char[end - start];
s.getChars(start, end, buf, 0);
System.out.println(buf);
}
}
Here is the output of this program:
demo
141
adrish.b@ardentcollaboratio
ns.com
http://www.ardentcollaborati
ons.com
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download Programming Java Part5-Java Programming-Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Character Extraction

Using-

getChars()

class getCharsDemo {

public static void main(String args[]) {

String s = "This is a demo of the getChars method.";

int start = 10;

int end = 14;

char buf[] = new char[end - start];

s.getChars(start, end, buf, 0);

System.out.println(buf);

 Here is the output of this program:

demo

141

adrish.b@ardentcollaboratio docsity.com

Character Extraction

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

String Comparison

equalsIgnoreCase( )

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

String Comparison

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

String Comparison

regionMatches( )

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 ==

equals( ) Versus ==

class EqualsNotEqualTo

public static void main(String args[])

String s1 = "Hello";

String s2 = new String(s1);

System.out.println(s1 + " equals " + s2 + " -> " +

s1.equals(s2));

System.out.println(s1 + " == " + s2 + " -> " + (s

== s2));

adrish.b@ardentcollaboratio

150

docsity.com

equals( ) Versus ==

equals( ) Versus ==

class EqualsNotEqualTo

public static void main(String args[])

String s1 = "Hello";

String s2 = new String(s1);

System.out.println(s1 + " equals " + s2 + " -> " +

s1.equals(s2));

System.out.println(s1 + " == " + s2 + " -> " + (s

== s2));

adrish.b@ardentcollaboratio

151

OUTPUT

Hello equals Hello -> true Hello == Hello -> false

docsity.com

Searching Strings

indexOf()

& lastIndexOf()

class indexOfDemo {

public static void main(String args[]) {

String s = "Now is the time for all good men " +"to come

to the aid of their country.";

System.out.println(s);

System.out.println("indexOf(t) = " +s.indexOf('t'));

System.out.println("lastIndexOf(t) = " +s.lastIndexOf('t'));

System.out.println("indexOf(the) = " +s.indexOf("the"));

adrish.b@ardentcollaboratio

153

docsity.com

Searching Strings

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

substring()

You can extract a substring using substring( ). It has

two forms.

String substring(int startIndex)

String substring(int startIndex, int endIndex)

adrish.b@ardentcollaboratio

156

docsity.com

substring()

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

concat( )

 You can concatenate two strings using concat( ), shown here:

String concat(String str)

This method creates a new object that contains the invoking string with the

contents of str appended to the end. concat( ) performs the same function as +.

For example,

String s1 = "one";

String s2 = s1.concat("two");

puts the string ―onetwo‖ into s2. It generates the same result as the following

sequence:

String s1 = "one";

String s2 = s1 + "two";

adrish.b@ardentcollaboratio

159

docsity.com

replace( )

 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