开发者

Compare 2 string with Regex (C#)

I have a correct string and then a string come anywhere. I must compare but this strings maybe not equal. Example Correct string is

SAAT:23:34

Coming string

SAAT:12:23

When I compare this strings, Answer must be true.

Patern like this

SAAT:..:..
Regex.IsMatch(); 

开发者_如何学Pythongive me t string but ı dont want to this.

How can I compare two string..


Based on the information you've provided and my lack of caffeine here is a solution:

    static bool IsEqual(String left, String right)
    {
        left = Regex.Replace(left, ":[0-9]*:[0-9]*", "");
        right = Regex.Replace(right, ":[0-9]*:[0-9]*", "");
        return left.Equals(right);
    }

    static void Main(string[] args)
    {
        Console.WriteLine(IsEqual("SAAT:232:34", "SAAT:12:23")); // True
        Console.WriteLine(IsEqual("PAAT:23:34", "SAAT:12:23")); // False
        Console.WriteLine(IsEqual("SAAT:23:34:HAT", "SAAT:12:23:HAT")); // True
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜