class SimpleString { private char[] cs; private int size; /** * Following standard API documentation: * This constructor initializes a newly created SimpleString object so * that it represents the sequence of characters currently contained in * the character array argument. The contents of the character array * are copied; subsequent modification of the character array does not * affect the newly created string. */ public SimpleString (char[] sourceChars) { size = sourceChars.length; cs = new char[size]; System.arraycopy(sourceChars, 0, cs, 0, size); } public int length () { return size; } /** * Again following standard API documentation: * If a character with value ch occurs in the character sequence * represented by this SimpleString object, then the index of the * first such occurrence is returned. If no such character occurs * in this string, then -1 is returned. */ public int indexOf (int ch) { for (int i=0; i=0; i--) { if (cs[i] == ch) return i; } return -1; } /** * If the character oldChar does not occur in the character * sequence represented by this SimpleString object, then a * reference to this SimpleString object is returned. Otherwise, a * new SimpleString object is created that represents a character * sequence identical to the character sequence represented by * this SimpleString object, except that every occurrence of * oldChar is replaced by an occurrence of newChar. */ public SimpleString replace (char oldChar, char newChar) { if (indexOf(oldChar) == -1) return this; // oldChar does not occur else { char[] newcs = new char[size]; for (int i=0; iOtherwise, if there is no character with a code greater than * \u0020 in the string, then a new SimpleString object * representing an empty string is created and returned. *

Otherwise, let k be the index of the first character in the * string whose code is greater than \u0020, and let m be the * index of the last character in the string whose code is greater * than \u0020. A new SimpleString object is created, representing * the substring of this string that begins with the character at * index k and ends with the character at index m. */ public SimpleString trim () { char spaceChar = '\u0020'; if (size == 0 || (cs[0] > spaceChar && cs[size-1] > spaceChar)) { return this; } // search for first non-blank int k = -1; for (int i=0; i spaceChar) k = i; } // search for last non-blank int m = size; for (int i=size-1; i>=0; i--) { if (cs[i] > spaceChar) m = i; } // if all blank if (k == -1 && m == size) { char[] emptycs = new char[0]; return new SimpleString(emptycs); } // copy chars between k and m inclusive int newsize = m - k + 1; char[] newcs = new char[newsize]; System.arraycopy(cs, k, newcs, 0, newsize); return new SimpleString(newcs); } public String toString () { return new String(cs); } } class TestSimpleString { public static void main (String[] args) { char[] cs = { 'h', 'e', 'l', 'l', 'o' }; SimpleString ss = new SimpleString(cs); System.out.println("SimpleString: " + ss); System.out.println("Length = " + ss.length()); System.out.println("indexOf('l') = " + ss.indexOf('l')); System.out.println("lastIndexOf('l') = " + ss.lastIndexOf('l')); System.out.println("indexOf('x') = " + ss.indexOf('x')); System.out.println("lastIndexOf('x') = " + ss.lastIndexOf('x')); System.out.println("replace('l','L') = " + ss.replace('l', 'L')); char[] csp = { ' ', ' ', ' ', 'x', ' ', ' ' }; ss = new SimpleString(csp); System.out.println(); System.out.println("SimpleString: *" + ss + "*"); System.out.println("trim(): *" + ss.trim() + "*"); } }