Home Listings By Name By Subject Email www.tomswan.com Help |
© 2001 by Tom Swan. All rights reserved.
Updated: 6/4/01 12:20:54 PM
Return to Listings page
Switch to Solutions by name page
Switch to Solutions by subject page
001: public String(); 002: public String(String value); 003: public String(char value[]); 004: public String(char value[], int offset, int count); 005: public String(byte bytes[], int offset, int length, String enc); 006: public String(byte bytes[], String enc); 007: public String(byte bytes[], int offset, int length); 008: public String(byte bytes[]); 009: public String(StringBuffer buffer);Return to top
001: class NullString { 002: // Return a null string reference 003: public static String badString() { 004: String s = null; 005: return s; 006: } 007: // Return a zero-length string 008: public static String goodString() { 009: String s = new String(); 010: return s; 011: } 012: // Try the preceding two methods 013: // The NullPointerException is intentional 014: public static void main(String args[]) { 015: String s; 016: s = badString(); // Change to goodString() and rerun 017: System.out.println("Length(s) = " + s.length()); 018: } 019: }Return to top
001: class CharArray { 002: public static void main(String args[]) { 003: String s; 004: char array[] = new char[26]; 005: for (char c = 'a'; c <= 'z'; c++) 006: array[c - 'a'] = c; 007: s = new String(array); 008: System.out.println(s); 009: } 010: }Return to top
001: import java.io.UnsupportedEncodingException; 002: 003: class CharEncoding { 004: public static void main(String args[]) { 005: String s; 006: byte byteArray[] = new byte[26]; 007: for (int i = 0; i < 26; i++) 008: byteArray[i] = (byte)(i + 'a'); 009: // Convert byte array to a String using an encoding 010: try { 011: s = new String(byteArray, "UTF-8"); 012: System.out.println(s); 013: } catch (UnsupportedEncodingException e) { 014: System.out.println(e.getMessage()); 015: } 016: } 017: }Return to top
001: // String class inspection methods 002: public int length(); 003: public char charAt(int index); 004: public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin); 005: public byte[] getBytes(String enc); 006: public byte[] getBytes(); 007: public int hashCode(); 008: 009: // String class comparison methods 010: public boolean equals(Object anObject); 011: public boolean equalsIgnoreCase(String anotherString); 012: public int compareTo(String anotherString); 013: public int compareTo(Object o); 014: public int compareToIgnoreCase(String str); 015: public boolean regionMatches(int toffset, String other, int ooffset, int len); 016: public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len); 017: public boolean startsWith(String prefix, int toffset); 018: public boolean startsWith(String prefix); 019: public boolean endsWith(String suffix); 020: 021: // String class index methods 022: public int indexOf(int ch); 023: public int indexOf(int ch, int fromIndex); 024: public int lastIndexOf(int ch); 025: public int lastIndexOf(int ch, int fromIndex); 026: public int indexOf(String str); 027: public int indexOf(String str, int fromIndex); 028: public int lastIndexOf(String str); 029: public int lastIndexOf(String str, int fromIndex); 030: 031: // String class conversion methods 032: public String substring(int beginIndex); 033: public String substring(int beginIndex, int endIndex); 034: public String concat(String str); 035: public String replace(char oldChar, char newChar); 036: public String toLowerCase(Locale locale); 037: public String toLowerCase(); 038: public String toUpperCase(Locale locale); 039: public String toUpperCase(); 040: public String trim(); 041: public char[] toCharArray(); 042: public native String intern();Return to top
001: class MonthNames { 002: public static void main(String args[]) { 003: String s = "#January#February#March#April" + 004: "#May#June#July#August#September#October" + 005: "#November#December#"; 006: int i = 0, j; 007: while (i++ >= 0) { 008: j = s.indexOf('#', i); // i = starting index 009: if (j >= 0) { 010: String month = s.substring(i, j); 011: System.out.println(month); 012: } 013: i = j; 014: } 015: } 016: }Return to top
001: class Compare { 002: public static void main(String args[]) { 003: String s1 = "abcdefg"; 004: String s2 = "ABCDEFG"; 005: int result = s1.compareTo(s2); 006: if (result == 0) 007: System.out.println("s1 = s2"); 008: else if (result < 0) 009: System.out.println("s1 < s2"); 010: else // if (result > 0) 011: System.out.println("s1 > s2"); 012: } 013: }Return to top
001: import java.util.Locale; 002: 003: class StringLocale { 004: public static void main(String args[]) { 005: String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 006: System.out.println("Before : " + s); 007: s = s.toLowerCase(Locale.CANADA); 008: System.out.println("After : " + s); 009: } 010: }Return to top
001: class Concat { 002: public static void main(String args[]) { 003: String s1 = " One"; 004: String s2 = " Two"; 005: String s3 = " Three"; 006: String test = "Testing"; 007: test = test.concat(s1); 008: test = test.concat(s2); 009: test = test.concat(s3); 010: System.out.println(test); 011: } 012: }Return to top
001: class StringTrimmer { 002: public static void main(String args[]) { 003: String s = " blankety blank "; 004: System.out.println("Length before = " + s.length()); 005: s = s.trim(); // trim blanks from string 006: System.out.println("Length after = " + s.length()); 007: } 008: }Return to top
001: class StringIntern { 002: public static void main(String args[]) { 003: String s1 = "Unique string"; 004: String s2 = s1.intern(); 005: if (s1 == s2) 006: System.out.println("s1 equals s2"); 007: } 008: }Return to top
001: public static String valueOf(Object obj); 002: public static String valueOf(char data[]); 003: public static String valueOf(char data[], int offset, int count); 004: public static String copyValueOf(char data[], int offset, int count); 005: public static String copyValueOf(char data[]); 006: public static String valueOf(boolean b); 007: public static String valueOf(char c); 008: public static String valueOf(int i); 009: public static String valueOf(long l); 010: public static String valueOf(float f); 011: public static String valueOf(double d);Return to top
001: // StringBuffer class constructors 002: public StringBuffer(); 003: public StringBuffer(int length); 004: public StringBuffer(String str); 005: 006: // StringBuffer class length and capacity methods 007: public int length(); 008: public int capacity(); 009: public synchronized void ensureCapacity(int minimumCapacity); 010: public synchronized void setLength(int newLength); 011: 012: // StringBuffer class char methods 013: public synchronized char charAt(int index); 014: public synchronized void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin); 015: public synchronized void setCharAt(int index, char ch); 016: 017: // StringBuffer class append methods 018: public synchronized StringBuffer append(Object obj); 019: public synchronized StringBuffer append(String str); 020: public synchronized StringBuffer append(char str[]); 021: public synchronized StringBuffer append(char str[], int offset, int len); 022: public StringBuffer append(boolean b); 023: public synchronized StringBuffer append(char c); 024: public StringBuffer append(int i); 025: public StringBuffer append(long l); 026: public StringBuffer append(float f); 027: public StringBuffer append(double d); 028: 029: // StringBuffer class delete and replace methods 030: public synchronized StringBuffer delete(int start, int end); 031: public synchronized StringBuffer deleteCharAt(int index); 032: public synchronized StringBuffer replace(int start, int end, String str); 033: 034: // StringBuffer class substring methods 035: public String substring(int start); 036: public synchronized String substring(int start, int end); 037: 038: // StringBuffer class insert methods 039: public synchronized StringBuffer insert(int index, char str[], int offset, int len); 040: public synchronized StringBuffer insert(int offset, Object obj); 041: public synchronized StringBuffer insert(int offset, String str); 042: public synchronized StringBuffer insert(int offset, char str[]); 043: public StringBuffer insert(int offset, boolean b); 044: public synchronized StringBuffer insert(int offset, char c); 045: public StringBuffer insert(int offset, int i); 046: public StringBuffer insert(int offset, long l); 047: public StringBuffer insert(int offset, float f); 048: public StringBuffer insert(int offset, double d); 049: 050: // StringBuffer class other methods 051: public synchronized StringBuffer reverse(); 052: public String toString(); 053: private synchronized void readObject(java.io.ObjectInputStream s);Return to top
001: class StringAppend { 002: public static void main(String args[]) { 003: // Declare and initialize a StringBuffer object 004: StringBuffer buffer = new StringBuffer(80); 005: // Declare some variables of different types 006: boolean truth = false; 007: long value = 1000000; 008: char ch = '$'; 009: // Append literal strings and variables to buffer 010: buffer.append("You won "); 011: buffer.append(ch); 012: buffer.append(value); 013: buffer.append(" is a "); 014: buffer.append(truth); 015: buffer.append(" statement!"); 016: // Display the result 017: System.out.println(buffer); 018: } 019: }Return to top
001: // Character class constructor 002: public Character(char value); 003: 004: // Character class methods 005: public char charValue(); 006: public int hashCode(); 007: public boolean equals(Object obj); 008: public String toString(); 009: public static char toLowerCase(char ch); 010: public static char toUpperCase(char ch); 011: public static char toTitleCase(char ch); 012: public static int digit(char ch, int radix); 013: public static int getNumericValue(char ch); 014: 015: // Character class "is" methods 016: public static boolean isLowerCase(char ch); 017: public static boolean isUpperCase(char ch); 018: public static boolean isTitleCase(char ch); 019: public static boolean isDigit(char ch); 020: public static boolean isDefined(char ch); 021: public static boolean isLetter(char ch); 022: public static boolean isLetterOrDigit(char ch); 023: public static boolean isJavaLetter(char ch); 024: public static boolean isJavaLetterOrDigit(char ch); 025: public static boolean isJavaIdentifierStart(char ch); 026: public static boolean isJavaIdentifierPart(char ch); 027: public static boolean isUnicodeIdentifierStart(char ch); 028: public static boolean isUnicodeIdentifierPart(char ch); 029: public static boolean isIdentifierIgnorable(char ch); 030: public static boolean isSpace(char ch); 031: public static boolean isWhitespace(char ch); 032: public static boolean isISOControl(char ch); 033: 034: // Character class other methods 035: public static int getType(char ch); 036: public static char forDigit(int digit, int radix); 037: public int compareTo(Character anotherCharacter); 038: public int compareTo(Object o);Return to top
001: class ChRadix { 002: public static void main(String args[]) { 003: System.out.println("Min radix = " + Character.MIN_RADIX); 004: System.out.println("Max radix = " + Character.MAX_RADIX); 005: 006: int radix = 12, result; 007: char ch = '0'; 008: if (Character.MIN_RADIX <= radix && 009: radix <= Character.MAX_RADIX) { 010: while (ch <= 'Z') { 011: result = Character.digit(ch, radix); 012: if (result >= 0) 013: System.out.println( 014: ch + " in base " + radix + " = " + result); 015: else 016: System.out.println("Char " + ch + " undefined for radix"); 017: if (ch == '9') 018: ch = 'A'; 019: else 020: ch++; 021: } // while 022: } else 023: System.out.println("Radix " + radix + " out of range"); 024: } 025: }Return to top
001: class ChType { 002: 003: // Display type of ch (not all types listed) 004: public static void showType(char ch) { 005: int type = Character.getType(ch); 006: String s; 007: switch (type) { 008: case Character.UPPERCASE_LETTER: 009: s = "uppercase letter"; break; 010: case Character.LOWERCASE_LETTER: 011: s = "lowercase letter"; break; 012: case Character.DECIMAL_DIGIT_NUMBER: 013: s = "decimal digit number"; break; 014: case Character.OTHER_PUNCTUATION: 015: s = "punctuation symbol"; break; 016: case Character.MATH_SYMBOL: 017: s = "math symbol"; break; 018: case Character.CURRENCY_SYMBOL: 019: s = "currency symbol"; break; 020: default: 021: s = "unknown symbol"; 022: } 023: System.out.println("char " + ch + " : " + s + 024: " (" + (int)ch + ")"); 025: } 026: 027: public static void main(String args[]) { 028: showType('A'); 029: showType('z'); 030: showType('3'); 031: showType('!'); 032: showType('+'); 033: showType('$'); 034: showType('/u0123'); 035: } 036: }Return to top
001: import java.io.IOException; 002: 003: class InputString { 004: public static void main(String args[]) { 005: try { 006: StringBuffer buffer = new StringBuffer(64); 007: char ch; 008: // Prompt for and read a string 009: System.out.print("Type something: "); 010: while ((ch = (char)System.in.read()) != '/n') 011: buffer.append(ch); // Build string using ch 012: // Display string entered 013: System.out.println("You entered: " + buffer); 014: } catch (IOException e) { // Trap exception 015: System.out.println(e.toString()); // Display error 016: } 017: } 018: }Return to top
001: class CommandLine { 002: public static void main(String args[]) { 003: System.out.println("Number of arguments = " + args.length); 004: for (int i = 0; i < args.length; i++) { 005: System.out.println(args[i]); 006: } 007: } 008: }Return to top