Author: Arpit Mandliya
- 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-67823f075f8eb859001902/] There are two overloaded variants of String’s join method: [crayon-67823f075f8f0132411661/] String join example: [crayon-67823f075f8f1610410051/] 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-67823f075f99c141058343/] 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 […]
- 07 May
Java String indexOf example
String’s indexOf method is used to find out index of any character or sub string. indexOf method can take char or string as argument and also you can provide fromIndex(Index after which you want to use indexOf method) also. Methods: public int indexOf(int ch) returns index of first occurrence of character within String public int […]
- 06 May
Java String substring example
String’s substring method is used to fetch part of a String.It returns a new String which is substring of given String. substring is nothing but subset of given String. There are two overloaded methods for substring. Method Signature: [crayon-67823f075fb75486679137/] It returns a new String which is substring of given String from specified startIndex(Inclusive) [crayon-67823f075fb78168299116/] It […]
- 06 May
Java String contains example
String’s contains method checks if sequence of characters can be found in String. It returns true if character sequence is present in String else return false. Method Signature: [crayon-67823f075fc15233622014/] String contains Example: [crayon-67823f075fc18647171552/] When you run above program, you will get below output: [crayon-67823f075fc19494044291/] Please note that contains method is case sensitive as you can […]
- 06 May
Java String concatenate example
String concatenate is a way to join multiple Strings to create large String. There are many places where we use to do String concatenate. For example: When you override toString() method of object, you concatenate multiple attributes to create a big String that represent that object. 3 ways to concatenate String Using + operator Using String’s […]
- 06 May
How to split a String in java
In this post, we will see how to split a String by delimiter in java. Sometimes, we need to split a String by delimiter for example: while reading a csv file, we need to split string by comma(,). We will use String class’s split method to split a String. This split(regex)Â takes a regex as […]