Strings
Strings
Strings
String()
String s = new String();
String(char chars[ ])
char chars[] = { 'a', 'b', 'c' }; String s = new String(chars);
String Constructors
String(String strObj)
char c[] = {'J', 'a', 'v', 'a'}; String s1 = new String(c); String s2 = new String(s1);
String(byte asciiChars[ ])
byte ascii[] = {65, 66, 67, 68, 69, 70 }; String s1 = new String(ascii);
String Length
int length()
String Literals
String s2 = "abc"; // use string literal
Example-2:
String s = "four: " + 2 + 2; System.out.println(s);
toString()
String toString()
Example-1:
// Override toString() for Box class. class Box { double width; double height; double depth; Box(double w, double h, double d) { width = w; height = h; depth = d; 6 }
public String toString() { return "Dimensions are " + width + " by " + depth + " by " + height + "."; } } class toStringDemo { public static void main(String args[]) { Box b = new Box(10, 12, 14); // concatenate Box object String s = "Box b: " + b; // convert Box to string System.out.println(b); System.out.println(s); } 7 }
charAt( )
char charAt(int where)
getChars( )
void getChars(int sourceStart, int sourceEnd, char target[], int targetStart)
getBytes( )
byte[] getBytes()
toCharArray( )
char[] toCharArray()
Character Extraction
regionMatches( )
boolean regionMatches(int startIndex, String str2, int str2StartIndex, int numChars) boolean regionMatches(boolean ignoreCase, int startIndex, String str2, int str2StartIndex, int numChars)
String Comparison
compareTo( )
int compareTo(String str) int compareToIgnoreCase(String str) Value Less than zero Zero Meaning The invoking string is less than str. The two strings are equal.
10
indexOf( )
int int int int indexOf(int ch) indexOf(String str) indexOf(int ch, int startIndex) indexOf(String str, int startIndex)
lastIndexOf( )
int int int int lastIndexOf(int ch) lastIndexOf(String str) lastIndexOf(int ch, int startIndex ) lastIndexOf(String str, int startIndex)
Searching Strings
11
substring( )
String substring(int startIndex) String substring(int startIndex, int endIndex)
concat( )
String concat(String str)
replace( )
String replace(char original, char replacement) String replaceAll(String original, String replacement)
trim( )
String trim( )
Modifying a String
12
valueOf( )
static static static static String String String String valueOf(double num) valueOf(long num) valueOf(Object ob) valueOf(char chars[])
Decomposing
String[] split(String regExp) String[] split(String regExp, int max)
13
StringBuffer( )
The default constructor reserves room for 16 characters without reallocation.
StringBuffer(int size)
This version accepts an integer argument that explicitly sets the size of the buffer.
StringBuffer(String str)
This version accepts a String argument that sets the initial contents of the StringBuffer object and reserves room for 16 more characters without reallocation.
StringBuffer Constructors 14
int length() int capacity() void ensureCapacity(int capacity) void setLength(int len) char charAt(int where) void setCharAt(int where, char ch) void getChars(int sourceStart, int sourceEnd, char target[], int targetStart)
Some Methods
15
append( )
StringBuffer append(String str) StringBuffer append(int num) StringBuffer append(Object obj)
insert( )
StringBuffer insert(int index, String str) StringBuffer insert(int index, char ch) StringBuffer insert(int index, Object obj)
reverse( )
StringBuffer reverse()
16
replace( )
StringBuffer replace(int startIndex, int endIndex, String str)
substring( )
String substring(int startIndex) String substring(int startIndex, int endIndex)
17