• 14 May

    Java String equals example

    String’s equals method compares String with specified object. It checks if both have same character sequence. It is generally used to compare two Strings. Method Signature: public boolean equals(Object anObject) String equals Example: package org.arpit.java2blog; public class StringEqualsExamplesMain { public static void main(String[] args) { String str1 = "Hello world from java2blog.com"; String str2 = […]

  • 14 May

    Java String contentEquals example

    String’s contentEquals method is used to compare String with StringBuffer. It returns boolean datatype. Method syntax: public boolean contentEquals(StringBuffer sb) String contentEquals Example: package org.arpit.java2blog; public class StringContentEqualsExample { public static void main(String[] args) { String str1 = "java2blog"; String str2="Hello world from java2blog"; String str3="JAVA2BLOG"; StringBuffer sb=new StringBuffer("java2blog"); System.out.println(str1.contentEquals(sb)); System.out.println(str2.contentEquals(sb)); System.out.println(str3.contentEquals(sb)); } } When […]

  • 13 May

    Java String endsWith example

    String’s endWith method checks if String ends with specific suffix. It returns boolean datatype. Method syntax: public boolean endsWith(String suffix) String endsWith Example: package org.arpit.java2blog; public class StringEndsWithExample { public static void main(String[] args) { String str = "java2blog"; if (str.endsWith("blog")) { System.out.println("String ends with blog"); } else { System.out.println("String does not end with blog"); […]

  • 12 May

    Java String startsWith example

    String’s startsWith method checks if String starts with specific prefix. It returns boolean datatype. Method Signature: public boolean startsWith(String prefix) String startsWith Example: package org.arpit.java2blog; public class StringStartsWithExample { public static void main(String[] args) { String str = "java2blog"; if (str.startsWith("java")) { System.out.println("String starts with java"); } else { System.out.println("String does not start with java"); […]

  • 12 May

    Java String charAt example

    String’s charAt method returns char value present at specified index. String is nothing but Sequence of char values and it starts with index 0. Method Signature: public char charAt(int index) String charAt Example: package org.arpit.java2blog; public class StringCharAtMain { /* * @Author : Arpit Mandliya */ public static void main(String args[]) { String str="java2blog"; System.out.println("char […]

  • 11 May

    Java String trim example

    String’s trim method is used to remove leading and trailing space. Sometimes, you get extra leading or trailing space and your program does not work as expected. It returns String object after removing leading and trailing spaces. String’s trim method does not remove middle spaces. String trim example: package org.arpit.java2blog; public class StringTrimExample { public […]

  • 10 May

    How to check if two Strings are Anagrams in Java

    In this post, we will see how to check if two Strings are Anagrams in java. Anagrams mean if two Strings have the same characters but in a different order. For example: Angel and Angle are anagrams There are many ways to check if Strings are anagrams in java. Some of them are: [toc] Using […]

  • 10 May

    Java String regionMatches example

    String’s regionMatches is used to compare substring of one string to substring of another.Here region may be refer to substring of String, so we are comparing regions of one string with another. Method Syntax:  There are two overloaded version of regionMatches:  // Case sensitive public boolean regionMatches(int toffset, String other, int ooffset, int len): // […]

  • 10 May

    Java String isEmpty example

    String’s isEmpty method checks if length of string is 0 or not. It returns true if length of String is 0 else return false. Method Signature: public boolean isEmpty() String isEmpty Example: package org.arpit.java2blog; public class StringIsEmptyExample { /* * @Author : Arpit Mandliya */ public static void main(String[] args) { String str1=""; String str2="java2blog"; […]

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.