• 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-66065dfb90c12815540225/] It returns a new String which is substring of given String from specified startIndex(Inclusive) [crayon-66065dfb90c1a877018267/] 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-66065dfb92bc2340016576/] String contains Example: [crayon-66065dfb92bcc883008105/] When you run above program, you will get below output: [crayon-66065dfb92bce002159589/] 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 […]

  • 04 May

    How to convert Char Array to String in java

    There are multiple ways to convert Char Array to String in java. Some of them are: Using String class’s constructor (By passing Char array to contructor) Using String class’s valueOf method Using iterating char array Example: [crayon-66065dfb969bf989852407/] When you run above program, you will get below output [crayon-66065dfb969c9115082820/] Other String Programs are : How to […]

  • 04 May

    How to convert String to Char Array in java

    Java String’s toCharArray() method can be used to convert String to char Array in java. It is simplest method to convert String to char Array. Example: [crayon-66065dfb96b5a633957314/] When you run above program, you will get below output: [crayon-66065dfb96b60182989712/] Other String Programs are : String : How to reverse String in java How to check if […]

  • 06 August

    Find first non repeated character in a String

    One of the interview question is “How will you find first non repeating character in String.” For example: If input string is “analogy”,  then program should return ‘n’ If input string is “easiest”, then program should return ‘a’ First approach: We will use [LinkedHashMap](https://java2blog.com/linkedhashmap-in-java-with-example/ “LinkedHashMap”) to find first non repeating character in String. Algorithm: Get […]

  • 03 August

    Find all substrings of a String in java

    In this post, we will see java program to find all substrings of a String. For example: If input is “abb”  then output should be “a”, “b”,”b”, “ab”, “bb”, “abb” We will use String class’s subString method to find all subString Program: [crayon-66065dfb99809177640116/] When you run above program, you will get following output: [crayon-66065dfb99810447119827/] Above […]