010506
J. W. Rider

Text Handling

Most computer programs must manipulate text in some form or another.

At the lowest level, the Java programming language stores text as atomic characters, either in variables of type char, or in objects of class Character.

Sequences of characters are "strings". Java has several storage mechanisms for strings, all of which serve some use at various points when handling text. There are four mechanisms designed explicitly for strings.

Java string storage
byte[]Pre-Unicode programmers will tend to think of strings as an array of bytes.
char[]Deals with character strings in their raw form. This provides the greatest flexibility for the programmer, but the programmer is responsible for declaring the array, determining the length of the string, and making sure that the array does not overflow.
StringLiteral strings in Java programs are automatically converted to instances of the class String. String objects are "immutable" and may not be changed after being created.
StringBufferJava uses StringBuffer instances to handle string concatenation.


The following example method combines together many of the techniques that you've learned for declaring variables and objects, using selection and iteration constructs, and different techniques for handling text. The method takes a String argument, and returns a String result that if displayed would look like the original string would look in your code. The method is useful for displaying the contents of strings when the strings might contain unreadable characters.

   public static String enquote(String s)
   {
      if (s == null) return null;

      StringBuffer sb = new StringBuffer();
      sb.append("\"");
      for (int i=0; i< s.length(); i++)
      {
         char c=s.charAt(i);
         switch (c)
         {
            case '\b': sb.append("\\b"); break; //backspace
            case '\t': sb.append("\\t"); break; //tab
            case '\n': sb.append("\\n"); break; //newline
            case '\f': sb.append("\\f"); break; //formfeed
            case '\r': sb.append("\\r"); break; //return
            case '\"': sb.append("\\\""); break; //double quote
            case '\'': sb.append("\\\'"); break; //single quote
            case '\\': sb.append("\\\\"); break; //backslash
            default:
               if(c>=' ' && c< '\u007f') sb.append(c); // readable ASCII
               else // convert to unicode form
               {
                  char[] hexdigits=new char[4];
                  for (int x=(int)c, j=hexdigits.length-1; j>=0; j--, x /= 16)
                      hexdigits[j]=Character.forDigit(x % 16, 16); 
                  sb.append("\\u").append(hexdigits);
               }
         } 
      }
      sb.append("\"");
      return sb.toString();
   }
1