简体   繁体   中英

How to completely check, if a string is equal to a contained word in a string in c#?

Maybe the question is a little confused. I'm going to explain this in a better way look this code

string myfirststring = "hello1,hello123,content";
string_2 = "ent";
if (myfirststring.Contains(string_2)){
    Console.WriteLine("Yes! this is included in the 1 string");
    //then check if string_2 is equal to the 3 element of 1 string: content
}

Now I want to check that my contained string "string_2" is completely equal to to the 3 element of myfirststring: content (in this case not because the value is ent and not content so ent != content) So how can I check this?

If the words are comma separated, you can do the following:

var exactlyContained = myFirstString.Split(',').Any(w => w == string_2);

This will return true if it finds an exact match , false in any other case.

UPDATE

Based on commentaries, it seems your string has words delimited by varying sequences of symbols. Maybe you could use this custom split method (you could adapt it to your specific needs, I don't have a lot of info to go by):

public static IEnumerable<string> CustomSplit(this string s)
{
    var buffer = new StringBuilder();

    foreach (var c in s)
    {
        if (!char.IsLetterOrDigit(c))
        {
            if (buffer.Length > 0)
            {
                yield return buffer.ToString();
                buffer.Clear();
            }
        }
        else
        {
            buffer.Append(c);
        }
    }

    if (buffer.Length > 0)
        yield return buffer.ToString();
}

And now the code would be:

var exactlyContained = myFirstString.CustomSplit().Any(w => w == string_2);

So since your string has no specified delimiter and following your example string I would reduce all non-letter-digit characters and re-create your myFirstString .

string myFirstString = "hello1,hello123,content";
string yourSecondString = "ent";

At first I define a delimiter, which I need to do the split at the end.

char delimiter = '#';

Now I create a new StringBuilder to efficiently add the found characters and set the character I want to check if the words ends to a non-letter-digit-character.

var firstStringBuilder = new StringBuilder();
char previousChar = delimiter;

After the preparation I loop over all characters in the string and add all characters to the StringBuilder and reduce all non-letter-digit-characters to a single delimiter between the words.

foreach (char singleChar in myFirstString)
{
    if (Char.IsLetterOrDigit(singleChar))
    {
        firstStringBuilder.Append(singleChar);
        previousChar = singleChar;
    }
    else if(Char.IsLetterOrDigit(previousChar))
    {
        firstStringBuilder.Append(delimiter);
        previousChar = singleChar;
    }
}

After I finished the loop I will do the check if any word matches yourSecondString

bool matched = firstStringBuilder.ToString()
                                 .Split(delimiter)
                                 .Any(str => string.Equals(str, yourSecondString));

I know it's generally frowned upon for performance reasons, but regex makes this a relatively simple lookup if you're always dealing with a single word (and not a sentence etc) eg.

// regex "\b" indicates a word boundary, so surrounding your lookup string with them ensures your expression matches the entire word 
if (Regex.IsMatch(myfirststring, $"\b{string_2}\b")){
    Console.WriteLine("Yes! this is included in the 1 string");
    //then check if string_2 is equal to the 3 element of 1 string: content
}

EDIT: Added missing closing bracket on if statement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM