C # compare ignoreCase string

As part of this testing method, I need to compare user3 strings, ignoring case sensitivity. I think I should use CultureInfo.InvariantCulture for ignoreCase. Is this the best way to achieve this, or is there a better way?

//set test to get user AsaMembershipProvider prov = this.GetMembershipProvider(); //call get users MembershipUser user1 = prov.GetUser("test.user", false); //ask for the username with deliberate case differences MembershipUser user2 = prov.GetUser("TeSt.UsEr", false); //getting a user with Upper and lower case in the username. MembershipUser user3 = prov.GetUser("Test.User", false); //prove that you still get the user, Assert.AreNotEqual(null, user1); Assert.AreNotEqual(null, user2); //test by using the ".ToLower()" function on the resulting string. Assert.AreEqual(user1.UserName.ToLower(), user2.UserName.ToLower()); Assert.AreEqual(user1.UserName, "test.user"); Assert.AreEqual(user3.UserName, "test.user"); 
+4
source share
3 answers

Using Assert.AreEqual with the ignoreCase parameter is better, because it does not require a new line (and, as out by @dtb points out, you can work by the rules of specific culture information)

 Assert.AreEqual(user1.UserName, user2.UserName, true, CultureInfo.CurrentCulture); 
+5
source

StringInstance.ToUpperInvariant ()

 user1.UserName.ToUpperInvariant() == user3.UserName.ToUpperInvariant(); user3.UserName.ToUpperInvariant() == "TEST.USER"; 
+3
source

It has a simple form; you can compare two lines, ignoring their case, as shown below.

 Assert.AreEqual(0,string.Compare("test", "TEST", true)); 

I'm not sure; why you need to go the way of a non-competitive case, since the case is a simple (not localized) case of unit test. Having said that, if you still want to go in this direction, refer to this link.

+1
source

Source: https://habr.com/ru/post/1479998/


All Articles