String
- 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-6729cd87d8e69854213768/] 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-6729cd87d92e6113398158/] 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-6729cd87d93c6233357442/] String isEmpty Example: [crayon-6729cd87d93c8718628722/] When you run above program, you will get below output: [crayon-6729cd87d93c9159017832/]
- 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-6729cd87d945d617899615/] String length Example: [crayon-6729cd87d9460448129048/] When you run above program, you will get below output: [crayon-6729cd87d9461051692259/]
- 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 […]
- 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-6729cd87d9651958925771/] There are two overloaded variants of String’s join method: [crayon-6729cd87d9654385444369/] String join example: [crayon-6729cd87d9655203105188/] 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-6729cd87d96ef232643568/] 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 […]