java string

We can convert float to String in Java using the following methods: String.valueOf() method Float.toString() method Convert float to String in Java using the String.valueOf() method There are multiple overloaded versions of the valueOf() method from the String class. We will use the one which accepts float. Example class Test { public static void main(String[] args)…

Read More Convert float to String in Java

Converting a Java String to a Double is a common task in Java programming. Whether you need to perform mathematical calculations or simply display numeric values, it’s important to know how to convert Strings to Doubles accurately and efficiently. In this tutorial, I will guide you through the process of converting a Java String to…

Read More Java String to a Double Conversion: Tips and Best Practices

Need to remove a character from String in Java?  You can do that by converting the String to a StringBuilder class and using the deleteCharAt() method that removes the character at a specified position. Example class Test { public static void main(String[] args) { String str = “Learn Java”; str = new StringBuilder(str).deleteCharAt(5).toString(); System.out.println(str); } }…

Read More Remove a Character from String in Java

Want to learn how to count character occurrences in Java String? Below is the program that uses a Map to store chars as keys and the number of occurrences as values. class Test { public static void main(String[] args) { String str = “moiujjmhrqdoasdsooqavvae”; Map<Character, Integer> map = new HashMap<>(); for (int i = 0;…

Read More Count Character occurrences in Java String

In this post, you will learn to check if a String contains only digits in Java. We will cover two methods: Using the Character.isDigit() method Using the Integer.parseInt() method Check if a String contains only digits in Java using the Character.isDigit() method In this example, we use a for loop to iterate over the elements…

Read More Check if a String contains only digits in Java

public class Test { public static void main(String[] args) { String str = “racecar”; System.out.println(str.equals(new StringBuilder(str).reverse().toString())); } } Output: true   We can do the same with the StringBuffer class since it also has the reverse() method. Using the for loop public class Test { public static void main(String[] args) { String str = “racecar”;…

Read More Check if String is Palindrome in Java

public class Test { public static void main(String[] args) { String str1 = “Learn Java Programming with alegrucoding.com”; String str2 = “Learn Java”; String str3 = “Simple Java Programming”; // check if str1 contains str2 if (str1.contains(str2)) { System.out.println(“str2 is present in str1”); } else { System.out.println(“str2 is not present in str1”); } // check…

Read More Check if a String Contains a Substring in Java

Output: Java   Methods from the StringUtils class that we can use to get a substring from String in Java: import org.apache.commons.lang3.StringUtils; public class Test { public static void main(String[] args) { String str = “Hello, my name is Tom, and I live in USA”; String substringBefore = StringUtils.substringBefore(str, “,”); String substringAfter = StringUtils.substringAfter(str, “,”); String substringBetween = StringUtils.substringBetween(str,…

Read More Get Substring from String in Java