【问题标题】:Need to get a string after a "word" in a string in c#需要在c#中的字符串中的“单词”之后获取一个字符串
【发布时间】:2013-02-06 13:27:22
【问题描述】:

我在 c# 中有一个字符串,我必须在字符串中找到一个特定的单词“code”,并且必须在单词“code”之后获取剩余的字符串。

字符串是

“错误描述,代码:-1”

所以我必须在上面的字符串中找到单词 code 并且我必须得到错误代码。 我见过正则表达式,但现在清楚地理解了。有什么简单的方法吗?

【问题讨论】:

  • 如果code 在所述字符串中出现两次会怎样?
  • 你能澄清一下 - 正则表达式有什么问题吗?您尝试过什么,目前拥有什么代码?
  • @LukeHennerley 代码可能出现两次或多次,但最后错误代码定义会是 code: error code

标签: c# string substring


【解决方案1】:

将此代码添加到您的项目中

  public static class Extension {
        public static string TextAfter(this string value ,string search) {
            return  value.Substring(value.IndexOf(search) + search.Length);
        }
  }

然后使用

"code : string text ".TextAfter(":")

【讨论】:

    【解决方案2】:
    string founded = FindStringTakeX("UID:   994zxfa6q", "UID:", 9);
    
    
    string FindStringTakeX(string strValue,string findKey,int take,bool ignoreWhiteSpace = true)
        {
            int index = strValue.IndexOf(findKey) + findKey.Length;
    
            if (index >= 0)
            {
                if (ignoreWhiteSpace)
                {
                    while (strValue[index].ToString() == " ")
                    {
                        index++;
                    }
                }
    
                if(strValue.Length >= index + take)
                {
                    string result = strValue.Substring(index, take);
    
                    return result;
                }
    
    
            }
    
            return string.Empty;
        }
    

    【讨论】:

      【解决方案3】:
      string originalSting = "This is my string";
      string texttobesearched = "my";
      string dataAfterTextTobeSearch= finalCommand.Split(new string[] { texttobesearched     }, StringSplitOptions.None).Last();
      if(dataAfterTextobeSearch!=originalSting)
      {
          //your action here if data is found
      }
      else
      {
          //action if the data being searched was not found
      }
      

      【讨论】:

        【解决方案4】:
        string toBeSearched = "code : ";
        string code = myString.Substring(myString.IndexOf(toBeSearched) + toBeSearched.Length);
        

        这样的?

        也许你应该处理丢失code :的情况...

        string toBeSearched = "code : ";
        int ix = myString.IndexOf(toBeSearched);
        
        if (ix != -1) 
        {
            string code = myString.Substring(ix + toBeSearched.Length);
            // do something here
        }
        

        【讨论】:

          【解决方案5】:
          var code = myString.Split(new [] {"code"}, StringSplitOptions.None)[1];
          // code = " : -1"
          

          您可以调整要拆分的字符串 - 如果您使用 "code : ",则返回数组的第二个成员 ([1]) 将包含 "-1",使用您的示例。

          【讨论】:

          • 拆分只需要params[] char,不需要params[] string :)
          • @LukeHennerley - Really?
          • 我的意思是说这不编译:P 我站得更正!
          • @LukeHennerley - 最简单的重载只需要params char[]。所有string[] 重载都需要额外的参数。
          【解决方案6】:

          更简单的方法(如果您唯一的关键字是 "code" )可能是:

          string ErrorCode = yourString.Split(new string[]{"code"}, StringSplitOptions.None).Last();
          

          【讨论】:

          • @LukeHennerley 这是初始化字符串数组的常用方法
          【解决方案7】:

          使用indexOf()函数

          string s = "Error description, code : -1";
          int index = s.indexOf("code");
          if(index != -1)
          {
            //DO YOUR LOGIC
            string errorCode = s.Substring(index+4);
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2022-01-17
            • 1970-01-01
            • 2018-09-30
            • 2014-10-03
            • 1970-01-01
            • 2013-09-07
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多