• 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-6729d0aadbaa7525009942/] It returns a new String which is substring of given String from specified startIndex(Inclusive) [crayon-6729d0aadbaaf701313895/] 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-6729d0aadbbf6762678078/] String contains Example: [crayon-6729d0aadbbfb528648113/] When you run above program, you will get below output: [crayon-6729d0aadbbfc106211451/] 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-6729d0aadbf13662078369/] When you run above program, you will get below output [crayon-6729d0aadbf18206251858/] 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-6729d0aadc013568315745/] When you run above program, you will get below output: [crayon-6729d0aadc018817496182/] 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-6729d0aadc24c826392076/] When you run above program, you will get following output: [crayon-6729d0aadc251613197635/] Above […]