简体   繁体   English

需要从字符串中的 X 中查找并删除下两个单词

[英]Need to find and remove the two next words from X in string

I am working on an assignment which I just cant figure out.我正在做一个我想不通的作业。

"Prompt the user for a positive integer. If it is, find the first word in the text which has the same length as the provided number (X). From the string, remove the two words in the two positions after the position of X, and at the same place insert the copy of X. As a result, you should achieve a string that contains one word less and two of the same words close to each other." "提示用户输入正数integer。如果是,则找到文本中第一个与提供的数字(X)长度相同的单词。从字符串中,删除X的position之后两个位置的两个单词, 并在同一个地方插入 X 的副本。结果,你应该得到一个包含少一个词和两个相同词彼此接近的字符串。”

I have managed to get X but cant get the the following words.我已经设法得到 X 但无法得到以下单词。 Anyone have any suggestions?有人有什么建议吗? :) :)

 let spliceText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus ornare mauris eget tortor accumsan posuere. Mauris pharetra pellentesque libero, ut cursus eros consectetur nec. Suspendisse id erat vitae tellus cursus rutrum ut sit amet nisi. Aliquam cursus ultrices nisl in vestibulum. Nunc lacinia metus a venenatis pretium. Nullam vitae tincidunt ante. Duis posuere, dolor ac accumsan consequat, ex mi congue sem, sit amet fringilla tellus velit at neque. Donec luctus mi eu ligula volutpat semper. Maecenas vulputate bibendum velit, at finibus velit consectetur sed. Maecenas commodo feugiat lorem, vitae eleifend velit iaculis ut. Duis ac suscipit nisl. Sed vel metus."; let spliceText1 = spliceText.replaceAll(".", "").replaceAll(",", ""); let length = parseInt(prompt("Specify the word's length:")); {} if (isNaN(length) && length < 0) {} const match = spliceText1.split(' ').filter(x => x.length === length); let newmatch = (match[0]); console.log(newmatch)

I'd divide the string up into an array of "words" (you've already found split ), then find the index of the first word with the given length (for instance, with findIndex ).我会将字符串分成一个“单词”数组(您已经找到了split ),然后找到具有给定长度的第一个单词的索引(例如,使用findIndex )。 Then, that index + 1 and + 2 are the indexes of the next two words after it.然后,那个索引+ 1+ 2是它后面接下来的两个单词的索引。 You can remove those either by creating a new array and leaving them out, or by using splice to remove them.您可以通过创建一个新数组并将它们排除在外,或者使用splice来删除它们来删除它们。 I wouldn't remove punctuation except for the purposes of comparison (unless you've been told you can), since presumably you need to reassemble the string with the original punctuation intact (probably with [ join ][3]).除非出于比较目的,我不会删除标点符号(除非您被告知可以),因为大概您需要重新组合原始标点符号完整的字符串(可能使用 [ join ] [3])。 (I'm assuming it's okay if two spaces in a row are converted to one or similar.) (我假设如果一行中的两个空格转换为一个或类似的空格是可以的。)

Reiterating in list form:以列表形式重申:

  • Get the desired word length得到想要的字长
  • Split the array on spaces在空格上拆分数组
  • Use findIndex to find the first word with that length (disregarding punctuation)使用findIndex查找具有该长度的第一个单词(忽略标点符号)
  • Either create a new array leaving out the words at that index + 1 and + 2 , or use a call to splice to remove the next two entries from the array创建一个新数组,删除该索引处的单词+ 1+ 2 ,或者使用调用splice从数组中删除接下来的两个条目
  • Recombine the string with joinjoin重新组合字符串

(We don't normally answer homework questions with working code, hence just having description.) (我们通常不会用工作代码回答家庭作业问题,因此只有描述。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM