Thursday, April 4, 2013

Strings : The Immutable Object


Strings of text are a fundamental and commonly used data type. In Java, however, strings are not a primitive type, like char, int, and float. Instead, strings are represented by the java.lang.String class, which defines many useful methods for manipulating strings. String objects are immutable: once a String object has been created, there is no way to modify the string of text it represents. Thus, each method that operates on a string typically returns a new String object that holds the modified string.

This code shows some of the basic operations you can perform on strings:


public class StringResearch

{


   public static void main(String[] args)
   {
   // Creating strings
      String s = "Now"; // String objects have a special literal syntax
      String t = s + " is the time."; // Concatenate strings with + operator
      String t1 = s + " " + 23.4; // + converts other values to strings
      t1 = String.valueOf('c'); // Get string corresponding to char value
      t1 = String.valueOf(42); // Get string version of integer or any value
      //t1 = Object.toString(); // Convert objects to strings with toString()
      // String length
      int len = t.length(); // Number of characters in the string: 16
      // Substrings of a string
      String sub = t.substring(4); // Returns char 4 to end: "is the time."
      sub = t.substring(4, 6); // Returns chars 4 and 5: "is"
      sub = t.substring(0, 3); // Returns chars 0 through 2: "Now"
      //sub = t.substring(x, y); // Returns chars between pos x and y-1
      int numchars = sub.length(); // Length of substring is always (y-x)
      // Extracting characters from a string
      char c = t.charAt(2); // Get the 3rd character of t: w
      char[] ca = t.toCharArray(); // Convert string to an array of characters
      t.getChars(0, 3, ca, 1); // Put 1st 3 chars of t into ca[1]-ca[3]
      // Case conversion
      String caps = t.toUpperCase(); // Convert to uppercase
      String lower = t.toLowerCase(); // Convert to lowercase
      // Comparing strings
      boolean b1 = t.equals("hello"); // Returns false: strings not equal
      boolean b2 = t.equalsIgnoreCase(caps); // Case-insensitive compare: true
      boolean b3 = t.startsWith("Now"); // Returns true
      boolean b4 = t.endsWith("time."); // Returns true
      int r1 = s.compareTo("Pow"); // Returns < 0: s comes before "Pow"
      int r2 = s.compareTo("Now"); // Returns 0: strings are equal
      int r3 = s.compareTo("Mow"); // Returns > 0: s comes after "Mow"
      r1 = s.compareToIgnoreCase("pow"); // Returns < 0 (Java 1.2 and later)
   // Searching for characters and substrings
      int pos = t.indexOf('i'); // Position of first 'i': 4
      pos = t.indexOf('i', pos+1); // Position of the next 'i': 12
      pos = t.indexOf('i', pos+1); // No more 'i's in string, returns -1
      pos = t.lastIndexOf('i'); // Position of last 'i' in string: 12
      pos = t.lastIndexOf('i', pos-1); // Search backwards for 'i' from char 11
      pos = t.indexOf("is"); // Search for substring: returns 4
      pos = t.indexOf("is", pos+1); // Only appears once: returns -1
      pos = t.lastIndexOf("the "); // Search backwards for a string
      String noun = t.substring(pos+4); // Extract word following "the"
      // Replace all instances of one character with another character
      String exclaim = t.replace('.', '!'); // Works only with chars, not substrings
      // Strip blank space off the beginning and end of a string
      String noextraspaces = t.trim();
      // Obtain unique instances of strings with intern()
      String s1 = s.intern(); // Returns s1 equal to s
      String s2 = "Now".intern(); // Returns s2 equal to "Now"
      boolean equals = (s1 == s2); // Now can test for equality with ==
   }

}



No comments:

Post a Comment