Java String Programs - Programming Examples for Interviews (2021)


A quick guide to java string based interview programming questions and examples.

1. Overview

In this article, We will see what are the String programs frequently asked in the java interviews.
All may be asked in the face to face or telephonic technical rounds. Every java programmer must know all of these questions.

Some of these will be tricky but easy if you understand clearly.

Java String Programs - Programming Examples for Interviews

2. Java String Programs

Next, Look at the java string based programs with example codes.

2.1 How to split the string with a delimiter

public class StringSplitExample1 {

    public static void main(String[] args) {
        String str = "java@program@to.com";

        String[] splitArray = str.split("@");

        for(String value : splitArray){

2.2 How to get codepoints for a String?

String str = "Code Points as Stream";
System.out.println("Input string value : "+str);

IntStream intStream = str.codePoints();
System.out.println("Printing each char from string as ASCII value");

intStream.forEach(value -> System.out.print(value+" "));

Input string value : Code Points as Stream
Printing each char from string as ASCII value
67 111 100 101 32 80 111 105 110 116 115 32 97 115 32 83 116 114 101 97 109 

2.3 How to remove the Zero's from String?

String str = "Digit ZERO 0 is not considered in input name. So removing all Zero's 00000000";
IntStream intStream = str.codePoints();

String zeroRemovedString = intStream.filter(ch -> ch != 48)
         .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)

Digit ZERO  is not considered in input name. So removing all Zero's

2.4 How to check the string is palindrome or not?

public class StringPalindromeAppend {

    public static void main(String[] args) {
        String input1 = "civic";
        StringBuffer buffer = new StringBuffer();

        for (int i = input1.length() - 1; i >= 0; i--) {
        String reversedString1 = buffer.toString();

        if (input1.equals(reversedString1)) {
            System.out.println(input1 + " is a palindrome");
        } else {
            System.out.println(input1 + " is not a palindrome");


2.5 How to check the String Palindrome recursively ?

public static boolean isPalindrome(String s) {

	// if the string has one or zero characters then recursive call is stopped.
	if (s.length() == 0 || s.length() == 1)
		return true;

	// checking the first and last character of the string. if equals then call the
	// same function with substring from index 1 to length -1. Because substring
	// excludes the endIndex.
	// if these two values are not same then string is not Palindrome so this
	// returns false.
	if (s.charAt(0) == s.charAt(s.length() - 1))
		return isPalindrome(s.substring(1, s.length() - 1));

	// this statment is executed if and if only first and last character of string
	// at any time is not equal.
	return false;

2.6 How to count vowels and consonants for String ?

String input = "This is using Collectors api methods !!!!";

List<Character> vowels = new ArrayList<>(Arrays.asList('a', 'e', 'i', 'o', 'u'));
input = input.toLowerCase();

IntStream stream = input.chars();

Map<Boolean, Long> finalResultMap = stream.mapToObj(ch -> (char) ch).filter(ch -> (ch >= 'a' && ch <= 'z'))
        .collect(Collectors.partitioningBy(ch -> vowels.contains(ch), Collectors.counting()));

System.out.println("Total count of vowels : " + finalResultMap.get(new Boolean(true)));
System.out.println("Total count of consonants : " + finalResultMap.get(new Boolean(false)));


Total count of vowels : 11
Total count of consonants : 20

2.7 How to compare different String objects with != operator ?

String status = new String("Failure");

if (status.intern() != "Failure") {
	System.out.println("Valid age");
} else {
	System.out.println("Invalid age");

Use intern() method to get the original string from String constant pool for string contents comparision with != operator.

2.8 How to find the first non repeated character from String ?

public static String firstNonRepeatedCharacterJava8(String input) {

  Map chars = input.codePoints().mapToObj(cp -> cp)
    .collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()));

  int pos = chars.entrySet().stream().filter(e -> e.getValue() == 1L).findFirst().map(Map.Entry::getKey)

  return String.valueOf(Character.toChars(pos));

2.9 How to convert String to Date in java 8?

String isoDateInString = "May 30, 2020";

DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("MMM d, yyyy");

LocalDate date = LocalDate.parse(isoDateInString, customFormatter);

System.out.println("Locale Date : "+date); // 2020-05-30

2.10 How to convert String to Int ?

Conversion from String to integer can be done using following techniques.

Integer Constructor

2.11 How to check String contains only digits ?

public boolean checkStringOnlyDigitsIsDigit(String input) {

 IntStream intStream = input.chars();
 boolean isMatched = intStream.anyMatch(ch -> Character.isDigit(ch));

 return isMatched;


2.12 How to reverse the words in String?

 public String reverseWordsWithStringBuilder(String input) {

  // step 1: converting input string into stream.
  Stream-<String-> stream = pattern.splitAsStream(input);

  // step 2: reversing each word.
  Stream->StringBuilder-> intermeidateOutput = stream.map(word -> new StringBuilder(word).reverse());

  // step 3: merging all reversed words with empty space " "
  String reversedInput = intermeidateOutput.collect(Collectors.joining(" "));

  return reversedInput;

3. Conclusion

In this article, we have seen the most used java string programs with examples. All questions are already explained in different ways in the previous article.



About Us

Author: Venkatesh - I love to learn and share the technical stuff.

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,16,Arrays,16,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,5,Collections,28,Collector,1,Command Line,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,92,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,35,Dictionary,1,Difference,1,Download,1,Eclipse,2,Efficiently,1,Error,1,Errors,1,Exception,1,Exceptions,3,Fast,1,Files,10,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,4,Grant,1,Grep,1,HashMap,1,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,5,Iterate,2,Jackson API,3,Java,30,Java 10,1,Java 11,5,Java 12,5,Java 13,2,Java 14,2,Java 8,104,Java 8 Difference,2,Java 8 Stream Conversions,2,java 8 Stream Examples,3,Java 9,1,Java Conversions,11,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,104,java.lang,5,java.util. function,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,1,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,2,Math,1,Matrix,5,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,27,String,58,String Programs,12,String Revese,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,16,Unix,3,Updates,3,util,5,While Loop,1,
JavaProgramTo.com: Java String Programs - Programming Examples for Interviews (2021)
Java String Programs - Programming Examples for Interviews (2021)
A quick guide to java string based interview programming questions and examples.
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content