22
10/21/10 1

10/21/101. 2 What are Strings?... A combination of characters is a string. Strings are instances of the class ‘String’. When a string is used in programs,

Embed Size (px)

Citation preview

Page 1: 10/21/101. 2 What are Strings?... A combination of characters is a string. Strings are instances of the class ‘String’. When a string is used in programs,

10/21/10 1

Page 2: 10/21/101. 2 What are Strings?... A combination of characters is a string. Strings are instances of the class ‘String’. When a string is used in programs,

10/21/10 2

What are Strings? . . .

• A combination of characters is a string.

• Strings are instances of the class ‘String’.

• When a string is used in programs, JAVA automatically creates instances of the String class.

Page 3: 10/21/101. 2 What are Strings?... A combination of characters is a string. Strings are instances of the class ‘String’. When a string is used in programs,

10/21/10 3

Declaring string variables . . .

String variable_name;

When the values are assigned to string variables, the value should be given within the double quotations (“ “).

Ex:String name=“Kamal”;String empNo=“EMP102”;String address=“No:5,Pushpadana Mawatha, Kandy”;

Page 4: 10/21/101. 2 What are Strings?... A combination of characters is a string. Strings are instances of the class ‘String’. When a string is used in programs,

02/10/10 4

Example 1. . .

class String_Example{

public static void main(String arg[]) {

;”String f_name=“Malan;”String m_initial=“C;”String l_name=“Karunarathne

;System.out.println(f_name+” “+m_initial+” “+l_name) {

{

class String_Example{

public static void main(String arg[]) {

String f_name=“Malan”;String m_initial=“C”;String l_name=“Karunarathne”;

System.out.println(f_name+” “+m_initial+” “+l_name); {

{

Write a simple JAVA program that prints the full name, when the first name, middle initial and the last name is given.

Page 5: 10/21/101. 2 What are Strings?... A combination of characters is a string. Strings are instances of the class ‘String’. When a string is used in programs,

10/21/10 5

String constructors . . .

• String class contains a default constructor with no parameters .

• It creates an empty string.

String s=new String();

Page 6: 10/21/101. 2 What are Strings?... A combination of characters is a string. Strings are instances of the class ‘String’. When a string is used in programs,

10/21/10 6

String constructors (cont) . . .

• To create a String initialized with characters, we can call another constructor of String class that accepts a char array.

char arr[]={‘a’ , ’b’ , ’c’, ’d’ , ’e’ };

String s=new String(arr);

Page 7: 10/21/101. 2 What are Strings?... A combination of characters is a string. Strings are instances of the class ‘String’. When a string is used in programs,

10/21/10 7

String constructors (cont) . . .

• There is another constructor in String class, that allows to specify the starting index and the number of characters to be included as the new string value.

char arr[]={‘a’ , ’b’ , ’c’, ’d’ , ’e’ };

String s=new String(arr,2,3);

This indicates that s contains 3 characters starting from index 2.

Page 8: 10/21/101. 2 What are Strings?... A combination of characters is a string. Strings are instances of the class ‘String’. When a string is used in programs,

10/21/10 8

String Arithmetic . . .

• The ‘+’ sign does not mean “addition” when it is used with String objects. In other words, the ‘+’ sign, behave differently when it is used with Strings.

It represents

“Concatenate two strings”

The ‘+’ sign creates a single String that contains the concatenation its operands.

Page 9: 10/21/101. 2 What are Strings?... A combination of characters is a string. Strings are instances of the class ‘String’. When a string is used in programs,

02/10/10 9

Example 2. . .

class String_Example{

public static void main(String arg[]) {

;”String f_name=“Malan;”String m_initial=“C;”String l_name=“Karunarathne

+String full_name=f_name” “+m_initial+” “ l_name+;

;System.out.println(full_name) {

{

class String_Example{

public static void main(String arg[]) {

String f_name=“Malan”;String m_initial=“C”;String l_name=“Karunarathne”;

String full_name=f_name+“ ”+m_initial+“ ” +l_name;

System.out.println(full_name); {

{

Page 10: 10/21/101. 2 What are Strings?... A combination of characters is a string. Strings are instances of the class ‘String’. When a string is used in programs,

10/21/10 10

String Arithmetic (cont). . .

• Add numerical values to a String…Ex:

int num=45; String code=“EMP” + 45;

The compiler calls a method that turns the numerical values into a String, which can be added with the + sign.

• The operator += can be used with the String.Ex:

String fname=“Kamal”;fname=fname+”Perera”;

orfname+=“Perera”;

Page 11: 10/21/101. 2 What are Strings?... A combination of characters is a string. Strings are instances of the class ‘String’. When a string is used in programs,

10/21/10 11

String Methods . . .The String class provides number of methods to manipulate Strings.

Method Use

length() Number of characters in a String

charAt() The Character at the specified location in the String

toCharArray() Produce a char[] containing the characters in the String

equals() An equality check on the contents of the two Strings. Consider the case (Upper case & Lower case) of the characters.

equalsIgnoreCase() An equality check on the contents of the two Strings. The case of the characters are ignored.

Page 12: 10/21/101. 2 What are Strings?... A combination of characters is a string. Strings are instances of the class ‘String’. When a string is used in programs,

10/21/10 12

String Methods (Cont) . . .Method Use

compareTo() Result is negative, zero or Positive depending on the lexicographical ordering of the Strings and the argument. Uppercase & Lowercase are not equal

indexOf() Returns -1 if the argument is not found within the String. Otherwise returns the index where the argument starts.

lastIndexOf() Similar to the indexOf(). But searching starts from the back end of the String.

substring() Returns a new String object containing the specified character list.

concat() Returns a new String object containing the original String’s characters followed by the characters in the argument.

Page 13: 10/21/101. 2 What are Strings?... A combination of characters is a string. Strings are instances of the class ‘String’. When a string is used in programs,

10/21/10 13

String Methods (Cont) . . .

Method Use

toLowerCase() Returns a new String by converting all the characters of the String to the lower case letters.

toUpperCase() Returns a new String by converting all the characters of the String to the upper case letters.

Page 14: 10/21/101. 2 What are Strings?... A combination of characters is a string. Strings are instances of the class ‘String’. When a string is used in programs,

10/21/10 14

String Methods: Examples . . .

Ex (1): Find the length of Text

int length=address.length();int length=address.length();

Consider the following text.

String address=“32/A, Passara road, Badulla”;

Ex (2): Find the character in the 3th index

char ch=address.charAt(3);char ch=address.charAt(3);

Page 15: 10/21/101. 2 What are Strings?... A combination of characters is a string. Strings are instances of the class ‘String’. When a string is used in programs,

10/21/10 15

String Methods: Examples (Cont). . .

Ex (4): Find the index of last ‘,’

int index_comma=address.lastIndexOf(‘,’);int index_comma=address.lastIndexOf(‘,’);

Ex (5): Extract the street of the address (ie. Passara road)

String street=address.substring(6,index_comma);String street=address.substring(6,index_comma);

Ex (3): Find the index of Text ‘/’

int index_slash=address.indexOf(‘/’);int index_slash=address.indexOf(‘/’);

Page 16: 10/21/101. 2 What are Strings?... A combination of characters is a string. Strings are instances of the class ‘String’. When a string is used in programs,

10/21/10 16

String Methods: Examples (Cont). . .

Ex (7): Convert the address to lower case letters

String lower_case=address.toLowerCase();String lower_case=address.toLowerCase();

Ex (6): Convert the address to upper case letters

String upper_case=address.toUpperCase();String upper_case=address.toUpperCase();

Page 17: 10/21/101. 2 What are Strings?... A combination of characters is a string. Strings are instances of the class ‘String’. When a string is used in programs,

10/21/10 17

Command Line Arguments . . .

• JAVA enables to get different kinds of inputs by passing as command line arguments.

• Command line arguments are given when the JAVA program is run.

• The JAVA program stores the command line arguments in the array of Strings which is passed to the main method in the program.

public static void main(String arg[]){

}

Page 18: 10/21/101. 2 What are Strings?... A combination of characters is a string. Strings are instances of the class ‘String’. When a string is used in programs,

10/21/10 18

Command Line Arguments (Cont) . . .

Apple Orange Lime

public static void main(String arg[]){

}

Page 19: 10/21/101. 2 What are Strings?... A combination of characters is a string. Strings are instances of the class ‘String’. When a string is used in programs,

10/21/10 19

Command Line Arguments (Cont) . . .

Apple Orange Lime

arg[0] arg[1] arg[2]

Number of arguments given in the command line (length of the String array) is given by,

arg.length

Page 20: 10/21/101. 2 What are Strings?... A combination of characters is a string. Strings are instances of the class ‘String’. When a string is used in programs,

10/21/10 20

Example . . .

public static void main(String arg[]){

for(int i=0;i<arg.length;i++){

;System.out.println(arg[i])

{{

public static void main(String arg[]){

for(int i=0;i<arg.length;i++){

System.out.println(arg[i]);

{{

Page 21: 10/21/101. 2 What are Strings?... A combination of characters is a string. Strings are instances of the class ‘String’. When a string is used in programs,

10/21/10 21

Command Line Arguments (Cont) . . .

• All the arguments which are passed to the main method are considered as Strings. In order to treat them as non-Strings, they should be converted into specific type.

These Strings should be converted to integer values

Page 22: 10/21/101. 2 What are Strings?... A combination of characters is a string. Strings are instances of the class ‘String’. When a string is used in programs,

10/21/10 22

Converting Strings to Numerical values . . .

• Converting Strings to integer values.String num=“34”;int n=Integer.parseInt(num);

• Converting Strings to float values.String num=“34.78”;float n=Float.parseFloat(num);

•Converting Strings to float values.String num=“34.98”;double n=Double.parseDouble(num);