Find the longest word in a string?

前端 未结 8 2151
旧巷少年郎
旧巷少年郎 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:39

    Try

    function longestWord(string) {
        var str = string.split(" ");
        var longest = 0;
        var word = null;
        for (var i = 0; i < str.length; i++) {
            if (longest < str[i].length) {
                longest = str[i].length;
                word = str[i];
            }
        }
        return word;
    }
    
    0 讨论(0)
  • 2021-01-17 05:44

    Here is another shorter version:

    function findTheLongestWord(str) {
      var temp = '';
      (str || "").split(/\s+/).forEach(function(word) {
        temp = word.length > temp.length && word || temp;
      });
      return "longest word is: " + temp + " and it's length is: " + temp.length;
    }
    
    alert (findTheLongestWord("The quick brown fox jumped over the lazy dog, once again!"));

    0 讨论(0)
  • 2021-01-17 05:46
    function findLongestWord(str) {
      var arr=str.split(" ");
      var sarr=arr.sort(function(a,b){return b.length-a.length;});
      str=sarr[0];
      return str.length;
    }
    

    using the javascript Array.prototype.sort() we can slove this problem elegantly without using the loop

    0 讨论(0)
  • 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
    
        }
    
    0 讨论(0)
  • 2021-01-17 06:00

    You can greatly simplify this using Array.prototype.sort and Array.prototype.shift. For example

    var str = 'The quick brown fox jumped over the lazy dog',
        longest = str.split(' ').sort(function(a, b) {
                      return b.length - a.length;
                  }).shift();
    
    document.getElementById('word').innerHTML = longest;
    document.getElementById('length').innerHTML = longest.length;
    <p id="word"></p>
    <p id="length"></p>


    The sort() will produce an array of strings sorted by their length (longest to shortest) and shift() grabs the first element. You could also sort the other way (shortest to longest) using return a.length - b.length and pop() instead of shift() to grab the last element.

    0 讨论(0)
  • 2021-01-17 06:03

    Your statement

    words.push(str.split(" "));
    

    should be

    words=str.split(" ")
    

    And @Tushar solved the rest of your "problems" too ... ;-)

    0 讨论(0)
提交回复
热议问题