Your SlideShare is downloading. ×
Comp102   lec 9
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Comp102 lec 9

270
views

Published on


0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
270
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. STRINGS
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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); }}