Java – String Methods

String Manipulation in Java

In this lesson, we are going to learn about various String methods. If you would need to manipulate strings, then these methods are handy.

 

Now, we are going to learn about six categories of methods

  1. Indexing Methods
  2. Comparison Methods
  3. Modifier Methods
  4. Case Modifiers Methods
  5. Substring Methods
  6. Byte and Character Methods
  7. Matching Methods
  8. Misc Methods

 

 

1. Indexing Methods

You you the indexing methods to find positions inside the string.

1 int indexOf(int ch)

Returns the index within this string of the first occurrence of the given character.

2 int indexOf(int ch, int fromIndex)

Returns the index within this string of the first occurrence of the given character, starting the search at the specified index.

3 int indexOf(String str)

Returns the index within this string of the first occurrence of the given substring.

4 int indexOf(String str, int fromIndex)

Returns the index within this string of the first occurrence of the given  substring, starting at the specified index.

5 int lastIndexOf(int ch)

Returns the index within this string of the last occurrence of the given  character.

6 int lastIndexOf(int ch, int fromIndex)

Returns the index within this string of the last occurrence of the given character, searching backward starting at the specified index.

7 int lastIndexOf(String str)

Returns the index within this string of the rightmost occurrence of the given substring.

8. int lastIndexOf(String str, int fromIndex)

Returns the index within this string of the last occurrence of the given substring, searching backward starting at the specified index.

 

 

2. Comparison Methods

We use the comparison to compare a string to another string or object.

SN Method and brief description
2 int compareTo(Object o)

Compares this String to the object given as parameter

3 int compareTo(String anotherString)

Compares one string with another string lexicographically.

4 int compareToIgnoreCase(String str)

Compares one string with another string lexicographically, and ignores case differences.

5 boolean equals(Object anObject)

Compares a string to the specified object and returns true if they are equal. Otherwise returns false.

6 boolean equalsIgnoreCase(String anotherString)

Compares a String with  another String, ignoring case considerations. Returns true if the two strings are equal.

7 boolean contentEquals(StringBuffer sb)

Returns true if and only if this String represents the same sequence of characters as the given StringBuffer. Returns false if otherwise

8 boolean startsWith(String prefix)

Checks if this string starts with the given prefix. Returns true if yes, otherwise false.

9 boolean startsWith(String prefix, int toffset)

Checks if a string starts with the given prefix beginning a specified index. It returns true is it does. Else, it returns false.

10 boolean endsWith(String suffix)

Checks if this string ends with the given suffix.

 

 

3. Modifier Methods

You use these set of methods to modify the value of a string. However, keep in mind that the changes are not persisted. This is because, as you know, Strings are immutable.

1. String replace(char c1, char c2)

It returns a new string that results from replacing all occurrences of c1 in this string with c2

2 String replaceAll(String regex, String replacement)

It replaces each substring of the string that matches the specified regular expression with the given replacement.

3 String replaceFirst(String regex, String replacement)

It replaces the first substring of this string that matches the specified regular expression with the given replacement.

4 String[] split(String regex)

Splits the string around matches of the specified regular expression. returns a string array

5. String[] split(String regex, int limit)

Splits a string around matches of the specified regular expression. Returns a string array.

6 String trim()

It returns a copy of the string, with leading and trailing whitespace removed.

 

4. Case Modifier Methods

Similar to the Modifier methods, the Case Modifier methods also modifies the value of a string. Unlike the modifiers however, case modifier specifically modifies the case of the string.

1 String toLowerCase()

Converts all of the characters in a String to lower case applying the rules of the default locale.

2 String toLowerCase(Locale locale)

Converts all of the characters in this String to lower case applying the rules of the specified Locale.

3 String toUpperCase()

Converts all of the characters in this String to upper case applying the rules of the default locale.

4. String toUpperCase(Locale locale)

Converts all of the characters in this String to upper case applying the rules of the specified Locale.

 

 

5. Substring Methods

You use  these methods to find either find substrings within given string

1 String substring(int beginIndex)

It finds a substring from a string starting from the given index to the end of the string. Then returns this string

2 String substring(int beginIndex, int endIndex)

It finds a substring from a string starting from the given index, beginIndex to the specified endIndex. Then returns this string


6. Bytes and Char Methods

These methods are use to retrieve char and byte related information.

1 byte getBytes()

It creates a sequence of bytes from a string by  using the platform’s default charset, and stores the result into a new byte array. Then returns the new byte array.

2 byte[] getBytes(String charsetName)

It creates a sequence of bytes from a string using the named charset, storing the result into a new byte array. Then returns it.

3 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

Copies characters from this string into the destination character array.

4. char[] toCharArray()

It converts the string to a character array.

5 char charAt(int index)

It returns the character at the given index.

 

 

7. Matching Methods

These methods are used to match the string against regular expressions or other texts.

1 boolean matches(String regex)

Tells if  a string matches the given regular expression. Returns true if it matches.Otherwise, returns false.

2 boolean regionMatches(ignoreCase, toffset, String other, int ooffset, int len)

Checks whether two string regions are equal.  Watch the video for the demo

3 boolean regionMatches(int toffset, String other, int ooffset, int len)

Tests whether two string regions are equal. Watch the video for the demo

 

 

8. Misc Methods

We also have other methods that does not fall into any of the categories. Hence they are miscellaneous methods. Find list of them below.

1 static String valueOf(primitive data type x)

This method returns the string representation of the specified data type argument.

2 int hashCode()

Returns a hash code for the string as integer

3. String intern()

Returns a canonical representation for the string a particular string.

4 int length()

Returns the length of this string. That is number of characters in the string

7 String toString()

Converts a string to a string. So basically returns the same string!

7 static String copyValueOf(char[] data)

It returns a String that represents the character sequence in the given character array.

8 static String copyValueOf(char[] data, int offset, int count)

It returns a String that represents the character sequence in the given character array starting from offset to the speficied count.

5 String concat(String str)

Concatenates the specified string to the end of a string. Combines the two strings together