Java .

Java Strings

java.lang.String

the String constructors

                          
//Note: Strings are immutable, 
//  so once a String is constructed it can not be changed.
//  StringBuffer is like String, however it is mutable.

//construct an empty string String emptyString = new String();

//constructors using a byte[] String stringFromByteArray = new String(byteArrayIn);

String stringFromByteArray = new String(byteArrayIn, intOffsetToCopyFrom, intLengthToCopy);

String stringFromByteArray = new String(byteArrayIn, encodingString);

String stringFromByteArray = new String(byteArrayIn, intOffsetToCopyFrom, intLengthToCopy, encodingString);

//constructors using a char[] String stringFromCharArray = new String(charArrayIn);

String stringFromCharArray = new String(charArrayIn, intOffsetToCopyFrom, intLengthToCopy);

//constructor using a String String stringFromString = new String(stringIn);

//constructor using a StringBuffer String stringFromStringBuffer = new String(stringBufferIn);

Finding out about a String

int comparisonValue = 
  stringToCompare.compareTo(objectToCompareTo);
  //note: objectToCompareTo must really be a String.
  //returns an int value of zero if Strings are equivalent
  //returns an int value less than zero if 
  //  stringToCompare is less than objectToCompareTo 
  //returns an int value greater than zero if 
  //  stringToCompare is greater than objectToCompareTo

int comparisonValue = stringToCompare.compareTo(stringToCompareTo); //returns an int value of zero if Strings are equivalent //returns an int value less than zero if // stringToCompare is less than stringToCompareTo //returns an int value greater than zero if // stringToCompare is greater than stringToCompareTo

int comparisonValue = stringToCompare.compareToIgnoreCase(stringToCompareTo); //returns an int value of zero if // Strings are equivalent, ignoring case //returns an int value less than zero if // stringToCompare is less than stringToCompareTo, // ignoring case //returns an int value greater than zero if // stringToCompare is greater than stringToCompareTo, // ignoring case

boolean doesStringAendWithStringB = stringA.endsWith(stringB);

boolean isObjectEqualToString = stringToCompare.equals(objectToCompare);

boolean isObjectEqualToString = stringToCompare.equalsIgnoreCase(objectToCompare);

int stringHashcode = stringToGetHashcodeFrom.hashcode();

int stringLength = stringToGetLengthOf.length();

boolean doesRegionMatch = stringAToCompare.regionMatches( booleanShouldIgnoreCase, intOfStringAoffsetToStartAt, stringBToCompare, intOfStringBoffsetToStartAt, intLengthToCompare);

boolean doesRegionMatch = stringAToCompare.regionMatches( intOfStringAoffsetToStartAt, stringBToCompare, intOfStringBoffsetToStartAt, intLengthToCompare);

Finding things in a String

int indexOfFoundCharacter = 
  stringToFindCharacterIn.charAt(intOfCharacter);
  //finds first occurance, 
  //  returns -1 if character is not found

int indexOfFoundCharacter = stringToFindCharacterIn.charAt( intOfCharacter, intOffsetToStartAt); //finds first occurance starting at offset, // returns -1 if character is not found

int indexOfFoundString = stringToFindStringIn.charAt(stringToFind); //finds first occurance, // returns -1 if String is not found

int indexOfFoundString = stringToFindStringIn.charAt( stringToFind, intOffsetToStartAt); //finds first occurance starting at offset, // returns -1 if String is not found

int indexOfFoundCharacter = stringToFindCharacterIn.lastIndexOf(intOfCharacter); //finds last occurance, // returns -1 if character is not found

int indexOfFoundCharacter = stringToFindCharacterIn.lastIndexOf( intOfCharacter, intOffsetToStartAt); //finds last occurance starting at offset, // returns -1 if character is not found

int indexOfFoundString = stringToFindStringIn.lastIndexOf( stringToFind); //finds last occurance, // returns -1 if String is not found

int indexOfFoundString = stringToFindStringIn.lastIndexOf( stringToFind, intOffsetToStartAt); //finds last occurance starting at offset, // returns -1 if String is not found

boolean doesRegionMatch = stringAToCompare.regionMatches( booleanShouldIgnoreCase, intOfStringAoffsetToStartAt, stringBToCompare, intOfStringBoffsetToStartAt, intLengthToCompare);

boolean doesRegionMatch = stringAToCompare.regionMatches( intOfStringAoffsetToStartAt, stringBToCompare, intOfStringBoffsetToStartAt, intLengthToCompare);

boolean doesStringStartWithStringToFind = stringToFindStringAtStart.startsWith(stringToFind);

boolean doesStringStartWithStringToFind = stringToFindStringAtStart.startsWith( stringToFind, intOffsetToCheckAt);

Getting things out of Strings

                             
char charAtIndex = 
  stringToGetCharFrom.charAt(intIndex);

String stringAandStringB = stringA.concat(stringB);

byte[] byteArrayFromString = stringToGetByteArrayFrom.getBytes();

byte[] byteArrayFromString = stringToGetByteArrayFrom.getBytes( stringCharacterEncoding);

stringToGetCharArrayFrom.getChars( intOffsetToCopyFromInclusive, intOffsetToCopyToExclusive, characterArrayToCopyInto, intOffsetInCharacterArrayToStartCopy);

String canonicalRepresentationOfStringA = stringA.intern();

String stringWithReplacements = stringToReplaceCharsIn.replace( charToFind, charToReplaceWith);

String substring = string.substring(intBeginningIndexInclusive);

String substring = string.substring( intBeginningIndexInclusive, intEndingIndexExclusive);

char[] charArrayFromString = stringToGetCharArrayFrom.toCharArray();

String lowercaseString = stringToGetLowerCaseStringFrom.toLowerCase();

String lowercaseStringUsingLocale = stringToGetLowerCaseStringFrom.toLowerCase(localeToUse); //uses java.util.Locale

String stringAsString = stringToReturnAsString.toString(); //returns String as a String

String uppercaseString = stringToGetUpperCaseStringFrom.toUpperCase();

String lowercaseStringUsingLocale = stringToGetUpperCaseStringFrom.toUpperCase( localeToUse); //uses java.util.Locale

String trimmedString = stringToTrim.trim(); //Removes all whitespaces. //Whitespaces are: horizontal tabs, vertiacal tabs, // line feeds, form feeds, carriage returns, // file separators, group separators, // record separators, unit separators, // Unicode space separators (not a no-break space), // Unicode line separators, and Unicode // paragraph separators.

valueOf()

                             
String stringFromCharArray = 
  String.copyValueOf(charArray);

String stringFromCharArray = String.copyValueOf(charArray, intOffsetToCopyFrom, intLengthToCopy);

String stringFromBoolean = String.valueOf(booleanIn); //returns "true" or "false"

String stringFromChar = String.valueOf(charIn);

String stringFromCharArray = String.valueOf(charArrayIn);

String stringFromCharArray = String.valueOf(charArrayIn, intOffsetToCopyFrom, intLengthToCopy);

String stringFromDouble = String.valueOf(doubleIn);

String stringFromFloat = String.valueOf(floatIn);

String stringFromInt = String.valueOf(intIn);

String stringFromLong = String.valueOf(longIn);

String stringFromObject = String.valueOf(objectIn);

References


online
The Java 2 Platform Specification
Books
Thinking in Java by Bruce Eckel
Core Java (TM) 2 Volume 1 - Fundamentals by Cay Horstmann and Gary Cornell
Comments
Sign in to be the first to comment on Java Strings.