C#:匹配字符串

时间:2016-12-11 17:36:24

标签: c# string

我有一个包含键值对的字符串,如"这个:1是:2 a:3最大:4和:5 i:6想要:7到:8传递:9 in:10 this:11测试:12和:13字符串:14继续:15"

现在我想从这个字符串中提取标签的值(比如测试)。我无法提取12的标记测试值,因为我编写的匹配逻辑与选项卡Bigtest匹配,输出为4。

我是C#的新手,所以需要一些专家帮助。

我的逻辑:message是包含键值的字符串,属性是tag(test)的名称

public static string GetAttributeValueByName(string message, string attributeName)
{
    int startIndex = message.IndexOf(attributeName + ":");

    string attribute = message.Substring(startIndex + (attributeName + "=").Length);

int position = attribute.IndexOf(' ', 1);


   if (position != -1)
{
    string attributeValue = attribute.Substring(1, position - 1);
    return attributeValue;
}

return "";
}

提前致谢。

4 个答案:

答案 0 :(得分:3)

如果您首先在空格处拆分字符串:

var pairs = input.Split(" ");

你最终会得到一个像这样的数组:

this:1
is:2
a:3
Bigtest:4
and:5
i:6
want:7
to:8
pass:9
in:10
this:11
test:12
and:13
string:14
continues:15

然后,您可以遍历冒号上的数组拆分的每个元素,并检查该对的第一个元素是否与您的测试词匹配。

字符串输出;     foreach(成对的var对)     {         var result = pair.Split(“:”);         if(result [0] == testWord)         {             output = result [1];             打破;         }     }

显然,您需要进行错误捕获和输入验证。

答案 1 :(得分:1)

假设字符串将始终采用“findstring:findvalue”格式,您可以执行以下操作:

private string GetValueFromString(string SearchString, string FindString)
{
    foreach (string Items in SearchString.split(' '))
    {
        string SubItems = Items.split(':');
        if (SubItems[0] == FindString)
        {
            return SubItems[1];
        }
    }
    return null;
}

string SearchString = "this:1 is:2 a:3 Bigtest:4 and:5 i:6 want:7 to:8 pass:9 in:10 this:11 test:12 and:13 string:14 continues:15";
Console.WriteLine(GetValueFromString(SearchString, "test"));

以上还假设您要搜索的文本不会重复。我的例程将值作为字符串返回。如果要转换为其他类型(如整数),则需要在返回值周围调用Convert.ToInt32()并更改函数的数据类型。

答案 2 :(得分:0)

有很多方法可以解决这个问题:

  • 在字符串的开头插入一个空格,然后搜索以空格开头的单词;
  • 使用与单词前面的单词边界匹配的正则表达式,方法是使用表达式中的\b元字符;
  • 使用String.Split函数将字符串拆分为字符串数组,然后查看每个数组元素中的第一个单词;
  • 通过以下方式更多地手动执行搜索:
    1. 在字符串的开头开始当前位置;
    2. 找到字符串中的下一个冒号;
    3. 检查从当前位置到该冒号的单词(如果它与您匹配,则完成);
    4. 找到字符串中的下一个空格;
    5. 将当前位置移动到该空格后面的字符;
    6. 从第2步开始重复,直到找到单词或到达字符串的结尾。

答案 3 :(得分:0)

试试这个:

    string message = "this:1 is:2 a:3 Bigtest:4 and:5 i:6 want:7 to:8 pass:9 in:10 this:11 test:12 and:13 string:14 continues:15";
    string searchWord = "this";
    int foundWords = 0;
    string[] arg = message.Split(new char[] { ' ' });
    int count = message.Split(' ').Length;
    for (int i = 0; i < count; i++)
    {
        if (arg[i].Contains(searchWord))
        {
            int index = arg[i].IndexOf(":") + 1;
            Console.WriteLine(arg[i].Substring(index, arg[i].Length - index));
            foundWords++;
        }
    }
    Console.WriteLine("found words: {0}", foundWords);

更新

//message that you want to search in..
            string message = "this:1 is:2 a:3 Bigtest:4 and:5 i:6 want:7 to:8 pass:9 in:10 this:11 test:12 and:13 string:14 continues:15";
        //read the word .
        Console.Write("Enter your word: ");
    string searchWord = Console.ReadLine();
        // Init integer variable to count how many (world) in (message)
    int foundWords = 0;
        //split message string at spaces
    string[] arg = message.Split(new char[] { ' ' });
        // get the length of the message items
    int count = message.Split(' ').Length;
        //loop through message items
    for (int i = 0; i < count; i++)
    {
        //check if first array item contains the desired word
        if (arg[i].Contains(searchWord))
        {
            //get the index of (:) mark
            int index = arg[i].IndexOf(":") + 1;
            //substring the string after the (:) word
            Console.WriteLine(arg[i].Substring(index, arg[i].Length - index));
            //increase the counter
            foundWords++;
        }
    }
    Console.WriteLine("found words: {0}", foundWords);
相关问题