C#:匹配字符串中的精确单词

C#:匹配字符串中的精确单词,c#,string,C#,String,我有一个包含键值对的字符串,比如“this:1是:2a:3bigtest:4和:5i:6want:7到:8pass:9in:10 this:11test:12和:13string:14 continues:15” 现在我想从这个字符串中提取标记(比如test)的值。我无法提取标记测试的值12,因为我编写的匹配逻辑与tab Bigtest匹配,并将输出作为4 我是C#的新手,所以在这方面需要一些专家帮助 我的逻辑:message是包含键值的字符串,attribute是标记的名称(test) 提前感

我有一个包含键值对的字符串,比如“this:1是:2a:3bigtest:4和:5i:6want:7到:8pass:9in:10 this:11test:12和:13string:14 continues:15”

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

我是C#的新手,所以在这方面需要一些专家帮助

我的逻辑:message是包含键值的字符串,attribute是标记的名称(test)


提前感谢。

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

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 result=pair.Split(“:”); if(结果[0]==测试字) { 输出=结果[1]; 打破 } }


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

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

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 result=pair.Split(“:”); if(结果[0]==测试字) { 输出=结果[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()
,并更改函数的数据类型。

假设字符串始终采用“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()
,并更改函数的数据类型。

有很多方法:

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

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

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

          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);
      
      string message=“this:1是:2 a:3 Bigtest:4和:5 i:6 want:7到:8 pass:9 in:10 this:11 test:12和:13 string:14 continues:15”;
      string searchWord=“this”;
      int-foundWords=0;
      字符串[]arg=message.Split(新字符[]{''});
      int count=message.Split(“”).Length;
      for(int i=0;i
      更新:
      //要在其中搜索的消息。。
      string message=“this:1是:2 a:3 Bigtest:4和:5 i:6想要:7到:8通过:9 in:10 this:11测试:12和:13字符串:14继续:15”;
      //读单词。
      控制台。写下(“输入您的单词:”);
      字符串searchWord=Console.ReadLine();
      //Init integer变量,用于计算(消息)中的(世界)数量
      int-foundWords=0;
      //在空格处拆分消息字符串
      字符串[]arg=message.Split(新字符[]{''});
      //获取消息项的长度
      int count=message.Split(“”).Length;
      //循环浏览消息项
      for(int i=0;i