如何检查是否一个词一个给定的字符开始? [英] How to check if a word starts with a given character?

查看:124
本文介绍了如何检查是否一个词一个给定的字符开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SharePoint项目清单:每个项目都有一个标题,描述和类型。
我顺利取出它,我把它叫做结果。我想先检查是否有在结果这与A则B然后是C开头的项,等等。我会做同样的每个字母字符,然后如果我觉得首先是这个角色,我将不得不显示粗体字一个字

I have a list of a Sharepoint items: each item has a title, a description and a type. I successfully retrieved it, I called it result. I want to first check if there is any item in result which starts with A then B then C, etc. I will have to do the same for each alphabet character and then if I find a word starting with this character I will have to display the character in bold.

我最初使用此功能显示的字符:

I initially display the characters using this function:

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;
}



我如何检查是否一个词一个给定的字符开头?

How can I check if a word starts with a given character?

推荐答案

要检查一个值,使用:

    string word = "Aword";
    if (word.StartsWith("A")) 
    {
        // do something
    }

您可以做一个小的扩展方法传递与A,b和C

You can make a little extension method to pass a list with A, B, and 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
    }

和作为奖励,如果你想知道你的字符串开头,从列表中,并以此为基础进行有价值的东西:

AND as a bonus, if you want to know what your string starts with, from a list, and do something based on that value:

    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;
    }



用法:

Usage:

    string word = "AWord";
    string startsWithValue;
    if (word.StartsWithAny(new List<string>() { "a", "b", "c" }, out startsWithValue))
    {
        switch (startsWithValue)
        {
            case "A":
                // Do Something
                break;

            // etc.
        }
    }

这篇关于如何检查是否一个词一个给定的字符开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆