Java string methods with examples: There are different string methods in java we will use this methods for perform various operations on the string in java.Following are some Java String Methods with Examples.

Program: Get character from a string

In this program we use charAt() method to get or access the single character from a specified position.

class StringExample
 {
  public static void main(String[] args)
 {
    String str = "Hello! World";
    System.out.println("The string is: " + str);
    System.out.println("The character at 2: " + str.charAt(2));
    System.out.println("The character at 9: " + str.charAt(9));
  }
}

Output:

The string is: Hello! World
The character at 2: l
The character at 9: r

Find character in a string

Program: Get a substring from a string in java

class StringExample
 {
  public static void main(String[] args) 
{

    String str = new String("Hello! World");

    System.out.println("Using the subString(): " + str.substring(7));
}
}

Output:

Using the subString(): World

SubString

Program: Convert string in lower case or upper case & replace the character.

In this example we use toLowerCase() method for converting string in lower case characters and toUpperCase() method for converting the string in upper case characters. Also use replace() method for replacing the string character into other character.

class StringExample
 {
  public static void main(String[] args)
 {

    String str = new String("Hello! World");

    System.out.println("Using the toLowerCase(): " + str.toLowerCase());

    System.out.println("Using the toUpperCase(): " + str.toUpperCase());

    System.out.println("Using the replace(): " + str.replace('!', 'o'));
  }
}

Output:

Using the toLowerCase(): hello! world
Using the toUpperCase(): HELLO! WORLD
Using the replace(): Helloo World

convert String in Lower case,Upper case and replace characters

If you want to learn more string methods in java then please see this article Basic operations on String in Java- Strings Concatenation, Compare.

Leave a Reply

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

Back to Top