Strings Hints And Tips
.
Summary Strings are one of the most commonly used features so understanding how they work and how to work with them is essential

Sun ""JavaDoc" http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

1.15 has expanded support for the java.lang.String package

Method Since Native Description
String() 1.0 yes constructor
String( char[] c) 1.0 yes constructor
String( byte[] b) 1.0 yes constructor
String( String s) 1.0 yes constructor
byte[] getBytes 1.0 yes Gets the underlying bytes of a string
static String valueOf(int i) 1.0 yes Generates a string value of the integer
static String valueOf(long l) 1.0 yes Generates a string value of the long
static String valueOf(Object o ) 1.15 no Invokes the toString() method of the object. Currently only functions where the object is a StringBuffer
boolean equals 1.0 yes Compares this object to another, if both strings does a character-wise compare
String substring(int beginIndex, int endIndex) 1.15 no Returns a new string that is a substring of this string
String substring(int beginIndex) 1.15 no Returns a new string that is a substring of this string
String boolean endsWith(String suffix) 1.15 no Tests if this string ends with the specified suffix
String public boolean startsWith(String prefix) 1.15 no Tests if this string starts with the specified prefix
String public boolean startsWith(String prefix, int toffset) 1.15 no Tests if this string starts with the specified prefix beginning a specified index
int indexOf(int ch) 1.15 no Returns the index within this string of the first occurrence of the specified character
int indexOf(int ch, int fromIndex) 1.15 no Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index
int lastIndexOf(int ch) 1.15 no Returns the index within this string of the last occurrence of the specified character
int lastIndexOf(int ch, int fromIndex) 1.15 no Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index
int length() 1.0 yes Returns the length of this string
char charAt(int index) 1.0 yes Returns the char value at the specified index, in 1.15 this was enhanced to return full unicode from FLASH strings. See notes on String storage formats

String Storage Formats

There are two forms of String storage. FLASH Storage, HEAP Storage.

FLASH Storage - FlashStrings

When a String is created with a String literal then a FLASH storage format string is created. In this format, the String object contains a FLASH address location of the String. The FLASH Stored string is stored in 16Bit word format, in 18F devices and 14Bit Word format in 16F device. When stored in 16Bit word format this is compatible with unicode 16Bit Strings. A literal String can be up to 65536 words long which allows literal Strings to be used very efficiently as binary resources. The charAt() method will return a 16bit char which should be cast to a int to

There are several techniques you can use to use Strings effectively

  • String literals can be declare directly, eg String = "HI";
  • String literals can be declared in unicode format, eg String = "\u0048\u0049";
  • String literals can be declared in compressed unicode format, eg String = "\u4948";

String Access

  • Use directly
  • Use charAt() to access 16Bit char
  • Use a FlashStreamer eg FlashRAW16InputStream

Examples

The String "HI" = { 0x48, 0x 0x49 }

                String hi = "HI"; 


                //This stores 0x0048, 0x0049 


                String hiUnicode = "\u0048\u0049";


                //This stores 0x0048, 0x0049 


                String hiCompressed = "\u4948";


                //Stores "HI" in a single word


                System.out.print( hi ) ;         //Prints Hi
                System.out.print( hiUnicode ) ; //Prints Hi


                int compressed = hiCompressed.charAt(0);


                System.out.print( (char) (compressed   & 0xFF) );        //Prints H
                System.out.print( (char) ((compressed >> 8) & 0xFF)  ); //Prints i 


                FlashRAW16InputStream in = new FlashRAW16InputStream(hiCompressed) ;


                while( true ){
                           int c = in.read();
                           if( c == -1){
                               break;
                           }
                           System.out.print( (char) c ); //Prints H,I


                }

HEAP Storage - HeapStrings

Strings can also exist in HEAP memory. Since muvium devices typically have very limited heap it is prefered to only use the Heap for temporary storage of the String, eg computed Strings and then allow the garbage collector to free the string. So it is not a good idea to cache HEAP strings for later use, it is better to dynamically create them, use them, then loose them. This is a general philosophy when using muvium. A Heap string is a more standard string in that it contains a reference to an array of bytes. Heap strings are created when you use a constructor passing an array or when a String is computed using the String.valueOf() or Object.toString(). Heap strings use a char[] which is in fact an 8 bit array. This is different to the java standard of 16 bit char[] but due to the extreme resource limits of the Heap the 8 bit char was chosen. This covers the regular ASCII range and is rarely an issue. String requiring 16Bit unicode format should use the Literal Flash format.

NOTE In the case of a StringBuffer toString() can result in a large HeapString. It is generally better to pass a StringBuffer and iterate charAt() over the StringBuffer which has an efficient storage of Strings and keeps ""FlashStrings" in Flash.
 Examples Creating HeapStrings


                //Creates a new HeapString
                String hi = new String( new byte[]{'H','I'} );


                //Creates a StringBuffer with FlashStrings
                StringBuffer buf = new StringBuffer();
                buf.append( "HI" );
                buf.append( "HI" );


                //Do this to keep FlashStrings in Flash while printint out ( efficient )
                for(int i = 0; i < buf.length() ; i++){
                    System.out.print( (char) buf.charAt(i) );
                }


                //This creates HeapString 'HIHI'( CAREFUL!! easy to create large HeapStrings )
                String hi2 = buf.toString();


                String hi3 = String.valueOf( 100 );


                System.out.println( hi );
                System.out.println( hi2 );
                System.out.println( hi3 );

Hints

Using Strings with println

The println, from PrintStream and PrintWriter appends a LF ( chr(10) ) to the end of a String when it prints. This may not be compatible with the way you view the Strings, for example Hyperterminal by default uses CRLF chr(10) + chr( 13 ). Here are some tips for getting the correct formatting

        //Add the correct formating directly to the String


        System.out.print("PrintMe\r\n"); //Add the crlf directly
        System.out.println("PrintMe\r"); //Add the cr and allow the LF to be appended by println
        System.out.print("\r\n\prepend the clrf");