You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constgetLongestWord=(str,sperator=" ")=>{if(!str){returnstr;}letlongestWord={len: 0,word: ""};str.replace(/[,\.;]/g,"").split(sperator).forEach(word=>{const{ len }=longestWord;if(word.length>len){longestWord={len: word.length,
word
};}});returnlongestWord;};console.log(getLongestWord("I have an apple."));console.log(getLongestWord("I have a pen."));
/** * @description: 查找英文字符串中最长的单词,并返回它的长度 * @param {str} string 英文字符串 * @return {number} */functionfindLongestWordLength(str){letlongestWord="";letcurrentWord="";//逐字母遍历for(leti=0;i<str.length;i++){if(str.charAt(i)===" "){//当前字符是空格if(currentWord.length>longestWord.length)longestWord=currentWord;//检查该单词是否是最长的单词currentWord="";//清除当前单词}else{currentWord+=str.charAt(i);//非空格字符,添加该字符继续创建单词}}if(currentWord>longestWord)longestWord=currentWord;//到达字符串末尾,再次检查当前单词是否是最长的单词returnlongestWord;}constlongest=findLongestWordLength("The quick brown fox jumped over the lazy dog");console.log(longest);
方法二:通过split()方法进行分割单词
缺点:不适用于含有标点符号的英文字符串
letstr='Thank you for your company over the years, you are my best friend and love you!';functionfindLongestWord(str){if(typeofstr!=='string')returnfalse;constarr=str.split(' ');letmax=arr[0];for(leti=1;i<arr.length;i++){max=max.length>arr[i].length ? max : arr[i];}console.log(max);}
方法三:通过math()方法进行分隔
优点:适用于含有标点符号的英文字符串
letstr='Thank you for your company over the years, you are my best friend and love you!';functionfindLongestWord(str){if(typeofstr!=='string')returnfalse;constmatchArr=str.match(/[a-zA-Z]+/gi);returnmatchArr.reduce((acc,cur)=>{if(acc.length>cur.length)returnacc;returncur;});}
constgetLongestWord=(str,sperator=" ")=>{if(!str){returnstr;}letlongestWord={len: 0,word: ""};str.replace(/[,\.;]/g,"").split(sperator).forEach(word=>{const{ len }=longestWord;if(word.length>len){longestWord={len: word.length,
word
};}});returnlongestWord;};console.log(getLongestWord("I have an apple."));console.log(getLongestWord("I have a pen."));
functionfindLongestWordLength(str){// 将字符串分割成单词数组varwords=str.split(' ');// 初始化最长单词长度varmaxLength=0;// 遍历单词数组,更新最长单词长度for(vari=0;i<words.length;i++){// 移除单词中的标点符号等非字母字符varcleanWord=words[i].replace(/[^a-zA-Z]/g,'');// 更新最长单词长度if(cleanWord.length>maxLength){maxLength=cleanWord.length;}}returnmaxLength;}// 示例用法varsentence="The quick brown fox jumps over the lazy dog.";varresult=findLongestWordLength(sentence);console.log(result);// 输出:6(因为"jumps"是最长的单词)
Activity
rocky-191 commentedon May 23, 2019
function findLongestWord(str){
// let arr=str.split(" ");
let arr=str.replace(/[,|.|;]/," ").split(" ");
let longLength=0,word;
for(let i=0;i<arr.length;i++){
if(arr[i].length>longLength){
word=arr[i];
longLength=arr[i].length;
}
}
return {
longLength,word
};
}
let str="hello world,my name is tom";
console.log(findLongestWord(str).longLength,findLongestWord(str).word);
ghost commentedon May 23, 2019
这个题目的说明有点模糊,单词的定义是什么?麻烦解释下
tzjoke commentedon May 28, 2019
@Dai-Ning 当然是空格分割了。。
tzjoke commentedon May 28, 2019
如果有多个单词一样长,只要返回随便一个的话比较简单了
第一想法是split成数组,然后sort函数来排序,返回arr[0]及其length
@rocky-191 我觉得涉及到遍历的逻辑一般用声明式的函数会好一点,比如你的逻辑我马上就想到用reduce:
Samuel-world commentedon Jun 2, 2019
如果有多个最长长度一样的单词,返回多个要怎么做呢?
tzjoke commentedon Jun 2, 2019
@Samuel-world 还用reduce的思路,保存的是数组就行了
myprelude commentedon Jun 13, 2019
think2011 commentedon Jun 17, 2019
Kntt commentedon Jul 9, 2019
最长的单词有重复, 再统计下次数呢?
Konata9 commentedon Jul 11, 2019
思路还是以空格进行分割,然后对数组进行操作,多了一个对分割符的拓展。
Vi-jay commentedon Jul 29, 2019
ps:感觉可以用正则来搞
seho-dev commentedon Sep 4, 2019
function test(str){
// 根据空格分隔
str = str.split(" ");
return str.reduce((start,end) => {
return start.length >= end.length? start: end
})
}
function test1(str){
// 根据空格分隔
str = str.split(" ");
return str.sort((first,end) => {
if(first.length < end){
return first < end
}else{
return first > end
}
})
}
fanqingyun commentedon Sep 6, 2019
function getWordMaxLen(str){
return Math.max(... str.match(/\b[a-zA-z]+\b/g).map(item => {
return item.length
}))
}
JJL-SH commentedon Sep 18, 2019
7 remaining items
rni-l commentedon Jan 16, 2020
Seastyle commentedon Mar 7, 2020
gaoryrt commentedon Mar 9, 2020
如果标准只是「用(多个)空格分割」,
不知道这题难度在哪里:
倒是如果给一个辅助方法「判断字符串是否是合法英文单词」,把这道题理解为「在字符串中找到最长的合法英文单词」那还有点意思
c37csq commentedon May 20, 2020
function getLength(str) {
if (!str) return;
const strArr = str.split(' ');
let info = {}
strArr.forEach(item => {
if (JSON.stringify(info) === '{}') {
info.value = item
info.length = item.length
} else {
if (item.length > info.length) {
info.value = item
info.length = item.length
}
}
})
return info
}
adi0754 commentedon Jun 12, 2020
blueRoach commentedon Jul 8, 2020
Alex-Li2018 commentedon Aug 11, 2020
'ab , cd abcde'.split(' ').reduce((pre, cur) => {
console.log(pre, cur);
return pre.length > cur.length ? pre : cur
}, '')
CoderLeiShuo commentedon Aug 14, 2020
方法一:逐字符遍历,生成单词,比较长度
方法二:通过split()方法进行分割单词
方法三:通过math()方法进行分隔
laboonly commentedon Aug 31, 2020
smile-2008 commentedon Oct 26, 2020
codelo-99 commentedon Dec 4, 2021
const getLongWordLength = (text) => [...text.match((/([a-z]+)/gi))].reduce((pre, cur) => Math.max(pre, cur.length), 0)
xiaoqiangz commentedon Jun 1, 2022
let strArr = str.split(' ')
return strArr.reduce((pre, cur) => {
return pre.length > cur.length ? pre : cur
}, 0)
CoderLeiShuo commentedon Oct 11, 2022
an31742 commentedon Nov 23, 2023
你可以通过编写一个函数来找到字符串中最长的单词,并返回其长度。以下是一个使用 JavaScript 的示例:
这个函数首先使用
split(' ')
将字符串分割成单词数组,然后通过一个循环遍历数组中的每个单词,使用replace
方法去除单词中的标点符号等非字母字符,最后比较单词的长度并更新最长单词长度。CoderLeiShuo commentedon Nov 23, 2023