sar*_*ara 15 c# sharepoint web-parts
我有一个Sharepoint项目列表:每个项目都有标题,描述和类型.我成功检索了它,我打电话给它result
.我想首先检查是否有任何项目result
以A开头然后是B然后是C等.我将为每个字母字符做同样的事情然后如果我找到一个以这个字符开头的单词我将必须显示粗体字符.
我最初使用此功能显示字符:
private string generateHeaderScripts(char currentChar)
{
string headerScriptHtml = "$(document).ready(function() {" +
"$(\"#myTable" + currentChar.ToString() + "\") " +
".tablesorter({widthFixed: true, widgets: ['zebra']})" +
".tablesorterPager({container: $(\"#pager" + currentChar.ToString() +"\")}); " +
"});";
return headerScriptHtml;
}
Run Code Online (Sandbox Code Playgroud)
如何检查单词是否以给定字符开头?
Dmi*_*kin 33
要检查一个值,请使用:
string word = "Aword";
if (word.StartsWith("A"))
{
// do something
}
Run Code Online (Sandbox Code Playgroud)
您可以使用一个小扩展方法来传递包含A,B和C的列表
public static bool StartsWithAny(this string source, IEnumerable<string> strings)
{
foreach (var valueToCheck in strings)
{
if (source.StartsWith(valueToCheck))
{
return true;
}
}
return false;
}
if (word.StartsWithAny(new List<string>() { "A", "B", "C" }))
{
// do something
}
Run Code Online (Sandbox Code Playgroud)
并且作为奖励,如果你想知道你的字符串从列表开始,并根据该值做一些事情:
public static bool StartsWithAny(this string source, IEnumerable<string> strings, out string startsWithValue)
{
startsWithValue = null;
foreach (var valueToCheck in strings)
{
if (source.StartsWith(valueToCheck))
{
startsWithValue = valueToCheck;
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
用法:
string word = "AWord";
string startsWithValue;
if (word.StartsWithAny(new List<string>() { "a", "b", "c" }, out startsWithValue))
{
switch (startsWithValue)
{
case "A":
// Do Something
break;
// etc.
}
}
Run Code Online (Sandbox Code Playgroud)