我有一个字符串数组和另一个字符串,有没有一种简单的方法来测试我的单个字符串是否以字符串数组中的一个字符串结尾?
const strings = [
'foo',
'bar',
'test'
];
if ('hi, this is a test'.endsWith(...strings)) { // I know that this isn't right but this is the idea
console.log("String ends with one of the strings !");
}
Run Code Online (Sandbox Code Playgroud) 我想验证字符串是否以 JavaScript 中的空格结尾。提前致谢。
var endSpace = / \s$/;
var str = "hello world ";
if (endSpace.test(str)) {
window.console.error("ends with space");
return false;
}
Run Code Online (Sandbox Code Playgroud) 我可以使用dplyr :: select(ends_with)选择适合多种条件的列名称。考虑到我的列名,我想使用结尾于而不是包含或匹配项,因为我要选择的字符串在列名的末尾相关,但也可能出现在其他中间。例如,
df <- data.frame(a10 = 1:4,
a11 = 5:8,
a20 = 1:4,
a12 = 5:8)
Run Code Online (Sandbox Code Playgroud)
我想选择以1或2结尾的列,以仅具有a11和a12列。select(ends_with)是最好的方法吗?
谢谢!
我试图在给定扩展名的目录中对文件进行排序,但提供了我先给出的订单.假设我想要扩展订单
ext_list = [ .bb, .cc , .dd , aa ]
Run Code Online (Sandbox Code Playgroud)
我能想到的唯一方法是遍历每个文件,并在每次遇到特定扩展时将它们放在列表中.
for subdir, dirs, files in os.walk(directory):
if file.endswith( '.bb') --> append file
then go to the end of the directory
then loop again
if file.endswith( '.cc') -->append file
and so on...
return sorted_extension_list
Run Code Online (Sandbox Code Playgroud)
然后最后
for file in sorted_extension_list :
print file
Run Code Online (Sandbox Code Playgroud) 我正在寻找的等价物find $DIR -iname '*.mp3'
,但我不想做怪异的['mp3', 'Mp3', MP3', etc]
事情。但是我不知道如何将这些re*.IGNORECASE
东西与简单endswith()
方法结合起来。我的目标是不遗漏单个文件,我希望最终将其扩展到其他媒体/文件类型/后缀。
import os
import re
suffix = ".mp3"
mp3_count = 0
for root, dirs, files in os.walk("/Volumes/audio"):
for file in files:
# if file.endswith(suffix):
if re.findall('mp3', suffix, flags=re.IGNORECASE):
mp3_count += 1
print(mp3_count)
Run Code Online (Sandbox Code Playgroud)
TIA的任何反馈
我试图计算以几个后缀结尾的单词的出现次数。我认为那endswith
可以接受迭代。不幸的是,事实并非如此。下面是代码片段:
s = 'like go goes likes liked liked liking likes like'
lst = s.split()
suffixes = ['s', 'es', 'ies', 'ed', 'ing']
counter = 0
prompt = 'like'
for x in lst:
if x.startswith(prompt) and x.endswith(any(suffix for suffix in suffixes)):
counter += 1
Run Code Online (Sandbox Code Playgroud)
的值counter
应4
在执行结束时。这是显示的错误消息:
TypeError: endswith first arg must be str or a tuple of str, not bool
Run Code Online (Sandbox Code Playgroud)
如何使以上代码起作用?
我有这样的声明:
string_tokens[-1].ends_with?(",") || string_tokens[-1].ends_with?("-") || string_tokens[-1].ends_with?("&")
Run Code Online (Sandbox Code Playgroud)
我想将所有标记(","
、"-"
、"&"
)放入一个常量中,并简化上面的内容来询问“字符串是否以这些字符结尾”,但我不知道该怎么做。
我正在尝试检查数据帧的列中的值是否以向量中包含的字符串的预定义列表中的任何值结尾。我知道可以使用endsWith检查单个字符串,但是我还没有找到一种使用此方法来检查字符串列表的方法。
x <- c(" 2", " 3", " II", " III")
title <- c("a 2", "adbs", "ddig", "difo 3", "fgij III", "sdgoij")
endsWith(title, x)
Run Code Online (Sandbox Code Playgroud)
我不希望该代码执行我想要的操作,但是我正在寻找的代码是使用上述数据提供以下输出的代码。否否否是否
我正在开发一个处于早期阶段的小型翻译软件。我试图获取一个单词的结尾字符并用它做一些事情,但我需要区分不同的情况。
我该如何使用if endswith("a" or "b" or "c") : doAThing
?
我已经尝试过||
,,|=
和or
。
if word.endswith("a" , "e" , "i" , "o" , "u") : print("JEFF")
Run Code Online (Sandbox Code Playgroud)
错误:
if word.endswith("a" , "e" , "i" , "o" , "u") : print("JEFF")
Run Code Online (Sandbox Code Playgroud) 如何在纯 JavaScript 中检查字符串的最后一个字符是否是数字/数字?
function endsWithNumber(str){
return str.endsWith(); // HOW TO CHECK IF STRING ENDS WITH DIGIT/NUMBER ???
}
var str_1 = 'Pocahontas';
var str_2 = 'R2D2';
if (endsWithNumber(str_1)) {
console.log(str_1 + 'ends with a number');
} else {
console.log(str_1 + 'does NOT end with a number');
}
if (endsWithNumber(str_2)) {
console.log(str_2 + 'ends with a number');
} else {
console.log(str_2 + 'does NOT end with a number');
}
Run Code Online (Sandbox Code Playgroud)
我也想知道最快的方法是什么?我想这可能听起来很荒谬:D,但在我的用例中,我经常需要这种方法,所以我认为它可能会有所作为。