string method example in java

Example 1: string method example in java

public char charAt(int index)
-----------------------------
String x = "airplane";
System.out.println( x.charAt(2) ); // output is 'r'

Example 2: string method example in java

public boolean equalsIgnoreCase(String s)
------------------------------------------
String x = "Exit"; 
System.out.println( x.equalsIgnoreCase("EXIT")); // is "true" 
System.out.println( x.equalsIgnoreCase("tixe")); // is "false"

Example 3: string method example in java

// all String methods
public class StringMethodsDemo {
	public static void main(String[] args) {
		String targetString = "Java is fun to learn";
		String s1= "JAVA";
		String s2= "Java";
		String s3 = "  Hello Java  ";
		
		System.out.println("Char at index 2(third position): " + targetString.charAt(2));
		System.out.println("After Concat: "+ targetString.concat("-Enjoy-"));
		System.out.println("Checking equals ignoring case: " +s2.equalsIgnoreCase(s1));
		System.out.println("Checking equals with case: " +s2.equals(s1));
		System.out.println("Checking Length: "+ targetString.length());
		System.out.println("Replace function: "+ targetString.replace("fun", "easy"));
		System.out.println("SubString of targetString: "+ targetString.substring(8));
		System.out.println("SubString of targetString: "+ targetString.substring(8, 12));
		System.out.println("Converting to lower case: "+ targetString.toLowerCase());
		System.out.println("Converting to upper case: "+ targetString.toUpperCase());
		System.out.println("Triming string: " + s3.trim());
		System.out.println("searching s1 in targetString: " + targetString.contains(s1));
		System.out.println("searching s2 in targetString: " + targetString.contains(s2));

		char [] charArray = s2.toCharArray();
		System.out.println("Size of char array: " + charArray.length);
		System.out.println("Printing last element of array: " + charArray[3]);

	}

}

Example 4: string method example in java

public boolean contains(“searchString”)

String x =Java is programming language”;
System.out.println(x.contains(Amit)); // This will print false
System.out.println(x.contains(Java)); // This will print true

Example 5: string method example in java

public String substring(int begin)/ public String substring(int begin, int end)
------------------------------------------------------------------------------
String x = "0123456789"; // the value of each char is the same as its index!
System.out.println( x.substring(5) ); // output is "56789"
System.out.println( x.substring(5, 8)); // output is "567"

Example 6: string method example in java

public int length()
-----------------------
String x = "01234567";
System.out.println( x.length() ); // returns "8"

Example 7: string method example in java

public String concat(String s)

String x = "book";
System.out.println( x.concat(" author") ); // output is "book author"

Example 8: string method example in java

public String trim()

String x = " hi ";
System.out.println( x + "x" ); // result is" hi x"
System.out.println(x.trim() + "x"); // result is "hix"

Tags:

Java Example