C# find exact-match in string

前端 未结 4 1549
生来不讨喜
生来不讨喜 2021-02-07 22:18

How can I search for an exact match in a string? For example, If I had a string with this text:

label
label:
labels

And I search for label, I only want

4条回答
  •  北荒
    北荒 (楼主)
    2021-02-07 23:06

    You could try a LINQ version:

    string str = "Hello1 Hello Hello2";
    string another = "Hello";
    string retVal = str.Split(" \n\r".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
                       .First( p => p .Equals(another));
    

提交回复
热议问题