需要在c#中的字符串中的“单词”之后获取字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14998595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:42:55  来源:igfitidea点击:

Need to get a string after a "word" in a string in c#

c#stringsubstring

提问by Narayan

i'm having a string in c# for which i have to find a specific word "code" in the string and have to get the remaining string after the word "code".

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

The string is

字符串是

"Error description, code: -1"

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

so i have to find the word codein the above string and i have to get the error code. I have seen regex but now clearly understood. Is there any simple way ?

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

采纳答案by xanatos

string toBeSearched = "code : ";
string code = myString.Substring(myString.IndexOf(toBeSearched) + toBeSearched.Length);

Something like this?

像这样的东西?

Perhaps you should handle the case of missing code :...

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

string toBeSearched = "code : ";
int ix = myString.IndexOf(toBeSearched);

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

回答by Oded

var code = myString.Split(new [] {"code"}, StringSplitOptions.None)[1];
// code = " : -1"

You can tweak the string to split by - if you use "code : ", the second member of the returned array ([1]) will contain "-1", using your example.

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

回答by asifsid88

use indexOf()function

使用indexOf()功能

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

回答by Nogard

Simpler way (if your only keyword is "code" ) may be:

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

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

回答by user3488501

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
}

回答by Caner LENGER

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

回答by hossein sedighian

add this code to your project

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

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

then use

然后使用

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