美好的一天,
我有一个Dictionary<String, String[])>
,其中一个键=&gt;值示例如下:
{ "Eten/drinken", new string[] { "canteen", "mcdonald's", "mimi" } }
我需要将字符串part
与Dictionary中的一个值进行比较:
if (categories.Any(x => x.Value.Contains(part))) {
category = categories.FirstOrDefault(x => x.Value.Contains(part)).Key;
}
在一个方案part = "mcdonald's veghel veghel"
中,与“词典”值进行比较后返回false
。
为什么这是假的? "mcdonald's veghel veghel"
确实包含mcdonald's
,零空间比较确实如此。
答案 0 :(得分:3)
lambda x.Value.Contains(part)
内的表达式意味着{"canteen", "mcdonald's", "mimi"}
的任何元素都包含字符串"mcdonald's veghel veghel"
,即false
。您想要条件的倒数,即长字符串part
包含列表中的任何关键字的位置:
categories.Any(x => x.Value.Any(s => part.Contains(s)))
答案 1 :(得分:2)
实际上library(shiny)
library(shinyjs)
library(jsonlite)
sentences <- data.frame(
group = c("A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B"),
sentence = c("Phrases", "cats are better than dogs", "cats eat kibble",
"cats are better than hamsters", "cats are awesome", "cats are people too",
"cats eat mice", "cats meowing", "cats in the cradle", "cats eat mice",
"cats in the cradle lyrics", "cats eat kibble", "cats for adoption",
"cats are family", "cats eat mice", "cats are better than kittens",
"cats are evil", "cats are weird", "cats eat mice"),
stringsAsFactors = FALSE
)
drawChart <- function(sentences, word){
sprintf("google.charts.load('current', {packages:['wordtree']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable(
%s
);
var options = {
wordtree: {
format: 'implicit',
word: '%s'
}
};
var chart = new google.visualization.WordTree(document.getElementById('wordtree_basic'));
chart.draw(data, options);
}
", toJSON(sentences), word)
}
shinyApp(
ui = fluidPage(
tags$head(
tags$script(src="https://www.gstatic.com/charts/loader.js")
),
useShinyjs(),
fluidRow(
column(width=6,
selectInput("filter", label="select group", choices=c("A","B"), selected="A", selectize=FALSE)
),
column(width=6,
textInput("word", "type word", "cats")
)
),
fluidRow(tags$head(titlePanel("title panel"))),
fluidRow(tags$div(id="wordtree_basic", style="width: 900px; height: 500px;"))
),
server = function(input, output, session) {
observe({
dat <- subset(sentences, group==input$filter)$sentence
sentences <- array(dat, dim=c(length(dat),1))
runjs(drawChart(sentences, input$word))
})
}
)
方法将检查给定字符串中存在的任何指定子环。遗憾的是,没有String.Contains
包含给定的字符串,而给定的值包含x.Value的项目,因此您必须更改您的查询,如下所示:
x.Value