String
- 21 October
How to convert Byte Array to String in java
Sometimes, we may need to convert String to bytes Array but how to convert bytes array to String back. toString method does not work correctly here as it just print bytes in String format. You can use String’s constructor to get String back. Example: [crayon-6750a594e89da238762488/] When you run above program, you will get below output: […]
- 15 September
Longest Common Prefix in an array of Strings in java
If you want to practice data structure and algorithm programs, you can go through data structure and algorithm interview questions. In this post, we are going to see longest common prefix in array of Strings. So lets say you have string array as below: [crayon-6750a594e8c4b674130137/] So Longest common prefix in above String array will be “java” […]
- 15 July
Java String Interview questions and answers
In this post, we will see interview questions on java String. String is most important data type which we use in our programs very often. 1. Why String is declared final or immutable in java? There are various reasons to make String immutable. String pool Thread Safe Security Class Loading Cache hash value You can […]
- 15 July
Find all Permutations of a String in java
In this post, we will see how to find all permutations of String in java. We will use a very simple approach to do it. Take out first character of String and insert into different places of permutations of remaining String recursively. Lets say you have String as ABC. So we take out A from […]
- 13 July
Difference between StringBuffer and StringBuilder in java
In this post, we will see difference between StringBuffer and StringBuilder in java StringBuffer vs StringBuilder Parameter StringBuffer StringBuilder Thread-safe StringBuffer is thread safe. Two threads can not call methods of StringBuffer simultaneously. StringBuilder is not thread safe, so two threads can call methods of StringBuilder simultaneously. Performance It is less performance efficient as it […]
- 03 June
How to remove non-ascii characters from a string in java
In this post, we will see how to remove non ascii character from a string in java. Sometimes, you get non-ascii characters in String and you need to remove them. We will use regular expressions to do it. Java program to remove non-ascii characters: [crayon-6750a594e909a650020100/] When you run above program, you will get below output: […]
- 03 June
How to check if String has all unique characters in java
Learn about how to check if String has all unique characters in java
- 24 May
Java String compareTo example
String’s compareTo method compares two String lexicographically. Both String get converted to unicode value and then compares. If you call str1.compareTo(str2) then if it returns positive number : str1 is greater than str2 0: str1 is equal to str2 negative number : str1 is smaller than str2 Java String compareTo example [crayon-6750a594e9333342906791/] When you run […]
- 24 May
How to convert String to Byte array in java
Sometimes, we need to convert String to byte array. We can use getBytes() method of String to do it, Method syntax: [crayon-6750a594e93db030872557/] String getBytes Example: [crayon-6750a594e93de937110987/] When you run above program, you will get below output: [crayon-6750a594e93df436057240/] There is one more overloaded method for getBytes Method syntax: [crayon-6750a594e93e1351792336/] It encodes String to specified charset format. […]