Tag Archives: Java-Strings

Searching characters and substring in a String in Java

Searching a character in the String indexOf(char c) : It searches the index of specified character within a given string. It starts searching from beginning to the end of the string (from left to right) and returns the corresponding index if found otherwise returns -1. Note: If given string contains multiple occurrence of specified character… Read More »

Split() String method in Java with examples

The string split() method breaks a given string around matches of the given regular expression. For Example: Input String: 016-78967 Regular Expression: – Output : {“016″,”78967”} Following are the two variants of split() method in Java: 1. Public String [ ] split ( String regex, int limit ) Parameters: regex – a delimiting regular expression… Read More »

String to Integer in Java – parseInt()

While operating upon strings, there are times when we need to convert a number represented as a string into an integer type. The method generally used to convert String to Integer in Java is parseInt(). How to use parseInt() method in Java? There are two variants of this method: public static int parseInt(String s) throws… Read More »

How to Initialize and Compare Strings in Java?

Initializing Strings in Java 1. Direct Initialization(String Constant) : In this method, a String constant object will be created in String pooled area which is inside heap area in memory. As it is a constant, we can’t modify it,i.e. String class is immutable. Examples: String str = “GeeksForGeeks”; str = “geeks”; // This statement will… Read More »

String vs StringBuilder vs StringBuffer in Java

Consider below code with three concatenation functions with three different types of parameters, String, StringBuffer and StringBuilder. Output: String: Geeks StringBuilder: Geeksforgeeks StringBuffer: Geeksforgeeks Explanation: 1. Concat1 : In this method, we pass a string “Geeks” and perform “s1 = s1 + ”forgeeks”. The string passed from main() is not changed, this is due to… Read More »