Find the longest string in an array of strings using Java

Pre-requisite knowledge

A single alphabet or a single digit or a single symbol or a single space within a single quote is known as a character. It requires 2 bytes of memory because in java character is always represented in Unicode format.

Examples of characters:  ‘1’, ‘@’, ‘b’, ‘ ‘, etc.

A single or collection of characters written in the double quote is called a string.

Examples of strings: “”, “123”, “a”, “5”, “@&”, etc.

An array is a container containing multiple data of the same type within a single variable. It means all data can be of numbers type, string type, etc. In this tutorial, we are going to focus on arrays containing strings only.

How to declare and initialize an array of strings using the String class:
String st[]={"code","speedy","is","very","good","at","providing","content"};

Steps to follow : longest string in an array of strings

  • Create and initialize the array of strings and let named as st.
  • Create two integer type variables let l and len and assign the length of the first string from the string array in l and len as zero (len=0). Assume l contains the length of largest string from the string array
  • Use .length() to find the length of any string.
  • Create an empty string variable using String class to finally store the largest string let s and assume it contains the largest. string from the string array.
  • Iterate the string array and follow the below steps.
  • Use len variable to store the length of each string from the string array from every iteration,
    updates after each iteration.
  • If the length of any string(len) from the string array is greater than the length of our assumed largest
    string(l) then update the largest length(l) as the length of the current string(len) [eg. l=len] and update the largest string(s) as current string.
  • After completion of iteration s will contain the largest string. Use it or display it.
public class Main
  public static void main(String[] args) {
    String st[]={"code","speedy","is","very","good","at","providing","content"};   //array of strings named as st
    int l=st[0].length(),len=0;   //l stores the length of the first string of the string array st assuming first string as the largest string
    String s="";   //empty string is created to finally store the largest string from the string array
    for(int i=0;i<st.length;i++)     //use to iterate through string array
        len=st[i].length();      //len will store the length of of each string of string array, changes after each iteration
        if(len>l)     //if the length of any string from the string array is greater than the lrngth of our assumed largest string
            l=len;    //update the largest length as length of current string
            s=st[i];    //update the largest string as current string 
    System.out.print(s);     //after complete iteration s will contain the largest string from the string array



Note: We are considering there is only one largest string present of that length. If more than one string is present of the same length as the length of the largest string then use to store them all in another string array or array list of string type and display it rather than using a single string variable to store the string like s in our example.

Leave a Reply

Your email address will not be published. Required fields are marked *