Please disable adblock to view this page.

← Go home

String Operations in Java

java-display

April 30, 2017
Published By : Pratik Kataria
Categorised in:

Understanding through Code

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

		String str1 = "Hello this is AI talking.";
		System.out.println("Length of string: " +str1.length());

		System.out.println("Position (Index) of substring: " +str1.indexOf("AI"));

		String substr = str1.substring(14); //for verification
		System.out.println(substr);

		String str2 = "Aloha          "; //string with spaces
		System.out.println("Length of string with spaces: " +str2.length());

		String str3 = str2.trim();  //removes whitespace

		System.out.println(str3.length());

	}
}

Output

  • Length of string: 25
  • Position (Index) of substring: 14
  • AI talking.
  • Length of string with spaces: 15
  • 5

 

Pratik Kataria is a budding programmer, web designer and developer.