1 / 11

String Class in Java

String Class in Java. java.lang Class String java.lang.Object java.lang.String We do not have to import the String class since it comes from java.lang. An object of the String class represents a string of characters. “abcde”;. Two ways to create a String object.

zazu
Download Presentation

String Class in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. String Class in Java • java.lang Class String • java.lang.Objectjava.lang.String • We do not have to import the String class since it comes from java.lang. • An object of the String class represents a string of characters. “abcde”;

  2. Two ways to create a String object • Create a String literal: String str = "abc"; • With the key word new since it is an object String str = new String(“abc”);

  3. Empty Strings • An empty string has no characters; • Its contents are “null”. String s1 = “"; String s2 = new String(); Empty strings

  4. String indexes • String is a sequence of characters. Each letter in the string has its own index location and can be accessed by it. index location The length of the String is how many letters it contains: 8 The last index of the String is length – 1; 7

  5. Index locations Index locations are from 0 to length-1 String s = “strawberry”; strawberry // 10 letters in the word // index from 0 to 9 int len = s.length() // 10 int lastIndex = s.length()-1 //9 S is the String object I created. Objects call methods with a dot operator.

  6. String Methods: • Page 78 • There are many ways to manipulate Strings. • Look on page 78 those tested on AP • You always use the period to separate the object from the method. • int len = s1.length();

  7. int length (); returns an int Returns the number of characters in the string Methods — length() String f = “Flower”; String w = “Wind”; Returns: 6 4 int lenF = f.length(); int lenW = w.length();

  8. Strings have index locations from 0 to length-1 Methods — substring String s2 = s.substring (i, k); returns the substring of chars in positions from i to k-1 String s3 = s.substring (i); returns the substring from i char to the end String s = “strawberry”; strawberry i k strawberry i Returns: raw rawberry String s2 = s.substring (2,5); start at 2 end at 4 String s3 = s.substring (2); start at 2 thru end

  9. String n = “computer"; String one = n.substring(0,7); String two = n.substring(1,6); String three = n.substring(2,5); String four = n.substring(4); String five = n.substring(3); String six = n.substring(1,n.length()-2); String seven = six.substring(0, n.length()/2); s.substring(i, k); returns the substring of chars in positions from i to k-1 s.substring(i); returns the substring from i char to the end

  10. Methods — Concatenation String s1 = “obi”; String s2 = “wan”; String result = s1 + s2; obiwan String result = s1.concat (s2); the same as s1 + s2 obiwan

  11. Methods — Find (indexOf) Index of return the index location of the first occurrence of the character requested. String date ="July 5, 2012 1:28:19 PM"; date.indexOf ('J'); 0 date.indexOf ('2'); 8 date.indexOf ("2012"); 8 date.indexOf ('2', 9); 11 date.indexOf ("2020"); -1 date.lastIndexOf ('2'); 15 0 8 11 15 Returns: (starts searching at position 9) (not found)

More Related