我已经能够找到有关如何删除字符串的最后一个单词的示例,但是在这种情况下,我试图删除字符串的最后3个单词。我尝试通过调整一些我遇到的答案来删除单个单词来尝试此操作,但是没有一个给我预期的结果。
示例字符串Highest ranked in the states
我希望我的返回值为Highest ranked
以下是我尝试过的代码片段:
let myString = "Highest ranked in the states";
myString = myString.substring(2, myString.lastIndexOf(" "));
console.log(myString)
let myString2 = "I want to remove the last word";
let mySplitResult2 = myString2.split(" ");
let lastWord = mySplitResult2[mySplitResult2.length-3]
console.log(lastWord)
通过将子字符串方法调整为(2, myString.lastIndexOf(" "));
,最终删除了我句子的前两个字母,只删除了states
一词,例如“来宾排名”。
将.split()方法的长度调整为-3时,它只是返回单词in
而不是in the states
答案 0 :(得分:2)
这是一个很好的可读性的衬纸:
<link rel="stylesheet" type="text/css" href="psi.css" media="all">
<script src="https://use.fontawesome.com/82c7176f2a.js"></script>
<div class="footer">
<nav class="footer-links">
<a class="footer-link" href="mailto:someone@yoursite.com" target="_blank"><i class="fa fa-envelope-o" aria-hidden="true"></i><span class="footer-link_text">email@yahoo.com</span></a>
<a class="footer-link" href="#link" target="_blank"><i class="fa fa-linkedin" aria-hidden="true"></i><span class="footer-link_text">Linkdn.com/profile</span></a>
<a class="footer-link" href="#link" target="_blank"><i class="fa fa-github" aria-hidden="true"></i><span class="footer-link_text">Github.com.com/profile</span></a>
</nav>
</div>
您可以通过以下方式轻松地将其概括为const remove3words = words => words.split(" ").slice(0, -3).join(" ");
console.log(remove3words("Highest ranked in the states"));
console.log(remove3words("Exactly three words"));
个单词:
n
答案 1 :(得分:0)
有关使用拆分和拼接的说明,请参见注释
let myString = "Highest ranked in the states";
//split the str using blank space between each word and add to new variable
let str = myString.split(" ");
//get the length
let num = str.length;
//splice the array removing the last three values with the number - 3
let newStr = str.splice(0, num - 3);
let displayText = '';
//back into string
newStr.forEach(function(value){
displayText += value + ' ';
});
display.textContent = displayText;
<div id="display"></div>
答案 2 :(得分:0)
let myString = "Highest ranked in the states";
myString = myString.split(' ')
myString = myString.splice(myString.length-5,2)
myString = myString.join(' ')
console.log(myString)
答案 3 :(得分:0)
这是可以为您完成此功能的功能。 (实际上,它可以从您指定的任何字符的倒数第二个字符中删除所有字符,而不仅仅是倒数第二个单词。)
在您的情况下:
char
参数应获取值' '
(即单词之间的空格)N
应该获得值3
(以倒数第三位为目标)我添加了一个 excludeChar
参数,您可以将其设置为true
,以避免返回最终空格(或者您可以对结果使用.trim
方法)。
const
myString = "Highest ranked in the states",
result = truncateAtNthToLastOccurencOfChar(myString, ' ', 3);
console.log(result);
function truncateAtNthToLastOccurencOfChar(str, char, N, excludeChar){
// Makes counter, converts str to arr & reverses it
let i = -1, rev = Array.from(str).reverse();
// Increments counter (and executes even when i=0)
while(++i || true){
// Returns original string if at the end `char` has occured fewer than `N` times
if(i >= rev.length - 1){
return str;
}
// If current character matches `char`, decrements `N`
if(rev[i] == char && --N === 0){
// If N=0, `i` is our target, all keeps characters (from reversed array)
// starting at `i` (or after `i` if excludeChar=true)
return rev
// The occurence of char can be excluded from result
.slice(excludeChar ? (i + 1) : i)
.reverse() // Restores original character order
.join(''); // Converts back to string
}
}
}