java string

We can convert Java Long to String using the following methods: String.valueOf() method Long.toString() method Convert Java Long to String 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 long. Example class Test { public static void main(String[] args) {…

Read More Convert Java Long to String

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 iterate over the elements (chars) of the String, and…

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 str = “1849231”; try { Integer.parseInt(str); Float.parseFloat(str); Double.parseDouble(str); Long.parseLong(str); System.out.println(“No error occurred. String is numeric!”); } catch (NumberFormatException e) { System.out.println(“String is not numeric!”); } } } Output: No error occurred. String is numeric!   And if we try to parse a String that…

Read More Check if String is Numeric 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

public class Test { public static void main(String[] args) { String stringToReverse = “Hello World of Java!”; StringBuilder stringBuilder = new StringBuilder(stringToReverse); String result = stringBuilder.reverse().toString(); System.out.println(result); } } Output: !avaJ fo dlroW olleH   We can do the same using the StringBuffer class since it also has the reverse() method. Reverse a String using the…

Read More Reverse a String in Java

public class Test { public static void main(String[] args) { String str = “Learn Java with alegrucoding.com”; // extract the substring starting from index 6 String substring = str.substring(6); // extract the substring from index 0 (inclusive) to 10 (exclusive) String substring2 = str.substring(0, 10); System.out.println(substring); System.out.println(substring2); } } Output: Java with alegrucoding.com Learn Java…

Read More Get Substring from String in Java