“Finden Sie das längste Wort in einer Zeichenfolge” Code-Antworten

Finden Sie das längste Wort in einer Zeichenfolge

// Find the Longest Word in a String

function findLongestWordLength(str) {
	let longest = '';
	let words = str.split(' ');
	for (let i = 0; i < words.length; i++) {
		if (longest.length < words[i].length) longest = words[i];
	}
	return longest.length;
}

findLongestWordLength('The quick brown fox jumped over the lazy dog');

// OR

function findLongestWordLength(s) {
	return s.split(' ').reduce(function (longest, word) {
		return Math.max(longest, word.length);
	}, 0);
}

// OR

// My favourite
function findLongestWordLength(str) {
	return Math.max(...str.split(' ').map((word) => word.length));
}

// OR

function findLongestWordLength(str) {
	// split the string into individual words
	const words = str.split(' ');

	// words only has 1 element left that is the longest element
	if (words.length == 1) {
		return words[0].length;
	}

	// if words has multiple elements, remove the first element
	// and recursively call the function
	return Math.max(words[0].length, findLongestWordLength(words.slice(1).join(' ')));
YosKa

So finden Sie die Länge eines Arrays in Java

class Main {
  public static void main(String[] args) {
    // Creating an array called x.
    String[] x = new String[]{"This", "Should", "return", "4"};
    // "x.length" finds the length of the array "x".
    System.out.println(x.length);
    // returns 4
  }
}
Enchanting Elephant

Finden Sie das längste Wort in einer Zeichenfolge

// Find the Longest Word in a String

function findLongestWordLength(str) {
	let longest = '';
	let words = str.split(' ');
	for (let i = 0; i < words.length; i++) {
		if (longest.length < words[i].length) longest = words[i];
	}
	return longest.length;
}

findLongestWordLength('The quick brown fox jumped over the lazy dog');

// OR

function findLongestWordLength(s) {
	return s.split(' ').reduce(function (longest, word) {
		return Math.max(longest, word.length);
	}, 0);
}

// OR

// My favourite
function findLongestWordLength(str) {
	return Math.max(...str.split(' ').map((word) => word.length));
}

// OR

function findLongestWordLength(str) {
	// split the string into individual words
	const words = str.split(' ');

	// words only has 1 element left that is the longest element
	if (words.length == 1) {
		return words[0].length;
	}

	// if words has multiple elements, remove the first element
	// and recursively call the function
	return Math.max(words[0].length, findLongestWordLength(words.slice(1).join(' ')));
}
YosKa

Finden Sie das längste Wort in einer Zeichenfolge

// https://dev.to/estheragbaje/three-ways-to-find-the-longest-word-in-a-string-using-javascript-5236

// For Loop
function findLongestWord(str) {
  const splStrArray = str.split(' ');

  //initialize a variable to store the longest word
  let longestWord = "";
  for(let index = 0; index < splStrArray.length; index++){
    if(splStrArray[index].length > longestWord.length){
         longestWord = splStrArray[index];
     }
  }
 return longestWord // put.length for integer result
}
Ralph Dizon

längstes Wort in einer Zeichenfolge

function findLongestWordLength(str) {
  return Math.max(...str.split(" ").map(word => word.length));
}
Testy Tortoise

Ähnliche Antworten wie “Finden Sie das längste Wort in einer Zeichenfolge”

Fragen ähnlich wie “Finden Sie das längste Wort in einer Zeichenfolge”

Weitere verwandte Antworten zu “Finden Sie das längste Wort in einer Zeichenfolge” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen