String Processing in Java

String class is an important and useful class to process text information in Java programs. These class provides a large number of methods that can make processing of string more efficient and understandable. The simplest way to create a string is:
  1. String myString = "This is string tutorial";
  1. Extract character and sub-strings from string. The numeration of the characters in the strings begins from 0 zero.For example, if you want to extract the seventh character from the string, you will do it in the following way:
    1. char charAtPosition7 = myString.charAt(6);
    In the case you need to extract a substring, you can use substring method. There are two variants of the substring method: The first one extracts string from the beginIndex to endIndex
    1. String substring(int beginIndex, int endIndex)
    Example:
    1. String string = myString.substring(8, 14);
    Another way is to extract a sub-string from the beginIndex to the end of the string:
    1. String substring(int beginIndex)
    Example:
    1. String endOfString = myString.substring(15);
    Now lets test these operations:
    1. System.out.println("Initial string " + myString);
    2. System.out.println("char at position 7 " + charAtPosition7);
    3. System.out.println("Substring from 9th char to 14th char " + string);
    4. System.out.println("End of string " + endOfString);
    The output of the program:
    1. Initial string This is string tutorial
    2. char at position 7 s
    3. Substring from 9th char to 14th char string
    4. End of string tutorial
  2. You can split a string using a regular expression. For this task split method exists. There are two variants of split method: The first returns an array of string that matches the regex:
    1. String[] split(String regex)
    The second has a limit for the capacity of the array:
    1. String[] split(String regex, int limit)
    For example we can extract all the words from the string that are divided by space:
    1. String[] sp = myString.split(" ");
    2. for(int i = 0; i != sp.length;++i)
    3. System.out.println(sp[i]);
    4. }
    The output:
    1. This
    2. is
    3. string
    4. tutorial
  3. Another useful method is trim. This method removes extra space characters from the begin and end of the string:
    1. String testTrim = " a lot of spaces here ";
    2. String edited = testTrim.trim();
    3. System.out.println("Initial string " + testTrim);
    4. System.out.println("Trimmed " + edited);
  4. A string can be easily converted to upper or lower case. For this task you can use the following methods:
    1. String toLowerCase()
    2. String toUpperCase()
  5. Another possibility provided by String class is searching for characters and sub-string in a string. To search for the first index of a character use:
    1. int indexOf(int ch)
    To get the last index of character:
    1. int lastIndexOf(int ch)
    Also, you can specify the starting position of the search by adding a parameter to these methods:
    1. int indexOf(int ch, int fromIndex)
    2. int lastIndexOf(int ch, int fromIndex)
    The same possibility exist for searching a sub-string in a string:
    1. int indexOf(String str)
    2. int lastIndexOf(String str)
    3. int indexOf(String str, int fromIndex)
    4. int lastIndexOf(String str, int fromIndex)
    All of these methods returns the index of the found character or sub-string or -1 if the character or sub-string doesn't occur. Also, you can check, if a string contains a specified char sequence:
    1. boolean contains(CharSequence s)
    You can use any string as the parameter to this function because CharSequence is the interface of the String class.
  6. Once a string is created, it can't be modified. So, if you need to edit a string,you will have to create a new string and use the following methods to modify the original string. Replace old char with new char:
    1. String replace(char oldChar, char newChar)
    Replace target sub-string with new sub-string:
    1. String replace(CharSequence target, CharSequence replacement)
    Replace all sub-string corresponding to the regular expression:
    1. String replaceAll(String regex, String replacement)
    There is the modification of this method that replaces only the first occurrence of the sub-string:
    1. String replaceFirst(String regex, String replacement)
Here are the basic operation that can be performed on the String objects. In the next tutorial you will find more complex examples to work with strings and use StringBuilder class.

Tags

Add new comment