Working With Strings in Java – Part 1

The java.lang.String class provides many useful methods for dealing with a sequence of char values. In this tutorial, you will see examples of using some of the methods. 

Also, this tutorial is part 1 of the series. Read part 2 to learn about other methods that the String class provides.

char charAt(int index)

Returns the character at the specified index.

class Test {
    
  public static void main(String[] args) {
    String str = "alegru";
    char c = str.charAt(2);
    
    System.out.println(c);
  }
}
Output: e

int indexOf(int ch)

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

class Test {
    
  public static void main(String[] args) {
    String str = "alegru";
    int index = str.indexOf('l');
        
    System.out.println(index);
  }
}
Output: 1

int indexOf(int ch, int fromIndex)

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

class Test {
    
  public static void main(String[] args) {
    String str = "alegrutechblog";
    int index = str.indexOf('l', 5); // search starts from char at index 5
        
    System.out.println(index);
  }
}
Output: 11

int indexOf(String str)

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

class Test {
    
  public static void main(String[] args) {
    String str = "alegrutechblog";
    int index = str.indexOf("tech");
        
    System.out.println(index);
  }
}
Output: 6

int indexOf(String str, int fromIndex)

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

class Test {
    
  public static void main(String[] args) {
    String str = "alegrutechblogtechblog";
    int index = str.indexOf("tech", 10); // search starts from char at index 10
        
    System.out.println(index);
  }
}
Output: 14

int lastIndexOf(int ch)

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

class Test {
    
  public static void main(String[] args) {
    String str = "alegrutechblogtechblog";
    int index = str.lastIndexOf('e');
        
    System.out.println(index);
  }
}
Output: 15
 

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.
class Test {
    
  public static void main(String[] args) {
    String str = "alegrutechblogtechblog";
    int index = str.lastIndexOf('e', 12); // search backward from char at index 12
      
    System.out.println(index);
  }
}
Output: 7
 

int lastIndexOf(String str)

Returns the index within this string of the rightmost occurrence of the specified substring.
class Test {
    
  public static void main(String[] args) {
    String str = "alegrutechblogtechblog";
    int index = str.lastIndexOf("tech");
     
    System.out.println(index);
  }
}
Output: 14
 

int lastIndexOf(String str, int fromIndex)

Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index.
class Test {
    
  public static void main(String[] args) {
    String str = "alegrutechblogtechblog";
    int index = str.lastIndexOf("blog", 15); // search backward from char at index 15
        
    System.out.println(index);
  }
}
Output: 10
 

substring(int beginIndex)

Returns a new string that is a substring of this string.
class Test {
    
  public static void main(String[] args) {
    String str = "alegrutechblogtechblog";
    String subString = str.substring(10);
        
    System.out.println(subString);
  }
}
Output: blogtechblog
 

substring(int beginIndex, int endIndex)

Returns a new string that is a substring of this string.
class Test {
    
  public static void main(String[] args) {
    String str = "alegrutechblogtechblog";
    String subString = str.substring(10, 15);
       
    System.out.println(subString);
  }
}
Output: blog
 

char[] toCharArray()

Converts this string to a new character array.
import java.util.Arrays;
class Test {
    
  public static void main(String[] args) {
    String str = "alegru";
    char[] charArray = str.toCharArray();
        
    System.out.println(Arrays.toString(charArray));
  }
}
Output: [a, l, e, g, r, u]

toString()

This object (which is already a string!) is itself returned.
class Test {
    
  public static void main(String[] args) {
    String str = "alegru";
    System.out.println(str.toString()); // this is redundant
  }
}
Output: alegru
 

trim()

Returns a copy of the string, with leading and trailing whitespace omitted.
class Test {
    
  public static void main(String[] args) {
    String str = "  alegru  ";
    String trimmedStr = str.trim();
      
    System.out.println(trimmedStr);
  }
}
Output: alegru
 

concat(String str)

Returns a copy of the string, with leading and trailing whitespace omitted.
class Test {
    
  public static void main(String[] args) {
    String str = "alegru";
    String newString = str.concat("techblog");
      
    System.out.println(newString);
  }
}
Output: alegrutechblog
 
This was part 1 of working with the String class in Java.

Leave a Reply

Your email address will not be published. Required fields are marked *