String Programs in Java

In Java, a String is an object that represents a sequence of characters. It is a built-in class in the Java programming language and is used to store and manipulate textual data.

A String is a sequence of characters enclosed within double quotes, like "Hello World". In Java, Strings are immutable, meaning that once created, their values cannot be changed. However, new Strings can be created by combining or modifying existing ones.

String objects can be created using the String class's constructors, such as:

String str = "Hello World"; // Using string literal String strObj = new String("Hello World"); // Using constructor

In summary, a String is a fundamental data type in Java used to represent textual data. It is an object, and Java provides many built-in methods to manipulate Strings in various ways.

Table of Content :

Java String Programs

Filter out String list with length> 5

package com.javacodestuffs.core.java8.streams.filter; import java.util.Arrays; import java.util.List; import java.util.stream.Stream; public class StreamFilterNames { public static void main(String[] args) { System.out.println("Names with length greater than 5 : \n"); // String List<String> names = Arrays.asList( "Sachin", "Rahul", "Sehwag", "Anil", "Sourav", "Sunil", "Laxman" ); // filter names with length greater than 5 Stream<String> stream = names.stream().filter(name -> name.length() > 5); // print to console using forEach stream.forEach(str -> System.out.println(str)); } }

Output

Names with length greater than 5 : Sachin Sehwag Sourav Laxman

Java Prtogram to get distinct characters and their count in a String

import java.util.Scanner; public class CheckVowelCount { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a string: "); String str = sc.nextLine(); int count = 0; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') { count++; } } System.out.println("The number of vowels in " + str + " is " + count); } }


Java program to find Palindrome String

package com.javacodestuffs.core.java.strings; import java.util.Scanner; public class PalindromeString { public static void main(String[] args) { try (Scanner sc = new Scanner(System.in)) { System.out.print("Enter a string: "); String str = sc.nextLine(); String rev = ""; for (int i = str.length() - 1; i >= 0; i--) { rev += str.charAt(i); } if (str.equalsIgnoreCase(rev)) { System.out.println(str + " is a palindrome."); } else { System.out.println(str + " is not a palindrome."); } } } }

output:
Enter a string: aba aba is a palindrome.


Java Program to reverse the given String

package com.javacodestuffs.core.java.strings; import java.util.Scanner; public class ReverseString { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the string: "); String str = sc.nextLine(); String rev = ""; for (int i = str.length() - 1; i >= 0; i--) { rev += str.charAt(i); } System.out.println("The reverse of " + str + " is " + rev); } }

Output:

Enter the string: bala The reverse of bala is alab

Splitting a String into an array

String str = "Hello World"; String[] arr = str.split(" "); for(String s : arr) { System.out.println(s); }

Java Program to check if a string contains a particular substring

package com.javacodestuffs.core.java.strings; import java.util.Scanner; public class CheckSubString { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the string: "); String str = sc.nextLine(); System.out.print("Enter a substring to find: "); String sub = sc.nextLine(); if (str.contains(sub)) { System.out.println(str + " contains " + sub); } else { System.out.println(str + " does not contain " + sub); } } }

output:

Enter the string: Hellow World Enter a substring to find: Wor Hellow World contains Wor

Java program to count number of words in a String

package com.javacodestuffs.core.java.strings; public class WordCount { public static void main(String[] args) { String str = "hello world welcome hello my name is john my favourite subject is maths"; int count = 0; boolean word = false; // loop through each character in the string for (int i = 0; i < str.length(); i++) { if (Character.isLetter(str.charAt(i)) && i != str.length() - 1) { word = true; } else if (!Character.isLetter(str.charAt(i)) && word) { count++; word = false; } else if (Character.isLetter(str.charAt(i)) && i == str.length() - 1) { count++; } } System.out.println("Number of words in the string: " + count); } }

output:

Number of words in the string: 13

Java program to check if a String contains only digits

package com.javacodestuffs.core.java.strings; import java.util.Scanner; public class DigitCheckinString { public static void main(String[] args) { try (Scanner sc = new Scanner(System.in)) { System.out.println("Enter a string: "); String str = sc.nextLine(); boolean containsOnlyDigits = true; for (int i = 0; i < str.length(); i++) { if (!Character.isDigit(str.charAt(i))) { containsOnlyDigits = false; break; } } if (containsOnlyDigits) { System.out.println("The string contains only digits"); } else { System.out.println("The string does not contain only digits"); } } } }

output:

Enter a string: 1234256 The string contains only digits Enter a string: Hello world The string does not contain only digits

What is StringBuffer in Java

In Java, StringBuffer is a class that is used to create and manipulate mutable sequences of characters. The StringBuffer class is similar to the String class in that it stores sequences of characters, but it is different in that the StringBuffer class is mutable, meaning that you can modify the contents of the sequence after it has been created.

The StringBuffer class provides a number of methods that allow you to modify the contents of the sequence, such as append(), insert(), delete(), replace(), and many others. Here's an example that demonstrates the use of some of these methods.

StringBuffer sb = new StringBuffer("Hello"); System.out.println("Original String: " + sb); // append some text to the end of the string sb.append(" World"); System.out.println("After append(): " + sb); // insert some text at a specific position in the string sb.insert(5, " there"); System.out.println("After insert(): " + sb); // delete a portion of the string sb.delete(5, 10); System.out.println("After delete(): " + sb); // replace a portion of the string with new text sb.replace(5, 10, "Dear"); System.out.println("After replace(): " + sb); // reverse the order of the characters in the string sb.reverse(); System.out.println("After reverse(): " + sb);

Conclusion

In this article, We have seen what is a String with examples.