Successfully reported this slideshow.
STRINGS
String classThe String class represents character strings. All string literals in Java programs, such as "abc", are imple...
MethodsThe class String includes methods for  examining individual characters of the sequence,  for comparing strings, ...
ConcatenationString name = “Saira";String introduction = "Hello there, my name is " + name;concat(String str)Concatena...
Character at given positiongetting the character at a given position within the string --  charAt()   public class test  ...
getting the number of characters in astring -- length()public class test{      public static void main(String [] args)    ...
extracting a substring from a string --substring()public class test{      public static void main(String [] args)      {  ...
Compare two strings for equality – equals()public class test{     public static void main(String [] args)     {           ...
Compare two strings – compareTo()=0 indicate equality>0 indicate calling string follows argument string<0 indicate call...
Tests if this string ends with the specified suffix.public class test{      public static void main(String [] args)      {...
Replacing all occurrences of oldChar in string withnewCharpublic class test{      public static void main(String [] args) ...
Converts String to character arraypublic class test{      public static void main(String [] args)      {            String...
Converts all characters to lowercase or uppercasepublic class test{      public static void main(String [] args)      {   ...
Upcoming SlideShare
Loading in …5
×

Comp102 lec 9

469 views

Published on

  • Be the first to comment

  • Be the first to like this

Comp102 lec 9

  1. 1. STRINGS
  2. 2. 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);
  3. 3. MethodsThe 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 string with all characters translated to uppercase or to lowercase
  4. 4. ConcatenationString 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); }}
  5. 5. 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); } }
  6. 6. getting the number of characters in astring -- length()public class test{ public static void main(String [] args) { String str = "hello"; int x= str.length(); System.out.print(x); }}
  7. 7. 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); }}
  8. 8. 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)); }}
  9. 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)); } }
  10. 10. 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)); }}
  11. 11. Replacing all occurrences of oldChar in string withnewCharpublic class test{ public static void main(String [] args) { String str = "hello world"; String str1 = str.replace(l,x); System.out.println(str1); }}
  12. 12. 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]); }}
  13. 13. Converts all characters to lowercase or uppercasepublic 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); }}

×