• 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-6623dba25006f798127146/] 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-6623dba2514dc339094737/] 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-6623dba251be6036139773/] String isEmpty Example: [crayon-6623dba251bed620180188/] When you run above program, you will get below output: [crayon-6623dba251bef640366827/]

  • 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-6623dba25222e113076913/] String length Example: [crayon-6623dba252237318534769/] When you run above program, you will get below output: [crayon-6623dba252239547718695/]

  • 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-6623dba252fcd230625003/] There are two overloaded variants of String’s join method: [crayon-6623dba252fd4773022445/] String join example: [crayon-6623dba252fd5526238947/] 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-6623dba2535e9195340614/] 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 […]