comp102 lec 9

13
STRINGS

Upload: fraz-bakhsh

Post on 19-May-2015

282 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Comp102   lec 9

STRINGS

Page 2: Comp102   lec 9

String class

The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.

Strings are constant; their values cannot be changed after they are created.

For example:String str = "abc"; is equivalent to:char data[] = {'a', 'b', 'c'};String str = new String(data);

Page 3: Comp102   lec 9

MethodsThe class String includes methods for

examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a stringwith all characters translated to uppercase or to

lowercase

Page 4: Comp102   lec 9

ConcatenationString name = “Saira";String introduction = "Hello there, my name is " + name;concat(String str)Concatenates the specified string to the end of this string.public class test

{

public static void main(String [] args)

{

String str = "hello";

String str1 = "World";

String str2=str.concat(str1);

System.out.println(str2);

}

}

Page 5: Comp102   lec 9

Character at given position

getting the character at a given position within the string -- charAt()

public class test{

public static void main(String [] args){

String str = "hello";char c= str.charAt(1);System.out.print(c);

}}

Page 6: Comp102   lec 9

getting the number of characters in a string -- length()

public class test{

public static void main(String [] args){

String str = "hello";int x= str.length();System.out.print(x);

}}

Page 7: Comp102   lec 9

extracting a substring from a string -- substring()

public class test{

public static void main(String [] args){

String str = "hello World";String str1= str.substring(2,8);System.out.print(str1);

}}

Page 8: Comp102   lec 9

Compare two strings for equality – equals()

public class test{

public static void main(String [] args){

String str = "hello";String str1 ="hello";System.out.println(str.equals(str1));

}}

Page 9: Comp102   lec 9

Compare two strings – compareTo()

=0 indicate equality>0 indicate calling string follows argument string<0 indicate calling string precedes argument string

public class test{

public static void main(String [] args){

String str = "hello";String str1 ="abcde";System.out.println(str.compareTo(str1));

}}

Page 10: Comp102   lec 9

Tests if this string ends with the specified suffix.

public class test{

public static void main(String [] args){

String str = "hello world";String str1 ="ld";System.out.println(str.endsWith(str1));

}}

Page 11: Comp102   lec 9

Replacing all occurrences of oldChar in string with newChar

public class test{

public static void main(String [] args){

String str = "hello world";String str1 = str.replace('l','x');System.out.println(str1);

}}

Page 12: Comp102   lec 9

Converts String to character arraypublic class test{

public static void main(String [] args){

String str = "hello world";char []array =str.toCharArray();System.out.println(array);System.out.println(array[0]);

}}

Page 13: Comp102   lec 9

Converts all characters to lowercase or uppercase

public class test{

public static void main(String [] args){

String str = "hello world";String str1 = str.toUpperCase();String str2= "HHHElloooo";String str3 = str2.toLowerCase();System.out.println(str1);System.out.println(str3);

}}