How do I exactly match a word with C#'s contains function?

独自空忆成欢 提交于 2019-12-23 10:25:35

问题


I am trying to read out scripts through C# and determine if they contain certain words, but these words should be identical to instead of only containing what I'm looking for. Is there a way to use the contains-function, single out the word, and check if it is identical to the exact word?

How can I determine if it both contains and is identical to the search term?

Currently I am using the following script:

// GetScriptAssetsOfType<MonoBehaviour>() Returns all monobehaviour scripts as text
foreach (MonoScript m in GetScriptAssetsOfType<MonoBehaviour>())
{
    if (m.text.Contains("Material"))
    {
       // Identical check?
    }
}

回答1:


How about a regex?

bool contains = Regex.IsMatch(m.text, @"\bMaterial\b");



回答2:


Use another check inside. I hope I understood your problem.

// GetScriptAssetsOfType<MonoBehaviour>() Returns all monobehaviour scripts as text
foreach (MonoScript m in GetScriptAssetsOfType<MonoBehaviour>())
{
    if (m.text.Contains("Material"))
    {
        if(m.text == "Material")
        {
             //Do Something
        }
    }
}



回答3:


Contains will search for the exact word you put as parameter check this as example in a small program

string a = "This is a test";
string b = "This is a TEST";
Console.WriteLine(a.Contains("test"));
Console.WriteLine(b.Contains("test"));

Hope I understood well your problem




回答4:


just using extension method to make the Regex Handy

Extension class

using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication11
        {
            public static class Extension
            {
                public static Match RegexMatch(this string input, string pattern, RegexOptions regexOptions = RegexOptions.IgnoreCase)
                {
                    return Regex.Match(input, pattern, regexOptions);
                }
            }
        }

and using of above class.

        using System.Text;
        namespace ConsoleApplication11
        {
            class Program
            {
                static void Main(string[] args)
                {
                    bool isMatch = "this is text ".RegexMatch(@"\bis\b").Success;
                }
            }
        }

see extension methods if not familiar http://www.codeproject.com/Articles/573095/A-Beginners-Tutorial-on-Extension-Methods-Named-Pa




回答5:


So you are looking for a regexp instead of Contains, now I understand your problem

string a = "This is a test for you";
string b = "This is a testForYou";
string c = "test This is a for you";
string d = "for you is this test.";
string e = "for you is this test, and works?";

var regexp = new Regex(@"(\stest[\s,\.])|(^test[\s,\.])");


Console.WriteLine(regexp.Match(a).Success);
Console.WriteLine(regexp.Match(b).Success);
Console.WriteLine(regexp.Match(c).Success);
Console.WriteLine(regexp.Match(d).Success);
Console.WriteLine(regexp.Match(e).Success);


来源:https://stackoverflow.com/questions/26013718/how-do-i-exactly-match-a-word-with-cs-contains-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!