• 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-6743fd0d91f6a088306921/] 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-6743fd0d9365f081246673/] 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-6743fd0d93832513595064/] String isEmpty Example: [crayon-6743fd0d93838623323538/] When you run above program, you will get below output: [crayon-6743fd0d9383a176282974/]

  • 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-6743fd0d93964905272390/] String length Example: [crayon-6743fd0d9396a018184427/] When you run above program, you will get below output: [crayon-6743fd0d9396b043713530/]

  • 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-6743fd0d93d21495698886/] There are two overloaded variants of String’s join method: [crayon-6743fd0d93d27871840071/] String join example: [crayon-6743fd0d93d28844482757/] 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-6743fd0d93e34460790030/] 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 […]