• 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: [crayon-67510e9f4f459429184758/] When you run above program, you […]

  • 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: Using String […]

  • 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: [crayon-67510e9f4f9a8311641849/] Parameters: ignoreCase: It is boolean field to indicate whether to ignore case or […]

  • 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: [crayon-67510e9f4fa74159179481/] String isEmpty Example: [crayon-67510e9f4fa77279664260/] When you run above program, you will get below output: [crayon-67510e9f4fa78776486827/]

  • 09 May

    Java String length example

    String’s length method is used to calculate length of String. It returns number of character in the String. Method Signature: [crayon-67510e9f4fb09986191572/] String length Example: [crayon-67510e9f4fb0b367420834/] When you run above program, you will get below output: [crayon-67510e9f4fb0c661184717/]

  • 08 May

    Java Program to find duplicate Characters in a String

    In this post, we will see how to find duplicate Characters in a String. Approach: Create a HashMap and character of String will be inserted as key and its count as value. If Hashamap already contains char,increase its count by 1, else put char in HashMap If value of Char is more than 1, that means it […]

  • Java String join
    08 May

    Java String join example

    String’s join method can be used to join various String by the delimiter. It is introduced in java 8. For example: We need to join various Strings by “,”. You can use join method as [crayon-67510e9f4fd93097601728/] There are two overloaded variants of String’s join method: [crayon-67510e9f4fd96852156435/] String join example: [crayon-67510e9f4fd98335137156/] When you run above program, […]

  • 07 May

    Why String is immutable in java

    String class is immutable in java. If you take dictionary meaning of immutable, it means unable to be changed or unchanging over time, so String is unchangeable or unmodifiable in java. Let’s understand with example. [crayon-67510e9f4fe42881927708/] As you can see, value of str1 did not change. It created another String object with value “Hellojava2blog” but […]

  • 07 May

    Java String lastIndexOf example

    String’s lastIndexOf method is used to find out last index of any character or sub string. lastIndexOf method can take char or string as argument and also you can provide fromIndex also. Methods: public int lastIndexOf(int ch) returns index of last occurrence of character within String public int lastIndexOf(int ch,int fromIndex) returns index of last occurrence […]