Find the longest word in a string?

前端 未结 8 2282
旧巷少年郎
旧巷少年郎 2021-01-17 05:34

I am trying to write a basic javascript function to find the longest word in a string and log it\'s length.

So far I have:

function findLongestWord(         


        
8条回答
  •  死守一世寂寞
    2021-01-17 05:57

    To find the longest keyword string in Java you need to write a program.

    public class FindLarge {
    
        private String longestWord;
    
        public String longestWord(String sen) {
    
            String arr[] = sen.split(" ");      // seperates each word in the string and stores it in array
    
            longestWord = arr[0];               // Assume first word to be the largest word
    
            for (String a : arr)
                if (longestWord.length() < a.length())      // check length of each word
                    longestWord = a;
    
            return longestWord;
        }
    
        public static void main(String[] args) {
    
            FindLarge fl=new FindLarge();
    
            String longestWord=fl.longestWord("Hello Welcome to Java");   // string to be checked
    
            System.out.println("Longest Word: "+ longestWord);          // Final Output
    
        }
    

提交回复
热议问题