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. |
| String | Literal 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. |
| StringBuffer | Java uses StringBuffer instances to handle string concatenation. |
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();
}
|