1 / 11

Strings

Strings. Contents. Constructing String Objects String Objects and References Common String methods Concatenation Comparing two String Objects Forming a substring Same Object/ Same Reference. 0. str. str. Java Strings. String Constructors. Empty String. String str = new String( );.

amandla
Télécharger la présentation

Strings

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. Strings Contents Constructing String Objects String Objects and References Common String methods Concatenation Comparing two String Objects Forming a substring Same Object/ Same Reference

  2. 0 str str Java Strings String Constructors Empty String String str = new String( ); With message String str = new String(“Java Strings’); Object Reference Note! String str = “Java Strings”; produces the same result

  3. Hello Dolly text str Hello Dolly str lo Do String Constructors With an array of characters char text [ ] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘ ‘, ‘D’, ‘o’, ‘l’, ‘l’, ‘y’}; String str = new String(text); With a subarray of a currently existing array of characters String(char[ ] data, int startpos, int length); String str = new String(text, 3, 5);

  4. String Constructors With an array of bytes byte [ ] theBytes = {(byte) ‘f’, (byte)’o ‘, (byte)’x’); String str = new String(theBytes); From subarray of an existing array of bytes String(byte [ ] byteArray, int startpos, int length); //form of constructor From the contents of a stringBuffer StringBuffer sb = new StringBuffer(“Hi Mom!”); String str = new String(sb);

  5. I like dogs. str1 I prefer cats. str2 String Objects String objects are immutable -- they cannot be changed once they have been created. References to string objects may be changed. String str1 = new String (“I like dogs.”); String str2 = new String(“I prefer cats.”); str1 = str2; //reassign reference Automatic garbage collection will reclaim unreferenced objects

  6. returns negative int if mssg target is less than compString, 0 if equal, and pos. int otherwise returns the substring starting at index indicated by start and ending at index indicated by end returns substring starting at index start to end of the source string returns index of the start of the first occurrence of substring in the source string, if present, else returns length of string Some Common String Processing Methods String concat(String anotherString); int compareTo(String compString); int compareToIgnoreCase(String compString); String substring(int start, int end); String substring(int start); int indexOf(String substring); String toUpperCase( ); String toLowerCase( );

  7. str1 This is the winter str2 of our discontent This is the winter of our discontent Note! The same result occurs for str1 = str1 + str2; Concatenation Declare and construct two strings, then concatenate str2 to the end of str1 String str1 = new String(“This is the winter “); String str2 = new String(“of our discontent”); str1 = str1.concat(str2); //message to str1 to concat str2 to its end Automatic garbage collection will reclaim unrferenced objects

  8. Comparing Strings String str1 = new String (“Bob”); String str2 = new String (“boy”); String stemp; if(str1.compareToIgnoreCase(str2) > 0) { stemp = str1; str1 = str2; str2 = stemp; } System.out.println(“The alphabetized listing is : ” + str1 + “,” + str2);

  9. end pos is the next index after the last character in the substring. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 P l e a s e d o n ‘ t e t a Substrings String strSource = new String(“Please don’t eat the daisies!”); String subString = strSource.substring(7,16); System.out.println(subString); The output from this code fragment is: don’t eat

  10. str1 A string str2 Same Object / Same Reference The String class has two methods boolean equals(String anotherString) and boolean equalsIgnoreCase(String anotherString) To test whether two string objects are the same (two strings have the same value) The relational operator == is used to test whether two Strings reference the same object.

  11. false true true false true Same Object / Same Reference String s1 = new String (“I am a string”); String s2 = “ a string”; String s3 = s1.substring(0,4); String s4 = “ a string”; String s5 = s3 + s2; if (s5 == s1) { } if (s1.equals(s5)) { } if (s2 == s4) { } if (s5 == s3 + s4) { } if (s5.equals(s3+s4)) { } The result of op. is stored in temporary string A new object is created when 2 strings are concatenated If the object already exists, it will be used with a duplicate ref. by new string

More Related