Java String class is represented by character strings. On this java tutorial we will be diving into details of String class. Examples are provided further for us to visualize the usage of String. Methods of this class were discussed one by one and we have also provided example source code that deals with the proper usage.

Rules in dealing with Java String Class

  • values cannot be modified or altered after once they are created.
  • All string literals are treated as an instance of this class

String Creation Example

As you would have noticed we have presented various ways in creating String objects. We have used the System.out.println to print the String “this is a string”. The second example is a direct assignment of String literal “anderson” to String object name. The third one is assigning a value of String literal “New York” to object address, to shed light on assigning a value to our string using the keyword new. The fourth example assigned a value from the contents of “sample string tutorial”, from which it uses a method substring(start,end). As you can see on this example that “all literals are treated as an instance of String class. You might be wondering why would we need to create a new instance whenever we use a previously instantiated String object such as the address. This is shown on the last example that once a String object is created, values cannot be modified or altered.

String Class Method Examples

The following are the list of methods available. The method name is clickable and you will be redirected to specific method tutorial.

return Method Description
char charAt(int index) This method returns a value of char at index specified on the argument
int codePointAt(int index) This method returns an int value based on the unicode equivalent on each character between the string and the parameter input str. It gives an output of 0 if the two strings are equal.
int codePointBefore(int index) -
int codePointCount(int beginIndex, int endIndex) -
int compareTo(String anotherString) This method returns an int value based on the unicode equivalent on each character between the string and the parameter String input str
int compareToIgnoreCase(String str) -
String concat(String str) This method returns a new String object which is the result of combining this to the method argument.
boolean contains(CharSequence s) returns a boolean datatype which which is a result in testing if our String contains the characters specified on our method argument in CharSequence object type.
boolean contentEquals(CharSequence cs) returns a boolean data type if the input parameter is equal to the CharSequence in terms of character sequence.
boolean contentEquals(StringBuffer sb) returns a boolean data type if the input parameter is equal to the StringBuffer in terms of character sequence.
String copyValueOf(char[] data) -
String copyValueOf(char[] data, int offset, int count) -
boolean endsWith(String suffix) This method returns booelan data type. The endsWith() method of String class generally checks if the end of the String is equivalent to suffix method argument.
boolean equals(Object anObject) This method returns boolean data type which checks if the String is equals to the object input parameter on this method.
boolean equalsIgnoreCase(String anotherString) This method returns a boolean data type if the input parameter is equal to the String in terms of character sequence.
String format(Locale l, String format, Object… args) This method returns a String representation of the result of the object args based on the format specified on the method arguments.
string format(String format, Object… args) This method returns a String representation of the result of the object args based on the format specified on the method arguments.
byte[] getBytes() This method returns byte[] array
byte[] getBytes(Charset charset) This method returns byte[] array
void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin) Deprecated
byte[] getBytes(String charsetName) This method returns byte[] array
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) This method returns void.
int hashCode()  This method returns an int datatype which corresponds to the hash code of the string.
int indexOf(int ch) Returns the index of the first occurrence of the specified character.
int indexOf(int ch, int fromIndex) Returns the index of the first occurrence of the specified character, starting the search at the specified inde
int indexOf(String str) This method returns an int datatype which which corresponds to the index of the string where the method argument str has been found starting
int indexOf(String str, int fromIndex) This method returns an int datatype which which corresponds to the index of the string where the method argument str has been found starting from index declared as method argument fromIndex.
String intern() Returns a canonical representation for the string object.
boolean isEmpty() This method returns boolean data type which signifies if the String length is 0 or not. This method generally checks if the string is empty.
int lastIndexOf(int ch) Returns the index of the last occurrence of the specified character.
int lastIndexOf(int ch, int fromIndex) Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index.
int lastIndexOf(String str) This method returns an int datatype which which corresponds to the last index of the string where the method argument str has been found.
int lastIndexOf(String str, int fromIndex) This method returns an int datatype which which corresponds to the last index of the string where the method argument str has been found starting the search from index declared as method argument fromIndex .
int length() This method returns an int data type which is the number of Uncicode units of the String . This is generally just counts how many characters are in the String object.
boolean matches(String regex) This method returns a boolean datatype which which is a result in testing if our regex pattern matches that of our String.
int offsetByCodePoints(int index, int codePointOffset) -
boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) This is generally tests if two string region are equal. To put it simple we are comparing two strings out of the substrings of those string sources.
boolean regionMatches(int toffset, String other, int ooffset, int len) This is generally tests if two string region are equal. To put it simple we are comparing two strings out of the substrings of those string sources.
String replace(char oldChar, char newChar) This method returns a String datatype which which is a result in replacing oldChar with the newChar as declared on the method arguments. This is basically changing a character to a new character which results to a new String object.
String replace(CharSequence target, CharSequence replacement) This method returns a String datatype which which is a result in replacing target with replacement as declared on the method arguments.
String replaceAll(String regex, String replacement) This method returns a new String object as a result of replacing all the occurrences of the regex pattern with String parameter replacement.
String replaceFirst(String regex, String replacement) This method returns a new String object as a result of replacing the first occurrence of the regex pattern with parameter replacement.
String[] split(String regex) This method returns a character array which corresponds to the result of splitting the this object given a java regular expression as a method parameter.
string[] split(String regex, int limit) This method returns a character array which corresponds to the result of splitting the String object given a java regular expression as a method parameter and the tokens are limited using the method parameter limit.
boolean startsWith(String prefix) The startsWith() method of String class generally checks if the beginning of the String is equivalent to String prefix method argument.
boolean startsWith(String prefix, int toffset) The startsWith() method of String class generally checks if the beginning of the String given an offset index is equivalent to prefix method argument.
CharSequence subSequence(int beginIndex, int endIndex) This method returns a CharacterSequence which is the result of splitting the String from beginIndex to endIndex
String substring(int beginIndex) This method returns a String datatype which corresponds to a portion of the original string starting from the begin index until the end of the String.
String substring(int beginIndex, int endIndex) This method returns a String datatype which corresponds to a portion of the original string starting from the begin index until the endIndex.
char[] toCharArray() This method returns a character array which corresponds to the character elements of our String.
String toLowerCase() The toLowerCase() method is simply converts the string object into lower case.
String toLowerCase(Locale locale) The toLowerCase() method is simply converts the string object into lower case with respect to Locale declared as method argument.
String toString() -
String toUpperCase() The toUpperCase() method of String class simply used in conversion into upper case.
String toUpperCase(Locale locale) The toUpperCase() method of String class simply used in conversion into upper case with respect to Locale declared as method argument.
string trim() This method returns a new object which is a exact value of the original String stripped with leading and trailing spaces.
string valueOf(boolean b) This method returns a string representation of boolean data type.
String valueOf(char c) This method returns a string representation of char data type.
String valueOf(char[] data) This method returns a string representation of the character array data.
String valueOf(char[] data, int offset, int count) This method returns a string representation of a subset of character array data.
string valueOf(double d) This method returns a string representation of double data type.
String valueOf(float f) This method returns a string representation of float data type.
String valueOf(int i) This method returns a string representation of int data type.
String valueOf(long l) This method returns a string representation of long data type.
String valueOf(Object obj) This method returns a string representation of the Object obj specified as method argument. To make it simple, it is only simple call to the Object.toString() method.